Download long.csv 

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

long.csv preview

*To import the data;

proc import datafile = 'C:/long.csv' out = long dbms = csv replace;

getnames = yes;

run;

Option 1: Reshape using proc transpose        

Option 2: Reshape using SAS arrays        

Option 3: Reshape using macro %towide        

Option 4: Reshape using proc summary        

Option 1: Reshape using proc transpose

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

proc sort data=long;

by first last age type;

run;

proc transpose data=long out=mixed_right (drop=_name_);

by first last age type;

id group;

var plant;

run;

proc print data=mixed_right;

title 'mixed_right';

run;

reshaped data

Option 2: Reshape using SAS arrays

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

proc sort data=long;

by first last age type;

run;

data mixed_right;

set long;

by first last age type;

keep first last age type A B C;

retain A B C;

if group='A' then group=1;

else if group='B' then group=2;

else if group='C' then group=3;

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

if first.type then do;

    do i=1 to 3;

            aplant(i)='';

    end;

end;

aplant(group)=plant;

if last.type then output;

run;

proc print data=mixed_right;

title 'mixed_right';

run;

reshaped data

Option 3: Reshape using macro %towide

* recode the character time-varying variable to numeric variable;

data long_recode;

set long;

if group='A' then group=1;

else if group='B' then group=2;

else group=3;

run;

%towide(long_recode,mixed_right,first last age type,group,1,3,plant, types=C,lengths=20);

proc print data=mixed_right(rename=(plant1=A plant2=B plant3=C));

title 'mixed_right';

run;

reshaped data

Option 4: Reshape using proc summary

proc summary data=long nway;

class first last age type;

output out=mixed_right(drop=_:) idgroup(out[3](plant)=p);

run;

proc print data=mixed_right(rename=(p_1=A p_2=B p_3=C));

title 'mixed_right';

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 ---