How to use PROC TRANSPOSE to get one record per BY group


Because of the structure of a data set, using the BY statement does not always guarantee that PROC TRANSPOSE will create an output data set containing one record per BY group. In some cases, it is necessary to use a BY variable that is unique for every record (if one does not already exist in the data set, use _N_), and then transpose the data set a second time. For example:

data test;

n=_n_;

input x y z;

datalines;

1 2 3

1 2 3

2 3 4

3 4 5

3 4 5

4 4 5

run;

/* In order to keep the implied ID variable _NAME_ */

/* from causing errors in the second transpose, */

/* eliminate it using the DROP= data set option */

proc transpose out=first(drop=_name_);

by n x; v

ar y z;

run;

proc print;

run;

proc transpose data=first out=second prefix=a;

by x;

var col1;

run;

proc print; 

run;