sassas-macrodo-loops

2 inputs into do loop in SAS


I need to create a sas do loop that will reference two moving values. This do loop will apply to a macro.

The Macro will look as follows:

%macro test (date1, date2);
DATA WANT;
SET HAVE;
IF INPUT_&DATE1. > INPUT_&DATE2. THEN R_&DATE1. = 1; ELSE R_&DATE2. = 0;
RUN;
%MEND;

I will then want to run this through a various dates so I would like to do something like this:

%MACRO APPLY_TEST;
%DO i = 4 %to 27 %by 1;
%test (&MMMYY&&i.);
%end;
%mend;

&APPLY_TEST;

Where &mmmyyy&i. will be dates so &mmmyy1 will be Sep23 for example and there are columns for all the dates I want to run through.

So as you can see the above will work when there's only one date inputting into the macro, but I would like it to input two dates. I'm not sure how to do this, would I need to include a

%DO j=5 %to 28 %by 1;

If so how would I add that to what I currently have?


Solution

  • I suspect you have created a series of macro variables with names that share a common prefix and have a number suffix. MMMYY1, MMMYY2, etc.

    To use an index (or counter) variable to reference those you need to use a double &.

    %let mmmyy1 = JAN2023 ;
    %let mmmyy2 = FEB2023 ;
    %let i=1;
    %put The value or MMMYY&i is &&MMMYY&i ;
    

    The && is converted to a single & and the macro process reminds itself to re-process the resulting string. So on the first pass &&MMMYY&i becomes &MMMYY1 which is then evaluated on the second pass to become JAN2023.

    Example:

    1295      %let mmmyy1 = JAN2023 ;
    1296      %let mmmyy2 = FEB2023 ;
    1297      %let i=1;
    1298      %put The value or MMMYY&i is &&MMMYY&i ;
    The value or MMMYY1 is JAN2023
    1299      %let j=%eval(&i+1);
    1300      %put The value or MMMYY&j is &&MMMYY&j ;
    The value or MMMYY2 is FEB2023