gen
gen

Reputation: 1

Running a SAS macro for every day in a range of dates

I am an intermediate user of SAS, but I have limited knowledge of arrays and macros. I have a set of code that prompts the user to enter a date range. For example, the user might enter December 1, 2015-December 5,2015. For simplicity, imagine the code looks like:

data new; set old; 
if x1='December 1, 2015'd then y="TRUE";
run;

I need to run this same code for every day in the date prompt range, so for the 1st, 2nd, 3rd, 4th, and 5th. My thought was to create an array that contains the dates, but I am not sure how I would do that. My second thought was to create a macro, but I can't figure out out to feed a list through a macro.

Also, just FYI, the code is a lot longer and more complicated than just a data step.

Upvotes: 0

Views: 1510

Answers (2)

Andrew
Andrew

Reputation: 3871

The following macro can be used as a framework for your code:

    %MACRO test(startDate, endDAte);

    %DO i=&startDate %to &endate;

         /* data steps go here */

         /* example */
         DATA test; 
              SET table;
              IF x1 = &i THEN y = "true";
         RUN;

    %END;

    %MEND;

Upvotes: 1

Reeza
Reeza

Reputation: 21274

Look into call execute to call your macro and a data null step using a do loop to loop through the days. Getting the string correct for the call execute can sometimes be tricky, but worth the effort overall.

data sample;
do date='01Jan2014'd to '31Jan2014'd;
    output;
end;
run;

%macro print_date(date);

proc print data=sample;
where date="&date"d;
format date date9.;
run;

%mend;

%let date_start=05Jan2014;
%let date_end=11Jan2014;

data _null_;
    do date1="&date_start"d to "&date_end"d by 1;
        str='%print_date('||put(date1, date9.)||');';
        call execute(str);  
    end;
run;

Upvotes: 0

Related Questions