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

Hello,

Can anyone advice me how to transpose the following data?

I have this data:

data have;
input Claim_ID CODE $5;
datelines;
10001       90651 
10001       90670
10001       90680
10001       90698
10001       91170
10002       92405
10002       90710
10003       90715
;

I want to have this data;

Claim_ID  Code_1      Code_2     Code_3    Code_4     Code_5

10001     90651      90670    90680     90698    91170  

10002     92405     90710          

10003     90715
;

The number of Code can be more than five per claim but I want to consider the maximum number of distinct Code by claim as my columns.

Thank you in advance!

 

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

Hi @Bikila   Are you asking for this?

 


data have;
input Claim_ID CODE :$5.;
datalines;
10001       90651 
10001       90670 
10001       90680 
10001       90698 
10001       91170 
10002       92405 
10002       90710 
10003       90715
;
proc transpose data=have out=want(drop=_name_) prefix=CODE_;
by claim_id;
var code;
run;

View solution in original post

3 REPLIES 3
novinosrin
Tourmaline | Level 20

Hi @Bikila   Are you asking for this?

 


data have;
input Claim_ID CODE :$5.;
datalines;
10001       90651 
10001       90670 
10001       90680 
10001       90698 
10001       91170 
10002       92405 
10002       90710 
10003       90715
;
proc transpose data=have out=want(drop=_name_) prefix=CODE_;
by claim_id;
var code;
run;
ed_sas_member
Meteorite | Level 14

Hi @Bikila ,

 

Another way to achieve this using an array (alternatively to the solution posted by @novinosrin )

 


/* Put the max number of code_ variables in a macro variable &max_obs -> here it is 5 */

proc sql noprint;
	select max(frequency) into: max_obs from (select count(Claim_ID) as frequency from have group by Claim_ID);
run;

data want;
	set have;
	
	array code_(&max_obs) $;
	
	by claim_id;
	retain code_;
	
 	if first.claim_id then do;
 		counter=0;
 		call missing (of code_(*));
 	end;
 	counter+1;

	do i=1 to dim(code_);
		code_(counter) = code;
	end;
	
	if last.claim_ID then output;
	drop i code counter;
run;
Bikila
Fluorite | Level 6
Thank you! This is exactly how I wanted it.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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
  • 481 views
  • 2 likes
  • 3 in conversation