DATA Step, Macro, Functions and more

How to use IN operator in SAS Macro

Accepted Solution Solved
Reply
Contributor
Posts: 67
Accepted Solution

How to use IN operator in SAS Macro

Hi Members,

I am trying to set up a run date base on whether it is holiday or not. The program below is for the date right after holiday only. For example, if the holiday is 09/02/2013, this program will run on 09/03/2013. In this case, it involves a date check,  I want to set up RUNDATE to the holiday date (09/02/2013).

ps: since program use today() function, and now is not holiday, so I assume that 06/17/2013 is holiday for testing purpose.

%LET HOLD = '02SEP2013'D ,'17JUN2013'D;

%MACRO CHCK (DATESET=);

   DATA TEST;

     FORMAT RUNDATE  DATE9.;

     TO_DAY = TODAY()-1;

     %IF &DATESET. = AUTO %THEN %DO;

         %IF TO_DAY IN &HOLD. %THEN RUNDATE = TODAY()-1;

         %ELSE %IF TO_DAY NOT IN &HOLD. %THEN RUNDATE = TODAY();

         %END;

     %ELSE RUNDATE = &DATESET.;

PROC PRINT DATA = TEST;

%MEND;

%CHCK(DATESET=AUTO);

ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition was:  TO_DAY IN &HOLD.

It seems like I did not use IN operator correctly.

Please help.

Thanks in advance.


Accepted Solutions
Solution
‎06-18-2013 08:42 PM
Super User
Super User
Posts: 6,497

Re: How to use IN operator in SAS Macro

You are not using macro conditionals properly.  It looks like you just want to use normal IF statement as you want to compare values of a dataset variable (TO_DAY).  I also find that it helps to keep the distinction if you intent the macro statements independently of the indentation of the actual SAS statements.

%MACRO CHCK (DATESET=);

DATA TEST;

  FORMAT TO_DAY RUNDATE DATE9.;

  TO_DAY = TODAY()-1;

%IF &DATESET. = AUTO %THEN %DO;

  IF TO_DAY IN (&HOLD) THEN RUNDATE = TO_DAY;

  ELSE RUNDATE=TO_DAY+1;

%END;

%ELSE %DO;

  RUNDATE = &DATESET.;

%END;

RUN;

PROC PRINT DATA = TEST;

RUN;

%MEND CHCK;

80   %LET HOLD = '02SEP2013'D ,'17JUN2013'D;

81   %CHCK(DATESET=AUTO);

MPRINT(CHCK):   DATA TEST;

MPRINT(CHCK):   FORMAT TO_DAY RUNDATE DATE9.;

MPRINT(CHCK):   TO_DAY = TODAY()-1;

MPRINT(CHCK):   IF TO_DAY IN ('02SEP2013'D ,'17JUN2013'D) THEN RUNDATE = TO_DAY;

MPRINT(CHCK):   ELSE RUNDATE=TO_DAY+1;

MPRINT(CHCK):   RUN;

...

82   %CHCK(DATESET='01JAN2013'D);

MPRINT(CHCK):   DATA TEST;

MPRINT(CHCK):   FORMAT TO_DAY RUNDATE DATE9.;

MPRINT(CHCK):   TO_DAY = TODAY()-1;

MPRINT(CHCK):   RUNDATE = '01JAN2013'D;

MPRINT(CHCK):   RUN;

View solution in original post


All Replies
Super User
Posts: 17,750

Re: How to use IN operator in SAS Macro

http://www.amadeus.co.uk/sas-technical-services/tips-and-techniques/macro/using-the-in-operator-with...

Common question, one link with the answer is above, many others on here or via google.

Contributor
Posts: 67

Re: How to use IN operator in SAS Macro

Hi Reeza,

Thanks for your link. I have checked online, but I didn't see an example involve date in IN operator.

It would be great if you have any reference that have date involved.

Thanks again.

Super User
Posts: 17,750

Re: How to use IN operator in SAS Macro

If that's in a data step it doesn't need to be a macro if/then, it can be standard SAS dataste if/then (no %).

Also the IN can be a standard IN, not a %in .

Solution
‎06-18-2013 08:42 PM
Super User
Super User
Posts: 6,497

Re: How to use IN operator in SAS Macro

You are not using macro conditionals properly.  It looks like you just want to use normal IF statement as you want to compare values of a dataset variable (TO_DAY).  I also find that it helps to keep the distinction if you intent the macro statements independently of the indentation of the actual SAS statements.

%MACRO CHCK (DATESET=);

DATA TEST;

  FORMAT TO_DAY RUNDATE DATE9.;

  TO_DAY = TODAY()-1;

%IF &DATESET. = AUTO %THEN %DO;

  IF TO_DAY IN (&HOLD) THEN RUNDATE = TO_DAY;

  ELSE RUNDATE=TO_DAY+1;

%END;

%ELSE %DO;

  RUNDATE = &DATESET.;

%END;

RUN;

PROC PRINT DATA = TEST;

RUN;

%MEND CHCK;

80   %LET HOLD = '02SEP2013'D ,'17JUN2013'D;

81   %CHCK(DATESET=AUTO);

MPRINT(CHCK):   DATA TEST;

MPRINT(CHCK):   FORMAT TO_DAY RUNDATE DATE9.;

MPRINT(CHCK):   TO_DAY = TODAY()-1;

MPRINT(CHCK):   IF TO_DAY IN ('02SEP2013'D ,'17JUN2013'D) THEN RUNDATE = TO_DAY;

MPRINT(CHCK):   ELSE RUNDATE=TO_DAY+1;

MPRINT(CHCK):   RUN;

...

82   %CHCK(DATESET='01JAN2013'D);

MPRINT(CHCK):   DATA TEST;

MPRINT(CHCK):   FORMAT TO_DAY RUNDATE DATE9.;

MPRINT(CHCK):   TO_DAY = TODAY()-1;

MPRINT(CHCK):   RUNDATE = '01JAN2013'D;

MPRINT(CHCK):   RUN;

Contributor
Posts: 67

Re: How to use IN operator in SAS Macro

Thanks Tom, this works for me.

☑ This topic is SOLVED.

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

Discussion stats
  • 5 replies
  • 427 views
  • 0 likes
  • 3 in conversation