BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
kz_
Quartz | Level 8 kz_
Quartz | Level 8

I would like to transpose the 'have' table so that there is one row per ID. I would like to order the events by date, so that 'type1' corresponds to the type of event that happened first and 'type2' to the type of event that happened second, etc. I don't necessarily know how many rows there are per ID. 

 

Thank you for your help! I still find proc transpose to be confusing. 

 

data have;
  input id type $ (date) (:yymmdd.) amount;
  format date yymmdd10.;
datalines;                      
1 A 2019/01/01 100
1 B 2019/10/01 200 
1 A 2018/10/01 200 
2 C 2019/08/01 150
2 A 2020/08/08 250
2 C 2021/08/08 200 
2 B 2022/08/08 250
;

data want; 
input id type1 $ (date1) (:yymmdd.) amount1 type2 $ (date2) (:yymmdd.) amount2 type3 $ (date3) (:yymmdd.) amount3 type4 $ (date4) (:yymmdd.) amount4;
format date1 date2 date3 date4 yymmdd10.;
datalines; 
1 A 2018/10/01 200 A 2019/01/01 100 B 2019/10/01 200 . . . 
2 C 2019/08/01 150 A 2020/08/08 250 C 2021/08/08 200 B 2022/08/08 250
; 
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

That is not what PROC TRANSPOSE is designed to handle.

 

Either write your own data step, probably using ARRAYs.

 

Or use PROC SUMMARY with IDGROUP.  https://support.sas.com/resources/papers/proceedings10/102-2010.pdf

You need to tell PROC SUMMARY how many copies of the variables you want to create.

You could just hard code some maximum value, like 4, in this case.

data have;
  input id type $ date :yymmdd. amount;
  format date yymmdd10.;
datalines;                      
1 A 2019/01/01 100
1 B 2019/10/01 200 
1 A 2018/10/01 200 
2 C 2019/08/01 150
2 A 2020/08/08 250
2 C 2021/08/08 200 
2 B 2022/08/08 250
;

%let max=4 ;

proc summary data=have;
  by id;
  output out=want idgroup(out[&max] (type date amount)=);
run;

Result:

Tom_0-1661541126791.png

If you need to count the maximum number of copies then you can add an SQL step before the PROC SUMMARY step.

proc sql noprint;
select max(n) into :max trimmed
  from (select id,count(*) as n from have group by id)
;
quit;

View solution in original post

2 REPLIES 2
Tom
Super User Tom
Super User

That is not what PROC TRANSPOSE is designed to handle.

 

Either write your own data step, probably using ARRAYs.

 

Or use PROC SUMMARY with IDGROUP.  https://support.sas.com/resources/papers/proceedings10/102-2010.pdf

You need to tell PROC SUMMARY how many copies of the variables you want to create.

You could just hard code some maximum value, like 4, in this case.

data have;
  input id type $ date :yymmdd. amount;
  format date yymmdd10.;
datalines;                      
1 A 2019/01/01 100
1 B 2019/10/01 200 
1 A 2018/10/01 200 
2 C 2019/08/01 150
2 A 2020/08/08 250
2 C 2021/08/08 200 
2 B 2022/08/08 250
;

%let max=4 ;

proc summary data=have;
  by id;
  output out=want idgroup(out[&max] (type date amount)=);
run;

Result:

Tom_0-1661541126791.png

If you need to count the maximum number of copies then you can add an SQL step before the PROC SUMMARY step.

proc sql noprint;
select max(n) into :max trimmed
  from (select id,count(*) as n from have group by id)
;
quit;
kz_
Quartz | Level 8 kz_
Quartz | Level 8
This is perfect! And so simple too. Thank you!

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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