pyll
pyll

Reputation: 1764

Proc Transpose SAS: Transpose and Rename Variables

I have a proc transpose question. The code I have isn't quite giving me what I want.

data have;
    input name $ x y z;
cards;
jon 1 85 1300
jon 2 90 2000
jon 3 95 1800
hal 1 70 1500
hal 2 78 4000
hal 3 83 3000
;
run;

proc sort data=have;
    by name;
run;

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

I want to end up with a dataset that looks like this:

name  y1  y2  y3  z1    z2    z3
hal   70  78  83  1500  4000  3000
jon   85  90  95  1300  2000  1800

The main thing here is being able to get one line per name. Secondary is the ability to use the x variable to rename the others (y1, y2, y3, etc). I know the first part is probably easy. Thoughts?

Thanks.

Upvotes: 2

Views: 7541

Answers (1)

Joe
Joe

Reputation: 63424

You need to make it in the form (by variables) (variable name) (variable value)

In this case that means six rows per person, with name varname value.

You can do this easily:

data have_pret;
 set have;
 varname=cats('y',x);
 value=y;
 output;
 varname=cats('z',x);
 value=z;
 output;
 keep name value varname;
run;

Then you can run proc transpose:

proc transpose data=have_pret out=want;
 by name;
 id varname;
 var value;
run;

Upvotes: 5

Related Questions