Hi all,
I have the data set
ID Status
1 V
1 V
1 V
1 I
1 I
2 I
2 I
2 I
3 V
3 I
3 I
3 I
and I would like to flag out the first occurrence for each different value of STATUS within the by - group (:by ID)
ID Status Flag
1 V 1
1 V 0
1 V 0
1 I 1
1 I 0
2 I 1
2 I 0
2 I 0
3 V 1
3 I 1
3 I 0
3 I 0
I would very much appreciate any help
Thank you
Nikos
don't want sort dataset ?
data have; input ID Status $; cards; 1 V 1 V 1 V 1 I 1 I 2 I 2 I 2 I 3 V 3 I 3 I 3 I ; run; data want(drop=n); set have; by id; array x{99999} $32 _temporary_ ; flag=0; if first.id then do;call missing(of x{*});n=0;end; if status not in x then do; flag=1;n+1;x{n}=status;end; run;
Xia Keshan
data want;
set have;
by id status notsorted;
if first.status then flag=1;
else flag=0;
run;
might work. You don't mention if the data would be sorted on ID or status.
Sorry..
It is sorted by ID
Thank you
If the solution provided by ballardw is OK, just ignore this question:
What results do you want fo this:
ID Status Flag
1 V 1
1 I 1
1 V ?
1 I ?
1 I 0
I believe that this kind of table is not the one that it's using. It seems that the dataset is sorted by id and status, and your example no.
proc sort data=xxxx
by id status;
run;
data xxxx;
set xxxx;
by id status;
if first.status then flag=1;
else flag=0;
run;
Yes, it is sorted by ID . STATUS is not sorted . Just represents the status in time..i.e. VVVV until becomes I and subsequently remains I
Thank you
Hi,
It should be
1 V
1 V
1 I
1 I
V's are in succession until I occurs which continues as I unti the end of the by group
Thank you
don't want sort dataset ?
data have; input ID Status $; cards; 1 V 1 V 1 V 1 I 1 I 2 I 2 I 2 I 3 V 3 I 3 I 3 I ; run; data want(drop=n); set have; by id; array x{99999} $32 _temporary_ ; flag=0; if first.id then do;call missing(of x{*});n=0;end; if status not in x then do; flag=1;n+1;x{n}=status;end; run;
Xia Keshan
Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.