Desktop productivity for business analysts and programmers

Macro-ing various 'if' statements

Accepted Solution Solved
Reply
Contributor
Posts: 27
Accepted Solution

Macro-ing various 'if' statements

Hi all,

when running this

data tests;

      set allofit;

      %macro anotherloop(start,stop);

      %do n = &start. %to &stop.;

            opendate_&n. > actual_inception_date and

      %end;

      %mend anotherloop;

      if %anotherloop(start=0,stop=1); opendate_2 < actual_inception_date;

run;

, SAS tells me there's a syntax error. What I'm trying to get to is this:

if opendate_0 > actual_inception_date and

opendate_1 > actual_inception _date and

...

opendate_27  > actual_inception_date and opendate_2 actual_inception_date;

Any help would be greatly appreciated.

thanks,

Marco


Accepted Solutions
Solution
‎06-02-2014 09:58 AM
Esteemed Advisor
Posts: 6,636

Re: Macro-ing various 'if' statements

Do not write a semicolon after the macro invocation. That semicolon breaks your if condition because it is not part of the macro invocation but part of the "normal" program code.

if %anotherloop(start=0,stop=1) opendate_2 < actual_inception_date;

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
Respected Advisor
Posts: 4,955

Re: Macro-ing various 'if' statements

The problem lies in switching between keyword and positional parameters in your macro.  If  you leave the macro definition as is, call it using:

%anotherloop (0,1)

Alternatively, you could change the macro definition to:

%macro anotherloop (start=, stop=);

Then you could leave the macro call as is.

Good luck.

Solution
‎06-02-2014 09:58 AM
Esteemed Advisor
Posts: 6,636

Re: Macro-ing various 'if' statements

Do not write a semicolon after the macro invocation. That semicolon breaks your if condition because it is not part of the macro invocation but part of the "normal" program code.

if %anotherloop(start=0,stop=1) opendate_2 < actual_inception_date;

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Esteemed Advisor
Esteemed Advisor
Posts: 7,190

Re: Macro-ing various 'if' statements

Just thinking out-loud, but would you not be better off finding the smallest date from opendate, then comparing that to actual_inception_date?  E.g.:

proc sql;

     create table WANT as

     select     A.*

     from       HAVE A

     left join   (select MIN(OPENDATE) as MIN_OPEN_DATE from HAVE group by **grouping variables**) B

     on          A.**grouping variables** = B.**grouping variables**

     where     B.MIN_OPEN_DATE > A.ACTUAL_INCEPTION_DATE;

quit;

This way if there is one date below the inception date, you know the rest of the if are going to fail (as they are all AND), and if the minimum is > inception date then the if will pas as none could be lower.

Respected Advisor
Posts: 4,955

Re: Macro-ing various 'if' statements

Something along those lines occurred to me, but you do have to watch out for missing values.  MIN will ignore them, so the DATA step logic could perform differently.

Esteemed Advisor
Esteemed Advisor
Posts: 7,190

Re: Macro-ing various 'if' statements

Sorry to re-post on this, I just wanted to correct myself as you are using transposed data rather than horizontal.  Use datastep with:

data want;

     set have;

     if min(of opendate_1-opendate27) > actual_inception_date then output;

run;

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 269 views
  • 6 likes
  • 4 in conversation