BookmarkSubscribeRSS Feed
Smitha9
Fluorite | Level 6

Hi,

I have a Dataset

ID     A       B       C

1      10     11      12

2      20    21      22

3      30    31      32

 

I want the dataset same ID grouped like below :

ID   want

1     10

1     11

1     12

2     20

2     21

2    22

3    30

3    31

3    32

 

Can I do this in SAS? thanks

3 REPLIES 3
ballardw
Super User

Example:

data have;
  input ID     A       B       C;
datalines;
1      10     11      12
2      20    21      22
3      30    31      32
;

proc transpose data=have out=want (drop=_name_);
   by id;
   var a b c;
run;

Since this uses a by Variable by default your data has to be sorted by the ID variable.

You did not indicate what the name of the new variable would be. The default from the procedure is Col1.

Note: if you have repeats of values of the ID variable you  will get multiple Col after transpose. So if you have multiples of the same ID you need to show an example of that data what you expect the result to be for that example.

tarheel13
Rhodochrosite | Level 12

You can also reshape data with arrays. 

data wide;
input ID A B C;
datalines;
1 10 11 12
2 20 21 22
3 30 31 32
;
proc print;
run;

data long2;
    set wide;
    array items(3) A B C;
    
    do i=1 to dim(items);
        want=items[i];
        output;
    end;
    
    drop i A B C;
run;

https://stats.oarc.ucla.edu/sas/modules/reshaping-data-wide-to-long-using-a-data-step/

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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
  • 761 views
  • 0 likes
  • 4 in conversation