BookmarkSubscribeRSS Feed
markc
Obsidian | Level 7

Hello,

 

I am trying to create a new variable (called Payment_YearMonth) in each observation related to the value stored in YearMonth in each observation and store Amount as that variable's value.

 

For example:

 

HAVE:

 

YearMonth   Amount

201607        10.00

201704        20.00

201713        30.00

201607        40.00

201704        50.00

201713        60.00

201607        70.00

201704        80.00

201713        90.00

 

WANT:

 

YearMonth

payment_201607

payment_201704

payment_201713

201607

$                       10.00

 

 

201704

 

$                       20.00

 

201713

 

 

$                          30.00

201607

$                       40.00

 

 

201704

 

$                       50.00

 

201713

 

 

$                          60.00

201607

$                       70.00

 

 

201704

 

$                       80.00

 

201713

 

 

$                          90.00

 

Any advice will be quickly responded to.

 

Thank you!

4 REPLIES 4
Tom
Super User Tom
Super User

That looks like a useless format, but it is simple to get with PROC TRANSPOSE.

proc transpose data=have  out=want prefix=payment_;
  by yearmonth;
  id yearmonth;
  var amount;
run;
markc
Obsidian | Level 7

Thanks so much, I tried sorting the data first by the BY variable but SAS is saying the ID value occurs twice in the same BY group.   Do you know why that might be?  Kind regards

Tom
Super User Tom
Super User
Only you know your data. You might need to add a new variable to use for the BY grouping.
In your original example it looked like you wanted one output observation per input observation.
data for_transpose;
row+1;
set have;
run;
proc transpose data=for_transpose out=want;
by row;
...
Astounding
PROC Star

A slight adjustment is needed:

data have2;
set have;
rec = _n_;
run;

Then within PROC TRANSPOSE, change the BY statement:

by rec;

Do not change the ID statement.

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!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 647 views
  • 0 likes
  • 3 in conversation