LISTSERV at the University of Georgia
Menubar Imagemap
Home Browse Manage Request Manuals Register
Previous (more recent) messageNext (less recent) messagePrevious (more recent) in topicNext (less recent) in topicPrevious (more recent) by same authorNext (less recent) by same authorPrevious page (June 1998, week 2)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:         Tue, 9 Jun 1998 10:30:30 -0400
Reply-To:     Sherrard Burton <sburton@RC.PHS.WFUBMC.EDU>
Sender:       "SAS(r) Discussion" <SAS-L@UGA.CC.UGA.EDU>
From:         Sherrard Burton <sburton@RC.PHS.WFUBMC.EDU>
Subject:      Re: Difficult programming problem
In-Reply-To:  <199806070456.AAA01117@rc.phs.wfubmc.edu>

> ---------- > Van: Sherrard Burton[SMTP:SBURTON@RC.PHS.WFUBMC.EDU] > Verzonden: dinsdag 9 juni 1998 16:30:30 > Aan: SAS-L@AKH-WIEN.AC.AT > Onderwerp: Re: Difficult programming problem > Automatisch doorgezonden door een regel > I had a second so I thought I'd take a stab at the 'number of consecutive ones' problem. I beleive that this solution is quite lengthy but effective. I just write the rows columns and diagonals out into a continuous string of array elements with each separated by a missing. Then I run each element through a loop and change the 'switch' variable only when the value changes. I then use some sql code to count the number of consecutive ones, zeros, and .'s. I got the idea from a simple compression type algorithm that takes numbers of consecutive ones and zeros and collapses them to the form (1 or 0)(number of consecutive), which is essentially what this code does. So you can actually tell how many consecutive ones and zeros you have whether it's groups of two, three or four, etc. I hope that this is the desired solution.

data temp; retain switch 0; keep num switch; array b(144) b1-b144; ARRAY A(5,6) (0,1,1,0,1,1, 1,1,1,0,0,1, 1,0,1,1,1,0, 0,1,0,1,0,1, 1,1,1,0,0,1); i=1; rowdim=dim(a,1); coldim=dim(a,2);

do row=1 to rowdim; do col=1 to coldim; b(i)=a(row,col); i=i+1; end; i=i+1; end; i=i+1;

do col=1 to coldim; do row=1 to rowdim; b(i)=a(row,col); i=i+1; end; i=i+1; end; i=i+1;

do j=1 to (rowdim-1); row=j; col=1; do while ((1 le row le rowdim) and (1 le col le coldim)); b(i)=a(row,col); i=i+1; row=row+1; col=col+1; end; i=i+1;

row=j; col=coldim; do while ((1 le row le rowdim) and (1 le col le coldim)); b(i)=a(row,col); i=i+1; row=row+1; col=col-1; end; i=i+1; end;

do j=2 to (coldim-1); row=1; col=j; do while ((1 le row le rowdim) and (1 le col le coldim)); b(i)=a(row,col); i=i+1; row=row+1; col=col+1; end; i=i+1;

row=1; col=j; do while ((1 le row le rowdim) and (1 le col le coldim)); b(i)=a(row,col); i=i+1; row=row+1; col=col-1; end; i=i+1; end;

do x=1 to dim(b); num=b(x); if x ge 2 then do; if b(x-1) ne b(x) then switch=switch+1; end; output; end;

run;

proc sql; create table list as select distinct num, count(*) as cons label="Consecutive" from temp group by switch order by switch; quit; proc sql; select count(*) label="Count of consecutive ones" from list where num=1 and cons gt 1;

================================================================ _/_/_/ _/_/_/_/ Sherrard Burton _/ _/ _/ _/ Department of Public Health Sciences _/ _/ _/ Wake Forest University School of Medicine _/_/_/ _/_/_/_/ Winston-Salem, NC 27517-1063 _/ _/ _/ _/ _/ _/ _/ Email: sburton@rc.phs.wfubmc.edu _/_/_/ _/_/_/_/ Fax: (336) 716-5425 Phone (336) 716-2983 ================================================================


Back to: Top of message | Previous page | Main SAS-L page