BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
leonzheng
Obsidian | Level 7
I have output looks like this:

year        row        meat          col01         col02

2017        1           beef             a1             a2

2017        1           chicken        b1             b2

2017        2           beef             a3             a4

2017        2           chicken        b3             b4

2018        1           beef             a5             a6

2018        1           chicken        b5             b6

2018        2           beef             a7             a8

2018        2           chicken        b7             b8

 

I want to change the format like below:

 

year        row       col        beef         chicken

2017       1           01         a1             b1

2017       1           02         a2             b2

2017       2           01         a3             b3

2017       2           02         a4             b4

2018       1           01         a5             b5

2018       1           02         a6             b6

2018       2           01         a7             b7

2018       2           02         a8             b8

 

Last week I asked a similar question, now I know how to do transpose without column 'year'. But with column 'year, format will look wired. Please help!

 

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
snoopy369
Barite | Level 11

Adding YEAR to the by statement should work fine for you, no?

 


data have;
input year row meat $col01 $ col02 $;
datalines;
2017        1           beef             a1             a2
2017        1           chicken        b1             b2
2017        2           beef             a3             a4
2017        2           chicken        b3             b4
2018        1           beef             a5             a6
2018        1           chicken        b5             b6
2018        2           beef             a7             a8
2018        2           chicken        b7             b8
;;;;
run;

proc transpose data=have out=want;
  by year row;
  id meat;
  var col01 col02;
run;

View solution in original post

4 REPLIES 4
snoopy369
Barite | Level 11

Adding YEAR to the by statement should work fine for you, no?

 


data have;
input year row meat $col01 $ col02 $;
datalines;
2017        1           beef             a1             a2
2017        1           chicken        b1             b2
2017        2           beef             a3             a4
2017        2           chicken        b3             b4
2018        1           beef             a5             a6
2018        1           chicken        b5             b6
2018        2           beef             a7             a8
2018        2           chicken        b7             b8
;;;;
run;

proc transpose data=have out=want;
  by year row;
  id meat;
  var col01 col02;
run;
leonzheng
Obsidian | Level 7

It doesn't work,

If I do this column 'row' will be transposed, it will look something like this:

year                      beef         chicken       beef         chicken

2017      row          1              1                 2              2

2017       1             a1            b1               a2            b2

2017       2             a3            b3               a4            b4

2018      row          1              1                 2              2

.

.

.

 

snoopy369
Barite | Level 11
You also need ROW on the by statement. Run the code in my example.
leonzheng
Obsidian | Level 7

you are right, I missed the ROW...

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 4 replies
  • 1063 views
  • 0 likes
  • 2 in conversation