BookmarkSubscribeRSS Feed
lmg
Calcite | Level 5 lmg
Calcite | Level 5

I am trying to transpose a table without having a set column number.

 

My data looks like this but the columns across can be more or less (group and category columns always stay the same).

GroupCategory201931201932201933201934201935201936201937
Awk                    28                    27                    26                    26                    25                    28                    31
Mwk                    28                    27                    26                    26                    25                    28                    31
Otherwk                    28                    27                    26                    26                    25                    28                    31
Abw                    24                    24                    24                    24                    24                    24                    24
Mbw                    24                    24                    24                    24                    24                    24                    24

 

This is what i need (not fully complete, but general idea): 

I need the column name to be in the row and the value associate with it.

 

GroupCategory week  value 
Awk201931                    28
Awk201932                    27
Awk201933                    26
Awk201934                    26
Awk201935                    25
Awk201936                    28
Awk201937                    31
Mwk201931                    28
Mwk201932                    27
Mwk201933                    26
Mwk201934                    26
Mwk201935                    25
Mwk201936                    28
Mwk201937                    31
Mwk201931                    28
Mwk201932                    27
Mwk201933                    26
Mwk201934                    26
Mwk201935                    25
Mwk201936                    28
Mwk201937                    31

 

I can use the proc contents to get the number of columns, but cannot transpose it properly.

 

Any items would be appreciated. Thank you.

1 REPLY 1
ballardw
Super User

@lmg wrote:

I am trying to transpose a table without having a set column number.

 

My data looks like this but the columns across can be more or less (group and category columns always stay the same).

Group Category 201931 201932 201933 201934 201935 201936 201937
A wk                     28                     27                     26                     26                     25                     28                     31
M wk                     28                     27                     26                     26                     25                     28                     31
Other wk                     28                     27                     26                     26                     25                     28                     31
A bw                     24                     24                     24                     24                     24                     24                     24
M bw                     24                     24                     24                     24                     24                     24                     24

 

This is what i need (not fully complete, but general idea): 

I need the column name to be in the row and the value associate with it.

 

Group Category  week   value 
A wk 201931                     28
A wk 201932                     27
A wk 201933                     26
A wk 201934                     26
A wk 201935                     25
A wk 201936                     28
A wk 201937                     31
M wk 201931                     28
M wk 201932                     27
M wk 201933                     26
M wk 201934                     26
M wk 201935                     25
M wk 201936                     28
M wk 201937                     31
M wk 201931                     28
M wk 201932                     27
M wk 201933                     26
M wk 201934                     26
M wk 201935                     25
M wk 201936                     28
M wk 201937                     31

 

I can use the proc contents to get the number of columns, but cannot transpose it properly.

 

Any items would be appreciated. Thank you.


Normally the restriction on variable names means that you can't have a variable name start with a digit. So, what are the actual names of your variables?

 

But all of those other variables all take numeric values then

 

proc transpose data=have out=trans

        name=week prefix=value

;

    by group category;

    var  _numeric_;

run;

 

should work assuming your data is sorted by Group and Category.

 

If your "week" variables have a mix of numeric and character values for some reason then you will need to create a data set that has all of one type or the other. You can't have mixed data types in a single output column as shown.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 1 reply
  • 305 views
  • 0 likes
  • 2 in conversation