Making subsets using call execute of macro

From sasCommunity

Jump to: navigation, search

2008-Jul-08 RJF2 see also http://www.sascommunity.org/wiki/Split_Data_into_Subsets

 
%*Split Data into subsets
  based on value of one character variable;

%Let Data = sashelp.class;
%Let Var  = sex;%*character variable;

%*task 1: make unique list;
PROC Sort data = &Data.(keep = &Var.)
          out  = Values
                 nodupkey;
          by     &Var. ;


%*task 2: make item processing macro;

%Macro MakeSubs(data  =
               ,var   =
               ,value =
               ,testing = 1
               );
%if &Testing. %then %put _local_;

%*note: insert your own code here;
%*e.g.: proc export or write .html or .xls, etc.;

DATA &Value.;
do until(EndoFile);
   set &Data.
      (where = (&Var. eq "&Value."))
       end   = EndoFile;
   output;
   end;
stop;
run;%Mend;


%*task 3: make macro calls using call execute;

DATA   _Null_;
attrib Stmnt   length =$132
       Testing length =   4; %*integer;
retain Testing 1;%*testing  ;
*retain Testing 0;%*production;

do  until(EndoFile);
set Values end = EndoFile;
Stmnt = cats('%MakeSubs('
            ,  "data=&Data."
            ,  ",var=&Var."
            ,",value=",&Var.
            ,')');
if   Testing then putlog Stmnt;
else call execute(cats('%nrstr(',Stmnt,')'));
end;
stop;
run;
run;

expected log:

 
%MakeSubs(data=sashelp.class,var=sex,value=F)
%MakeSubs(data=sashelp.class,var=sex,value=M)
  • This page was last modified 15:35:34, 2007-04-30.
    • This page has been accessed 154 times as of 2008-Jul-08
Personal tools