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 and %towide        

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;

run;

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

by first last age;

id type;

var A B C;

run;

proc print data=mixed_left;

title 'mixed_left';

run;

reshaped data

Option 2: Reshape using SAS arrays

data mixed_left1;

set mixed_right;

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

if Type='Veggie' then do;

    do temp=1 to 3;

            Veggie=aVeggie(temp);

            output;

    end;

end;

drop A B C;

run;

proc sort data=mixed_left1;

by temp;

run;

data mixed_left2;

set mixed_right;

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

if Type='Fruit' then do;

    do temp=1 to 3;

            Fruit=aFruit(temp);

            output;

    end;

end;

drop A B C;

run;

proc sort data=mixed_left2;

by temp;

run;

data mixed_left;

merge mixed_left1 mixed_left2;

by temp;

if temp=1 then Group='A';

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

else Group='C';

drop temp type;

run;

proc print data=mixed_left;

title 'mixed_left';

run;

reshaped data

Option 3: Reshape using macro %tolong and %towide

* rename variables to have numeric suffixes;

data mixed_right_recode;

set mixed_right;

rename A=plant1 B=plant2 C=plant3;

if type='Veggie' then type=1;

else type=2;

run;

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

%towide(mixed_left_temp,mixed_left,first last age key,type,1,2,Plant, types=C,lengths=20);

data mixed_left;

set mixed_left;

rename plant1=Veggie plant2=Fruit;

if key=1 then Group='A';

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

else Group='C';

drop key;

run;

proc print data=mixed_left;

title 'mixed_left';

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