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

Hi, 

 

I have a data set containing approx. 20,000 observations. Each ID represents a medical patient that had a medical exam. Based on the diagnosis, a code was assigned. One patient can have many codes and many exams at different date.

 

It looks like this:

iddatecode1code2code3
12010-04-1201.0101.02 
12011-02-2001.0101.02 
12012-08-2402.0501.0101.02
22012-08-1901.01  
22012-10-1001.0901.01 
22015-01-2701.0101.09 
32014-05-0203.02  
32014-12-1601.0103.02 
42010-03-2803.05  

 

 

I am asking for your help to create observation containing the code2 and code3 value. Basically, I want to tell SAS that "if the code2 variable is not empty, take what is in the code2 variable and put it a new observation with the related date."

iddatecode
12010-04-1201.01
12010-04-1201.02
12011-02-2001.01
12011-02-2001.02
12012-08-2402.05
12012-08-2401.01
12012-08-2401.02
22012-10-1001.09
22012-10-1001.01
22015-01-2701.01
22015-01-2701.09
32014-05-0203.02
32014-12-1601.01
32014-12-1603.02
42010-03-2803.05

 

Can anyone help me with that?

 

Thank you !

1 ACCEPTED SOLUTION

Accepted Solutions
kiranv_
Rhodochrosite | Level 12

something like this using proc transpose or 

 

data have;
infile datalines dlm='09'x missover;
input id	date:yymmdd10.	code1	code2	code3;
format date yymmdd10.;
datalines;
1	2010-04-12	01.01	01.02	 
1	2011-02-20	01.01	01.02	 
1	2012-08-24	02.05	01.01	01.02
2	2012-08-19	01.01	 	 
2	2012-10-10	01.09	01.01	 
2	2015-01-27	01.01	01.09	 
3	2014-05-02	03.02	 	 
3	2014-12-16	01.01	03.02	 
4	2010-03-28	03.05
;

proc sort data=have out=have1;
by id date;
run;





proc transpose data=have1 out=want(rename=(col1=code) drop =_name_ where=(code ne .));
by id date;
var code1 code2 code3;
run;

 or by array

 

 

data want (drop = code1 code2 code3 i where=(code ne .));
set have;
array co(*) code1 code2 code3;
do i= 1 to dim(co) ;
code= co(i);
output;
end;
run;



 

View solution in original post

2 REPLIES 2
kiranv_
Rhodochrosite | Level 12

something like this using proc transpose or 

 

data have;
infile datalines dlm='09'x missover;
input id	date:yymmdd10.	code1	code2	code3;
format date yymmdd10.;
datalines;
1	2010-04-12	01.01	01.02	 
1	2011-02-20	01.01	01.02	 
1	2012-08-24	02.05	01.01	01.02
2	2012-08-19	01.01	 	 
2	2012-10-10	01.09	01.01	 
2	2015-01-27	01.01	01.09	 
3	2014-05-02	03.02	 	 
3	2014-12-16	01.01	03.02	 
4	2010-03-28	03.05
;

proc sort data=have out=have1;
by id date;
run;





proc transpose data=have1 out=want(rename=(col1=code) drop =_name_ where=(code ne .));
by id date;
var code1 code2 code3;
run;

 or by array

 

 

data want (drop = code1 code2 code3 i where=(code ne .));
set have;
array co(*) code1 code2 code3;
do i= 1 to dim(co) ;
code= co(i);
output;
end;
run;



 

dera
Obsidian | Level 7
Thank you Kiranv_v! It works perfectly.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1955 views
  • 0 likes
  • 2 in conversation