DATA Step, Macro, Functions and more

Selective Transposition

Accepted Solution Solved
Reply
Super Contributor
Posts: 413
Accepted Solution

Selective Transposition

[ Edited ]

Hi,

 

I have a data table in the following form:

 

Company Var year_2001 year_2002
A x 11 31
A y 12 32
A z 13 33
B x 21 41
B y 22 42

 

And I would like this table to be transformed into the following table:

 

Company year x y z
A year_2001 11 12 13
A year_2002 31 32 33
B year_2001 21 22 .
B year_2002 41 42 .

 

So here what I need is to transform the values of "Var" into columns, and to transform the "2001" and "2002" columns into a new column "Year".

 

Thanks!


Accepted Solutions
Solution
‎07-15-2017 06:16 PM
Contributor
Posts: 61

Re: Selective Transposition

[ Edited ]
data want;
  input company $ var $ year_2001 year_2002;
  datalines;
A x 11 31
A y 12 32
A z 13 33
B x 21 41
B y 22 42
;
run;

proc print data=want;
run;

proc transpose data=want 
               out=have
               name=year;
  by company;
  id var;
run;

proc print data=have;
run;

 

EDIT - realized after posting that I probably should have reversed the data set names.  :-)

View solution in original post


All Replies
PROC Star
Posts: 549

Re: Selective Transposition

2001 and 2002 are not valid SAS variable names Smiley Happy

Solution
‎07-15-2017 06:16 PM
Contributor
Posts: 61

Re: Selective Transposition

[ Edited ]
data want;
  input company $ var $ year_2001 year_2002;
  datalines;
A x 11 31
A y 12 32
A z 13 33
B x 21 41
B y 22 42
;
run;

proc print data=want;
run;

proc transpose data=want 
               out=have
               name=year;
  by company;
  id var;
run;

proc print data=have;
run;

 

EDIT - realized after posting that I probably should have reversed the data set names.  :-)

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 181 views
  • 3 likes
  • 3 in conversation