Making subsets using call execute of macro

From sasCommunity
Jump to: navigation, search

Author: Ronald_J._Fehd

%*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
         );
%let Testing = &Testing or %sysfunc(getoption(mprint)) eq MPRINT;
%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 makesubs;
 
%*task 3: make macro calls using call execute;
 
DATA _Null_;
     attrib Stmnt   length =$132
            Testing length =   4; %*integer;
     retain Testing &Testing;
 
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)

References

See also

-- created by User:Rjf2 14:16, 08 July 2008 (EDT)

--Ronald_J._Fehd macro.maven == the radical programmer