I'm trying to work with the bladder dataset obtained here http://support.sas.com/documentation/cdl/en/statug/66859/HTML/default/viewer.htm#statug_phreg_exampl...
As provided, the data are repeated measures with 4 observations for each subject. Each observation has a visit number from 1-4 and a binary status variable. I would like to have each subject only appear once with either the first visit where status=1 or if a subject never has status=1, then the last visit (visit=4). I'm thinking maybe I need to somehow use a do loop iterating over each ID, but I'm not sure how to implement. Any help is greatly appreciated!
There is probably a cleaner way to do this but I think this is what you're looking for!
data want;
set have(where=(status=1) in=a) have(in=b);
by id;
retain _outobs 0;
if not (_outobs) and ((a and first.id) or (b and last.id)) then
do;
output;
_outobs=1;
end;
if last.id then
_outobs=0;
drop _:;
run;
Checking result:
proc sort data=have(where=(status=1))
nodupkey out=check_sts;
by id;
run;
data check;
set check_sts have(where=(visit=4));
run;
proc sort data=check
nodupkey out=check_out;
by id;
run;
proc compare base=want compare=check_out; run;
-unison
There is probably a cleaner way to do this but I think this is what you're looking for!
data want;
set have(where=(status=1) in=a) have(in=b);
by id;
retain _outobs 0;
if not (_outobs) and ((a and first.id) or (b and last.id)) then
do;
output;
_outobs=1;
end;
if last.id then
_outobs=0;
drop _:;
run;
Checking result:
proc sort data=have(where=(status=1))
nodupkey out=check_sts;
by id;
run;
data check;
set check_sts have(where=(visit=4));
run;
proc sort data=check
nodupkey out=check_out;
by id;
run;
proc compare base=want compare=check_out; run;
-unison
Although the interleaving of two streams of HAVE data works in this case, this is not really a situation that demands two streams. Here's an approach that uses a single stream. The logic, of course, is effectively identical to the solution provided by @unison :
data want (drop=n_status1);
set have;
by id;
if first.id then n_status1=0;
n_status1+(status=1);
if (n_status1=1 and status=1) or
(n_status1=0 and last.id=1) then output;
run;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.
Find more tutorials on the SAS Users YouTube channel.