BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
cashsas
Calcite | Level 5

Hi, I cannot for the life of me understand why my macro is acting this way. 

 

Context: I am creating a macro to conditionally generate reports depending on the macro variable lReportNum. Below is a snippet of my code.

 

%if &lReptNum. in (1,2,3,4) %then
%do;

[proc report.. more sas code...]

%end;

 

For some strange reason, when opening a new SAS session and '%include'ing my macro to run it, I always get the following result from the first run:

SYMBOLGEN: Macro variable LREPTNUM resolves to 1
MLOGIC(PMREPORT_YOY): %IF condition &lReptNum. in (1,2,3,4) is FALSE

 

I don't understand why my condition is giving me FALSE. Here's the strange part: when I %include my macro again and run it a second time, I get the following result:

SYMBOLGEN: Macro variable LREPTNUM resolves to 1
MLOGIC(PMREPORT_YOY): %IF condition &lReptNum. in (1,3,5,7) is TRUE

 

Note that this results only if I %include my macro a second time. I am not sure why this is happening. Any insight or help would be appreciated.

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

The IN operator is a recent addition to macro code.  As such you need to make sure that you have set system options properly if you want to use it.

http://documentation.sas.com/?docsetId=mcrolref&docsetTarget=p0pbehl7wj5sl4n1ov1ortnehtba.htm&docset...

 

I suspect that your first attempt is changing this setting so that your second attempt then works.

 

That said your syntax does not look correct. Here is example from page linked above.

%put %eval(a IN a b c d);

Why do you have the parentheses?  Also did you make sure to set the delimiter to comma instead of space?

 

View solution in original post

4 REPLIES 4
Tom
Super User Tom
Super User

The IN operator is a recent addition to macro code.  As such you need to make sure that you have set system options properly if you want to use it.

http://documentation.sas.com/?docsetId=mcrolref&docsetTarget=p0pbehl7wj5sl4n1ov1ortnehtba.htm&docset...

 

I suspect that your first attempt is changing this setting so that your second attempt then works.

 

That said your syntax does not look correct. Here is example from page linked above.

%put %eval(a IN a b c d);

Why do you have the parentheses?  Also did you make sure to set the delimiter to comma instead of space?

 

cashsas
Calcite | Level 5

Hi Tom, thanks for your reply.

 

I found the solution. I had already set the following system options properly

options

minoperator
mindelimiter=',';

but I had set them inside the macro that conditionally generates the report. I moved the above options code outside the macro, and my code worked on the first run.

 

Just a follow-up question if you don't mind, do you know why setting the system options inside the macro caused my code to work on only the second time after %include'ing a second time?

 

Thanks again.

ballardw
Super User

@cashsas wrote:

Hi Tom, thanks for your reply.

 

I found the solution. I had already set the following system options properly

options

minoperator
mindelimiter=',';

but I had set them inside the macro that conditionally generates the report. I moved the above options code outside the macro, and my code worked on the first run.

 

Just a follow-up question if you don't mind, do you know why setting the system options inside the macro caused my code to work on only the second time after %include'ing a second time?

 

Thanks again.


A guess without seeing the entire macro code would be that the options were set after the first use of the in operator. If the include file had multiple macros it might be the order they were called for use such that the macro with the options wasn't called before another macro that used the option. Just defining a macro with the options wouldn't set them until the macro was executed at least once.

Tom
Super User Tom
Super User

When you set the options makes a difference.

It is the setting when CALL the macro that makes the difference.

120   %macro test ;
121     %if x in a b c %then %put found ; %else %put not found ;
122   %mend test;
123
124
125   options nominoperator;
126   %put %sysfunc(getoption(minoperator));
NOMINOPERATOR
127   %test;
ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is
       required. The condition was: x in a b c
ERROR: The macro TEST will stop executing.
128
129   options minoperator;
130   %put %sysfunc(getoption(minoperator));
MINOPERATOR
131   %test;
not found

Note that if you use the options on the %MACRO statement then your macro will not depend on the system option.

156   %macro test / minoperator;
157     %if x in a b c %then %put found ; %else %put not found ;
158   %mend test;
159
160
161   options nominoperator;
162   %put %sysfunc(getoption(minoperator));
NOMINOPERATOR
163   %test;
not found
164
165   options minoperator;
166   %put %sysfunc(getoption(minoperator));
MINOPERATOR
167   %test;
not found

 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 817 views
  • 1 like
  • 3 in conversation