Making subsets using call execute

From sasCommunity

Jump to: navigation, search

2008-Jul-08 RJF2 deprecated, see 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 subsets, using call execute;

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

Stmnt = 'DATA ';
link CallExec;

do until(EndoFile);
   set Values end = EndoFile;
   %*for numerics: Stmnt = cats('_',&Var.);
   %*for num-real: need to translate ;
   %*from decimal dot to underline ;
   Stmnt = &Var.;
   link CallExec;
   end;

do Stmnt = ';do until(EndoFile);'
         , "set &Data. end = EndoFile;"
         , 'select(&Var.);';
   link CallExec;
   end;

EndoFile = 0;

do until(EndoFile);
   set Values end = EndoFile;
   %*char       when ("F")  output F;
   %*num-int    when ( 1 )  output _1;
   %*num-real   when ( 1.2) output _1_2;
   Stmnt = cat('when ("'   , &Var.
              ,'") output ', &Var., ';');
   link CallExec;
   end;

do Stmnt = 'otherwise;end;%*select;'
         , 'end;%*do until(EndoFile);stop;';
   link CallExec;
   end;

stop;

return; CallExec:
if   Testing then putlog Stmnt;
else        call execute(Stmnt);
return;
run;
run;
/*expected output: log ***********
NOTE: CALL EXECUTE generated line.
1         + DATA
2         + F
3         + M
4         + ;do until(EndoFile);
5         + set sashelp.class end = EndoFile;
6         + select(sex);
7         + when ("F") output F;
8         + when ("M") output M;
9         + otherwise;end;
10        + end;stop;
59         run;
NOTE: There were 17 observations read from the data set
      SASHELP.CLASS.
NOTE: The data set WORK.F has 9 observations and 5 variables.
NOTE: The data set WORK.M has 8 observations and 5 variables.
/***********************************************************/

--macro maven == the radical programmer 10:13, 8 July 2008 (EDT)

  • This page was last modified 15:21:03, 2007-04-30.
    • This page has been accessed 200 times as of 2008-Jun-08
Personal tools