Download mixedL.csv 

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

mixedL.csv preview

*To import the data;

proc import datafile = 'C:/mixedL.csv' out = mixed_left 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_left;

by first last age group;

run;

proc transpose data=mixed_left out=long name=Type;

by first last age group;

var Veggie Fruit;

run;

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

title 'long';

run;

reshaped data

Option 2: Reshape using SAS arrays

data long;

set mixed_left;

array aplant(1:2) $ Veggie Fruit;

do temp=1 to 2;

    Plant=aplant(temp);

    output;

end;

drop Veggie Fruit;

run;

data long;

set long;

if temp=1 then Type='Veggie';

else Type='Fruit';

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

set mixed_left;

rename veggie=plant1 fruit=plant2;

run;

%tolong(mixed_left_rename,long,first last age group,key,1,2,Plant, types=C,lengths=20);

data long;

set long;

if key=1 then Type='Veggie';

else Type='Fruit';

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