Monday, July 18, 2011

Date Manipulation in SAS

I need to produce a report that contains historical data of some sort based it's Tuesday snapshots in the year 2009 and 2010.  Then I found this Intnx function to help.  For example:

To get a list of Tuesdays starting Jan 6, 2010 into a dataset called tuesdays:
%let start_date='06jan2009'd;
data tuesdays ;
do i = 0 to 103;
    Tuesday=put(Intnx( 'Day' , &start_date ., i*7 ),date9.);
drop i;
run ;
proc print data=tuesdays; run;
To load all Tuesday's SAS data files:
%let start_date='06jan2009'd;
%macro load_tuesday_sas_dataset;
    %global tuedate;
    %do i=0 %to 103;
        data _null_;
            call symput("tuedate",left(put(Intnx('Day',&start_date., &i. * 7),date9.)));
        %put &tuedate;
        data dataset_&tuedate.;
            set filelib.sasfile_&tuedate.;
%mend load_tuesday_sas_dataset;
Enjoy learning SAS, a little bit at a time every day.

No comments: