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

I have a data which looks like this-

 

Patient codeepisodecodecode2
P11NSS
P11SS
P11SS
P11NSS
P12NSNS
P12NSNS
P13SS
P21SS
P22NSS
P22SS

 

I have patient code , episode and code and want to create a variable like code2.

Code 2 should be S if any of the rows corresponding to an episode of a patient is S.  Like for P1 , episode 1 , since middle two rows are S all rows corresponding to P1, episode 1 should show S.

 

How do I write a code for this?

Would first. and last. come to use here.

 I am clueless.

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Could do something like:

proc sort data=have;
  by patient_code episode descending code;
run;
data want;
  set have;
  retain code2;
  by patient_code episode;
  if first.episode then code2=code;
run; 

It wouldn't be in your original order, but I am guessing there is other information for that (i.e. dates/times or something).  Oh, an please post test data in the form of a datastep in future, the above is not tested.

View solution in original post

3 REPLIES 3
Jagadishkatam
Amethyst | Level 16
data have;
input Patient $ 	episode	code$;
cards;
P1 1 NS	
P1 1 S	
P1 1 S	
P1 1 NS	
P1 2 NS	
P1 2 NS	
P1 3 S	
P2 1 S	
P2 2 NS	
P2 2 S	
;

proc sort data=have;
by patient episode code;
run;

data want;
do until(last.episode);
set have;
by patient episode code;
if last.episode then code2=code;
end;
do until(last.episode);
set have;
by patient episode code;
output;
end;
run;

Thanks,
Jag
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Could do something like:

proc sort data=have;
  by patient_code episode descending code;
run;
data want;
  set have;
  retain code2;
  by patient_code episode;
  if first.episode then code2=code;
run; 

It wouldn't be in your original order, but I am guessing there is other information for that (i.e. dates/times or something).  Oh, an please post test data in the form of a datastep in future, the above is not tested.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 657 views
  • 2 likes
  • 4 in conversation