Date: Sun, 18 Mar 2001 22:10:39 GMT
Reply-To: Roger Lustig <julierog@IX.NETCOM.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Roger Lustig <julierog@IX.NETCOM.COM>
Subject: Re: scan value skips inside an variable, by group
Content-Type: text/plain; charset=us-ascii
...and here's a corrected version...
Roger Lustig wrote:
>
> Here's a non-SQL way, using PROC SUMMARY. This may be faster, and
> is probably easier to understand (especially if you're not into PROC SQL
> yet). The "faster" is based on the data already being sorted by ID.
>
> data prep;
> set my_data;
> by id;
> retain first_0;
> if first.id then do;
> first_0=.;
> position=0;
> end;
> position + 1; **** Accumulators get retained automatically;
> if first_0=. and half=0 then first_0=position;
> run;
>
> proc summary data=prep;
> by id;
> var first_0 half;
> output
> out=split (drop=_type_)
> max(first_0)=sumfirst /*Could use min or mean here instead...but not sum!!!*/
> sum(half)=n_ones
> ;
> run;
>
> data
> type1
> type2
> type3
> ;
>
> merge
> prep
> split
> ;
> by id;
>
> drop _freq_ position first_0 sumfirst n_ones; *** Housekeeping;
>
> if n_ones=_freq_ then output type2; *** All ones = no zeros;
>
> else if n_ones=1 then output type1; *** Only one? The first one.;
>
> else if position >= first_0 then output type3; *** No output until we get to a 0;
>
> run;
>
> Roger
>
> PD wrote:
> >
> > To Whom This May Interest,
> >
> > This question I have is essentially to scan a variable. This variable takes
> > values 1 or 0, length 1, numeric. I need to scan the variable vertically,
> > from first observation to the last observation. This variable, called Half,
> > is sorted by ID field called ID. So the data look like this
> > "
> > ID Half
> > 1 1
> > 1 0
> > 1 0
> > 3 1
> > 3 1
> > 3 0
> > 3 0
> > 3 1 ........"
> >
> > For each unique ID value, there is at least one Half value =1. It is not
> > possible that Half values are all 0s for an unique ID value. Also, every by
> > group starts with the Half value=1. That is, for every first.ID, Half=1. ID
> > values such as 3 here may repeat itself up to 36 times. Or another unique ID
> > values may only have, 3, 4, 5, 6... occurences.
> >
> > What I want to do is this: scan Half from the top of each group to the end
> > of it. If the first Half value=1 is the only 1 values through out the whole
> > history of the ID group, output it to a data set called set1. If an ID group
> > has no occurence of 0, that is, value 1s all the way, output it to data set
> > called set 2. If an ID group has 1s and 0s mixed after the first 1 value at
> > first.ID, delete all the group's history/obserations prior to the first
> > occurence of 0 and output it to data set called set3.
> >
> > Any help or tips are greatly appreciated. Or somebody can just refer me to
> > some SAS publications that can solve the problem, that I am not aware of. I
> > am still digging this group's previous msgs archived at UGA to check out
> > previous postings. Thank you.
> >
> > Paula D X
|