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
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.