Date: Thu, 20 Mar 2003 10:22:55 -0800
Reply-To: cassell.david@EPAMAIL.EPA.GOV
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: "David L. Cassell" <cassell.david@EPAMAIL.EPA.GOV>
Subject: Re: selecting consequent observations problem
Content-type: text/plain; charset=us-ascii
"Primak, Philip" <Philip.Primak@GENZYME.COM> wrote [in part]:
> I have to distinguish those observations, where A is consequent 7
numbers.
> Another words I need to create variable B, which will be equal to 1
for
> observations 4-10 and 0 otherwise.
I looked at the other solutions, and decided to add this one.
Note that I do a complete evaluation of A{i} to A{i+6}, since
I could come up with mathematical constructs where just checking
the sum of the 7 numbers, or the (min and max and mean together)
were not enough to guarantee 7 consecutive numbers. This solution
will also handle cases where you have multiple strings of 7 which
overlap.
/* first get number of records, including missings */
proc sql noprint;
select count(a)+nmiss(a) into :numrecs
from temp1;
quit;
data temp2 (keep=obs a b);
array aa{&NUMRECS} _temporary_;
array bb{&NUMRECS} (&NUMRECS * 0) ;
/* the ever-popular DOW-loop */
do _n_ = 1 by 1 until(eof);
set temp1 end=eof;
aa{_n_} = a;
end;
/* do a complete check - going by the mean/min/max isn't enough */
do _n_ = 1 to (dim(aa)-6);
if (aa{_n_+1}=aa{_n_}+1) & (aa{_n_+2}=aa{_n_}+2) & (aa{_n_+3}=aa
{_n_}+3)
& (aa{_n_+4}=aa{_n_}+4) & (aa{_n_+5}=aa{_n_}+5) & (aa{_n_+6}=aa
{_n_}+6)
then do _i_=_n_ to _n_+6; bb{_i_}=1; end;
end;
/* and output the desired variables to TEMP2 */
do _n_ = 1 to dim(aa);
a = aa{_n_};
b = bb{_n_};
output;
end;
run;
proc print data=temp2 noobs; run;
And, as desired, you get:
obs a b
1 . 0
2 1 0
3 2 0
4 2 1
5 3 1
6 4 1
7 5 1
8 6 1
9 7 1
10 8 1
11 8 0
12 9 0
13 99 0
HTH,
David
--
David Cassell, CSC
Cassell.David@epa.gov
Senior computing specialist
mathematical statistician