Download mixedR.csv 

(see “CSV format” under “Files used in the examples”)

mixedR.csv preview

*To import the data;

proc import datafile = 'C:/mixedR.csv' out = mixed_right replace;

getnames = yes;

run;

Option 1: Reshape using proc transpose        

Option 2: Reshape using SAS arrays        

Option 3: Reshape using macro %tolong        

Option 1: Reshape using proc transpose

* Datasets MUST be sorted on ID variable prior to reshaping;

proc sort data=mixed_right;

by first last age type;

run;

proc transpose data=mixed_right out=long name=Group;

by first last age type;

var A B C;

run;

proc print data=long(rename=col1=Plant);

title 'long';

run;

reshaped data

Option 2: Reshape using SAS arrays

data long;

set mixed_right;

array aplant(1:3) $ A B C;

do temp=1 to 3;

    Plant=aplant(temp);

    output;

end;

drop A B C;

run;

data long;

set long;

if temp=1 then Group='A';

else if temp=2 then Group='B';

else Group='C';

drop temp;

run;

proc print data=long;

title 'long';

run;

reshaped data

Option 3: Reshape using macro %tolong

* rename variables to have numeric suffixes;

data mixed_right_rename;

set mixed_right;

rename A=plant1 B=plant2 C=plant3;

run;

%tolong(mixed_right_rename,long,first last age type,key,1,3,Plant, types=C,lengths=20);

data long;

set long;

if key=1 then Group='A';

else if key=2 then Group='B';

else Group='C';

drop key;

run;

proc print data=long;

title 'long';

run;

reshaped data

Functions referenced:

proc import

proc sort

proc transpose

data step: array

data step: merge

%towide

%tolong

proc summary

proc print

--- The End ---