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/

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

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