Your example works if all obs has values on every var, try this:
data test;
length b a g d 8;
b = 1; a = 2; d=10; g = 30;output;
b = 2; g = 31;output;
b = 3; a = 9; d=7; g = 32;output;
b = 4; g = 33;output;
run;
data test2;
retain a b d g;
set test;
run;
proc print data = test2;run;
retain is used for retaining vars....I think...:-)
I made a small macro that i hope works , even with work-tables...;-)
%macro sortvar(ds);
proc contents data = &ds out = metadata;run;
proc sort data = metadata; by name;run;
data _null_;
retain counter 0;
set metadata end=last;
counter+1;
call symput('VAR'||compress(put(counter,8.)),trim(name));
if type eq 1 then call symput('TYP'||compress(put(counter,8.)),trim(''));
else call symput('TYP'||compress(put(counter,8.)),trim('$'));
call symput('LEN'||compress(put(counter,8.)),trim(length));
if last then do;
call symput('NOVARS',compress(put(counter,8.)));
end;
run;
data &ds;
%DO I = 1 %TO &NOVARS;
length &&VAR&I &&TYP&I&&LEN&I;
%END;
set &ds;
run;
%mend;
/* example of usage, replace sashelp.class with your own dataset */
%sortvar(sashelp.prdsale);
//Fredrik