BookmarkSubscribeRSS Feed
Abby7
Calcite | Level 5

Needing some help with converting a data set with multiple rows for the same data set to multiple columns.

 

An example of the current data set is:

ID NumberType
1A
1AA
2A
2AAA
2AAAAAAA
3AAAAAAAAAA
4AA
4A
5A
5AAAAAAA
5AAAA

 

An example of what the data set needs to look like is:

ID NumberTypeType 2Type 3 Type 4Type 5
1AAA   
2AAAAAAAAAAAAAAAAAAAAA 
3AAAAAAAAAA    
4AAA   
5AAAAAAAAAAAA  

 

 

I have tried a proc transpose however that has resulted in an output like this:

ID NumberType AType 2Type 3 Type 4Type 5Type 6Type 7Type 8Type 9
11        
21 1   1 1
3        1
411       
51  1  1  

 

Thank you for your help 

 

2 REPLIES 2
qoit
Pyrite | Level 9

Hope the below helps:

 

data have;
	infile datalines missover;
	input id type$;
	datalines;
1 A
1 AA
2 A
2 AAA
2 AAAAAAA
3 AAAAAAAAAA
4 AA
4 A
5 A
5 AAAAAAA
5 AAAA
;
run;

data have1;
	set have;
	by id;
	retain cnt 0;

	if first.id then
		cnt = 0;
	cnt + 1;
run;

proc sql;
	select max(id)
		into :max_id TRIMMED
			from have1;
quit;

%put &=max_id;

data want (keep=id type1 - type&max_id.);
	set have1;
	by id;
	retain type1 - type&max_id.;
	array types{&max_id.} $30. type1 - type&max_id.;

	if first.id then
		do i = 1 to &max_id.;
			types{i} = .;
		end;

	types{cnt} = type;

	if last.id then
		output;
run;
mkeintz
PROC Star

It looks to me like the proc transpose produces what you want:

 

data have;
	infile datalines missover;
	input id type$;
	datalines;
1 A
1 AA
2 A
2 AAA
2 AAAAAAA
3 AAAAAAAAAA
4 AA
4 A
5 A
5 AAAAAAA
5 AAAA
run;
proc transpose data=have out=want (drop=_name_) prefix=type_ ;
  by id;
  var type;
run;
proc print;run;

The result is:

Obs    id    type_1      type_2     type_3
 1      1    A           AA
 2      2    A           AAA        AAAAAAA
 3      3    AAAAAAAA
 4      4    AA          A
 5      5    A           AAAAAAA    AAAA

 

--------------------------
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

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

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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