Friday, February 1, 2013

SAS: Process six months of data at a time

Due to space constraints, I need to process six months of data at a time.  Here's how I will do it:

* How I will process six months of data at a time ;

* These will come from global constants file ;
%let GLBL_EARLIEST_DATE_TO_IMPORT = '01Jan2011'd;
%let GLBL_LATEST_DATE_TO_IMPORT = '01Jan2014'd;

* Show what we got ;
%put GLBL_EARLIEST_DATE_TO_IMPORT = %sysfunc(putn(&GLBL_EARLIEST_DATE_TO_IMPORT, yymmddn8.));
%put GLBL_LATEST_DATE_TO_IMPORT = %sysfunc(putn(&GLBL_LATEST_DATE_TO_IMPORT, yymmddn8.));

* Here is the start of the macro definition ;

%macro doit;

* Starting date is determined by global macro constant ;
%let FIRST_DAY = %sysevalf(&GLBL_EARLIEST_DATE_TO_IMPORT);
%put FIRST_DAY = %sysfunc(putn(&FIRST_DAY, yymmddn8.));

* Will count number of iterations. Count will be used in filename of temp output ;
%let ITERATIONS = 0;

%do %while (&FIRST_DAY <= %sysevalf(&GLBL_LATEST_DATE_TO_IMPORT));

%let ITERATIONS = %sysevalf(&ITERATIONS + 1);

* Last day for this iteration is the last day of the month five months from now ;
%let LAST_DAY = %sysfunc(intnx(month, &FIRST_DAY, 5, e));

* Do not go past the indicated last date to import ;
%if (&LAST_DAY > %sysevalf(&GLBL_LATEST_DATE_TO_IMPORT)) %then %do;
    %let LAST_DAY = %sysevalf(&GLBL_LATEST_DATE_TO_IMPORT);
%end;

* Show values this iteration ;

%put ITERATIONS = &ITERATIONS  FIRST_DAY = %sysfunc(putn(&FIRST_DAY, yymmddn8.))
     LAST_DAY = %sysfunc(putn(&LAST_DAY, yymmddn8.)) ;

* This is where the real work will happen ;
* For test purposes, create a simple dataset with the iteration number ;
data Work.table&ITERATIONS;
a = &ITERATIONS;
run;

* For the next iteration, the first day is the first day of the month six months from now ;
%let FIRST_DAY = %sysfunc(intnx(month, &FIRST_DAY, 6, b));

%end; * do while ;

* Checking ... ;
%put Did loop &ITERATIONS times.;

* Concatenate the work files which were produced ;
data work.concat;
set
%do i = 1 %to &ITERATIONS;
  work.table&i
%end;
; * end the set statement;
run;

* Show concatenated data set ;
proc print data=Work.concat noobs;
run;

%mend doit;

* Ok, go... ;
%doit;

No comments:

Post a Comment