|
Counting the size of by groups seems a common requirement. I am just curious
if the categories in Half are more than 1 or 0 can represetn, then...
Thanks for your Help. I will test out this SQL proc and will let you know.
Paula
"Ya Huang" <huanga@worldnet.att.net> wrote in message
news:grYs6.3923$M%2.307349@bgtnsc05-news.ops.worldnet.att.net...
> Paula,
>
> I offer another solution based on sql. Before I use
> proc sql, I have to add a in-group order variable n, which is
> done by the first data step, this is necessary because sql can
> not track the physical order of the observations. The second step is
> proc sql, it adds three flags. Based on the three flags,
> the third data step can then seperate the data into three::
>
> data t1;
> input ID Half;
> cards;
> 1 1
> 1 0
> 1 0
> 3 1
> 3 1
> 3 0
> 3 0
> 3 1
> 4 1
> 4 1
> 4 1
> ;
>
> data t1;
> set t1;
> by id;
> retain n;
> if first.id then n=0;
> n+1;
>
> proc sql;
> create table t1 as
> select *,
> sum(half)=1 as flag1, /* you've guaranteed that every first.id, half=1
*/
> sum(half)=count(*) as flag2,
> min(case when half=0 then n else . end) as first0n
> from t1
> group by id
> order by id,n
> ;
>
> data ts1(keep=id half) ts2(keep=id half) ts3(keep=id half);
> set t1;
> if flag1=1 then output ts1;
> else if flag2=1 then output ts2 ;
> else if flag1=0 and flag2=0 then do;
> if n<first0n then delete;
> else output ts3;
> end;
>
> proc print data=ts1;
> proc print data=ts2;
> proc print data=ts3;
> run;
>
> HTH
>
> Ya Huang
>
>
>
>
> PD wrote in message ...
> >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
> >
> >
> >
>
>
|