BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
tmcwill
Fluorite | Level 6

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! 

 

1 ACCEPTED SOLUTION

Accepted Solutions
unison
Lapis Lazuli | Level 10

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

-unison

View solution in original post

2 REPLIES 2
unison
Lapis Lazuli | Level 10

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

-unison
mkeintz
PROC Star

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;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

sas-innovate-white.png

Missed SAS Innovate in Orlando?

Catch the best of SAS Innovate 2025 — anytime, anywhere. Stream powerful keynotes, real-world demos, and game-changing insights from the world’s leading data and AI minds.

 

Register now

How to connect to databases in SAS Viya

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.

Discussion stats
  • 2 replies
  • 816 views
  • 2 likes
  • 3 in conversation