Erica Jh Lee
Erica Jh Lee

Reputation: 121

SAS Data formatting (reverse proc transpose?)

    Q1  Q2  Q3   Q4
A   1   2   3   4
B   1   2   3   4
C   1   2   3   
D   1   2       
E   1   2   3   4

I have data like above and would like to import and reshape the data into what it looks like below:

    Qtr var1
A   Q1  1
A   Q2  2
A   Q3  3
A   Q4  4
B   Q1  1
B   Q2  2
B   Q3  3
B   Q4  4
C   Q1  1
C   Q2  2
C   Q3  3
D   Q1  1
D   Q2  2
E   Q1  1
E   Q2  2
E   Q3  3
E   Q4  4

It seems like a 'proc transpose' but it's reversed.

Please help!

Upvotes: 0

Views: 4509

Answers (2)

Longfish
Longfish

Reputation: 7602

Why not stick with PROC TRANSPOSE. It can do wide to long as well as long to wide.

data have;
input ID $ Q1  Q2  Q3   Q4;
datalines;
A   1   2   3   4
B   1   2   3   4
C   1   2   3   .
D   1   2   .   . 
E   1   2   3   4
;
run;

proc transpose data=have out=want name=Qtr;
by ID;
run;

Upvotes: 3

Joe
Joe

Reputation: 63434

The basic concept is the data step array.

data want;
set have;
array qs q1-q4;
do _t = 1 to dim(qs);
  quarter=vname(qs[_t]);
  var1=qs[_t];
  output;
end;
drop _t;
run;

Upvotes: 1

Related Questions