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

Hello,

 

New SAS user here. I have a dataset with ID, date, and different diagnoses given on that date. I would like to transform this dataset from long to wide as seen in the example below. But I am having trouble using do-loop.

 

The first table is the dataset I have. The second table is what I want. Any help would be appreciated.  Thank you

 

IDDateDiagnosis1Diagnosis2Diagnosis3Diagnosis4Diagnosis5
11/20/19F123F124   
11/21/19F284F124F3  
11/22/19F156    
11/23/19F189    
22/1/19F98    
22/2/19F78    
33/1/19F56    
33/2/19F57    
33/3/19F10F11F12F13F14
13/20/19F17    
39/1/19F124    

 

 

 

ID Date1Diagnosis1Date2Diagnosis2Date3Diagnosis3Date4Diagnosis4Date5Diagnosis5Date6Diagnosis6Date7Diagnosis7Date8Diagnosis8
11/20/19F1231/20/19F1241/21/19F2841/21/19F1241/21/19F31/22/19F1561/23/19F1893/20/19F17
22/1/19F982/2/19F78            
33/1/19F563/2/19F573/3/19F103/3/19F113/3/19F123/3/19F133/3/19F149/1/19F124
1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

Check my paper - Merge Skill:

 

http://support.sas.com/resources/papers/proceedings15/2785-2015.pdf

 

data have;
infile cards expandtabs truncover;
input ID	(Date	Diagnosis1	Diagnosis2	Diagnosis3	Diagnosis4	Diagnosis5) ($);
cards;
1	1/20/19	F123	F124	 	 	 
1	1/21/19	F284	F124	F3	 	 
1	1/22/19	F156	 	 	 	 
1	1/23/19	F189	 	 	 	 
2	2/1/19	F98	 	 	 	 
2	2/2/19	F78	 	 	 	 
3	3/1/19	F56	 	 	 	 
3	3/2/19	F57	 	 	 	 
3	3/3/19	F10	F11	F12	F13	F14
1	3/20/19	F17	 	 	 	 
3	9/1/19	F124	 	
;
run;
proc transpose data=have out=temp(where=(col1 is not missing));
by id date notsorted;
var diag: ;
run; 
data temp1;
 set temp;
 by id;
 if first.id then n=0;
 n+1;
run;
proc freq data=temp1 noprint;
table n/out=n list ;
run;

data _null_;
 set n end=last;
 if _n_=1 then call execute('data want;merge ');
 call execute(catt('temp1(where=(n=',n,') rename=(date=date',n,' col1=diag',n,'))'));
 if last then call execute(';by id;drop _name_ n ;run;');
run;

View solution in original post

5 REPLIES 5
Reeza
Super User
Why do you want that? In general, it's much much easier to work with your data in SAS (and other languages) in a long format than a wide format. The wide format is conventional in SPSS and for doing statistical analysis, but not as useful for reporting or calculating overall metrics.
ballardw
Super User

Please describe exactly what you are going to do with that wide format data. It is sometimes awkward enough to deal with multiple related variables on a single date but by the time you get all of those dates mixed in, it gets much harder to work with.

 

 

analyst11
Calcite | Level 5

The goal is to display dates and diagnoses by observation in a table format. This is the preferred method for the client. 

Reeza
Super User
There's a macro on here (you can use the search feature above) called "A better way to flip" that will do this for you.

You may need to restructure your data set to a long overall format first though.
Ksharp
Super User

Check my paper - Merge Skill:

 

http://support.sas.com/resources/papers/proceedings15/2785-2015.pdf

 

data have;
infile cards expandtabs truncover;
input ID	(Date	Diagnosis1	Diagnosis2	Diagnosis3	Diagnosis4	Diagnosis5) ($);
cards;
1	1/20/19	F123	F124	 	 	 
1	1/21/19	F284	F124	F3	 	 
1	1/22/19	F156	 	 	 	 
1	1/23/19	F189	 	 	 	 
2	2/1/19	F98	 	 	 	 
2	2/2/19	F78	 	 	 	 
3	3/1/19	F56	 	 	 	 
3	3/2/19	F57	 	 	 	 
3	3/3/19	F10	F11	F12	F13	F14
1	3/20/19	F17	 	 	 	 
3	9/1/19	F124	 	
;
run;
proc transpose data=have out=temp(where=(col1 is not missing));
by id date notsorted;
var diag: ;
run; 
data temp1;
 set temp;
 by id;
 if first.id then n=0;
 n+1;
run;
proc freq data=temp1 noprint;
table n/out=n list ;
run;

data _null_;
 set n end=last;
 if _n_=1 then call execute('data want;merge ');
 call execute(catt('temp1(where=(n=',n,') rename=(date=date',n,' col1=diag',n,'))'));
 if last then call execute(';by id;drop _name_ n ;run;');
run;

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
  • 5 replies
  • 1446 views
  • 1 like
  • 4 in conversation