Thursday, May 8, 2008

Create a variable having value of median in categories

To find the median values of the categories (e.g. to assess test of trend)


%macro hint(var1,var2);

proc means data=fret median;
class &var1;
var &var2;
OUTPUT OUT=&var2 MEDIAN= ;
run;

data null;
set &var2;
call symputx("&var2.m"||put(_n_,1.),&var2,'g');
run;
%put &var2.m;
%put _user_;
%mend;
**depending on the number of categories (n), you would have n+1 macro variables.
**********run like this *************;
%hint(_alcoc,alc) /***_alcoc has 4 categories **/

/*** this would create macro variables as &alcm2 - &alcm(n+1)***/

data new;
set fret;

if _alcoc =. then alcm = .;
else if _alcoc =1 then alcm=&alcm2; /**note this is &alcm2 ***/
else if _alcoc =2 then alcm=&alcm3;
else if _alcoc= 3 then alcm=&alcm4;
else if _alcoc = 4 then alcm=&alcm5;