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

 

Early bird rate extended! Save $200 when you sign up by March 31.

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 4 replies
  • 1733 views
  • 0 likes
  • 2 in conversation