How can I ensure that the variables in the output data set that is created by the TRANSPOSE procedure retain their formats?


To ensure that the variables in the output data set that is created by the TRANSPOSE procedure retain their formats, ensure that the same format is applied to all of the variables in the input data set, as in Example 1.

/* Example 1 */
data test;
 input ID $  X   Y;
cards;
A1    1   1
A2    1   5
A3    5   1
A4    5   5
;
run;

proc format ;
 value ynfmt 1='yes' 5='no';
 value mffmt 1='female' 5='male';
run;

proc transpose data=test out=out name=Name;
 id id;
 var x y;
 format x y ynfmt.;
 run;

proc print data=out noobs; run;

/* RESULTS of Example 1 */

Name    A1     A2     A3     A4

 X      yes    yes    no     no
 Y      yes    no     yes    no

However, because any variable in a SAS data set can have only one format associated with it, when you TRANSPOSE a data set that contains variables formatted with multiple formats, none of those formats are associated with the variables in the output data set. In Example 2, because there can be only one format for variable A1, PROC TRANSPOSE will not choose between the two formats that are specified for variables Y and X. However, the code in Example 2 shows how you can produce the desired output by changing the values of X and Y and then transposing those the new values.

/* Example 2 */

data test;
 input ID $  X   Y;
cards;
A1    1   1
A2    1   5
A3    5   1
A4    5   5
;
run;

proc format ;
 value ynfmt 1='yes' 5='no';
 value mffmt 1='female' 5='male';
run;

data new;
 set test;
 newx=put(x, ynfmt.);
 newy=put(y, mffmt.);
 run;

proc transpose data=new out=new name=Name;
 id id;
 var newx newy;
run;

proc print noobs; run;

/* RESULTS of Example 2 */

Name    A1         A2     A3         A4

newx    yes       yes     no        no
newy    female    male    female    male