BookmarkSubscribeRSS Feed
current_thing
Obsidian | Level 7

Hello, I'm trying to change a table from narrow to wide. What I have:

Col1Col2Col3
afoo_aval_1
afoo_bval_2
bfoo_aval_1
bfoo_bval_2

 

What I want:

Col1Col2_1Col2_2Col3_1Col3_2
afoo_afoo_bval_1val_2
bfoo_afoo_bval_1val_2

 

I want to do it programmatically, since the unique values in Col2 and Col3 vary. Your help is appreciated!

6 REPLIES 6
AMSAS
SAS Super FREQ

Take a look at PROC TRANSPOSE 

 

Transpose Example 

Astounding
PROC Star
A few related questions:

Will you ever have more than two rows for a single COL1 value?

Which variables are character and which are numeric?

Why transform the data? In the majority of cases programming on narrow data is easier and more flexible.
current_thing
Obsidian | Level 7

Thanks. Yes, for sure the actual data set will have many unique values in Col2, Col3, Coln for each distinct value in Col1. And the reason for the transformation is that I need to join 3 tables, and what's happening is the final table ends up being ~450m rows because the join fields occur multiples times. Does that make sense? Which is why I need to convert from narrow to wide

andreas_lds
Jade | Level 19

Please post examples of all datasets in usable form and show the expected result of merging those datasets.

Astounding
PROC Star

Yes, PROC TRANSPOSE as recommended by @AMSAS can help.  But keep in mind that the resulting data set could easily be bigger than the original data set.  The wider data set would contain fewer rows, but many more columns.  Take a look at the sample data you posted, where you start with 12 data elements and end up with 10.  That's a small reduction.  But keep in mind that you can have a different number of rows in the narrow data set for each COL1.  So in your sample data, if there were only one COL1 row for "a" you would be starting with 9 data elements in total, but ending up with 10 (two of which would have missing values).  The number of variables in the wide data set will match the COL1 value with the largest number of rows, creating missing values for other COL1 values that don't have so many rows.

 

Still,  you could end up saving space depending on the number of variables in the other tables that you are joining.

Tom
Super User Tom
Super User

If you want to simultaneously transpose multiple variables then you probably want to use the IDGROUP feature of PROC SUMMARY.  You will need to tell it in advance the number of new variables to create (the maximum number of observations per ID group).

 

Search: https://www.google.com/search?q=%40sas.com+idgroup+transpose

 

Number one hit is the article by @data_null__ 

 

https://support.sas.com/resources/papers/proceedings10/102-2010.pdf

 

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!

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
  • 6 replies
  • 1003 views
  • 1 like
  • 5 in conversation