BookmarkSubscribeRSS Feed
sunnyday
Calcite | Level 5

Hi Experts!

I'm new to this platform as well as SAS. Please help me out. I was trying to do proc transpose of a dataset with multiple variables and I wish to get all of them in columns and also the year sorting be done in ascending order. Below is the example.

 

data have;
input Id FirstName LastName year sales $ debt $;
cards;
ab1 Sam Tim 2020 100 0
ab1 Sam Tim 2021 50 70
cd2 Long Phelp 2019 225 10
ef3 Pam Trace 2022 15 15
;

 

Ran this query - 

 

proc transpose data=have out=want;
by Id FirstName LastName;
id year;
var sales debt;
run;

 

Got this - 

IDFirstnameLastname_name_2020202120222019
ab1SamTimsales10050  
ab1SamTimdebt 70  
cd2LongPhelpsales   225
cd2LongPhelpdebt   10
ef3PamTracesales  15 
ef3PamTracedebt  15 

 

Want this - 

 

   sales   debt   
IDFirstnameLastname20192020202120222019202020212022
ab1SamTim 10050   70 
cd2LongPhelp225   10   
ef3PamTrace   15   15

 

 

Please let me know how can this be done.

 

Thank you!

3 REPLIES 3
PaigeMiller
Diamond | Level 26

You can't have PROC TRANSPOSE create a SAS data set with the layout that you described where "Sales" is above 2019. There is no such thing as a variable in a data set that has a name that has two parts, one above the other.

 

You can get this via PROC REPORT.

 

data have;
input Id $ FirstName $ LastName $ year sales debt;
cards;
ab1 Sam Tim 2020 100 0
ab1 Sam Tim 2021 50 70
cd2 Long Phelp 2019 225 10
ef3 Pam Trace 2022 15 15
;
proc report data=have;
    columns ("First Name" firstname) ("Last Name" lastname) ("Sales" year,sales) ("Debt" year,debt);
    define firstname/group ' ' order=data;
    define lastname/group ' ' order=data;
    define sales/sum ' ';
    define year/across ' ';
    define debt/sum ' ';
run;

 

Please test your DATA step code before providing it to make sure it works. Testing code to make sure it works is a good thing! Your code did not work. I had to modify it to get it to work, please next time you get it to work before posting it.

--
Paige Miller
PaigeMiller
Diamond | Level 26

Slight improvement to my earlier code:

 

data have;
length fullname $ 40;
input Id $ FirstName $ LastName $ year sales debt;
fullname=catx(', ',lastname,firstname);
cards;
ab1 Sam Tim 2020 100 0
ab1 Sam Tim 2021 50 70
cd2 Long Phelp 2019 225 10
ef3 Pam Trace 2022 15 15
;
proc report data=have;
    columns ("Name" fullname) ("Sales" year,sales) ("Debt" year,debt);
    define fullname/group ' ' order=data;
    define sales/sum ' ';
    define year/across ' ';
    define debt/sum ' ';
run;
--
Paige Miller
sunnyday
Calcite | Level 5
Thanks for the quick reply! I'll take care of the Code from next time.

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 3 replies
  • 820 views
  • 0 likes
  • 2 in conversation