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


Hi, can anyone help?

I have this statment:

%IF (&cur_period = &srt_period1) .......

The LOG are:

 

SYMBOLGEN: Macro variable CUR_PERIOD resolves to 2013-11

SYMBOLGEN: Macro variable SRT_PERIOD1 resolves to 2012-10

MLOGIC(BUILD_STRING_2): %IF condition (&Cur_period = &srt_period1) is TRUE

I think it should be FALSE. Any hints? Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Of course it is true. 2013-11 is 2002 and 2012-10 is also 2002.

If you want the implied %eval() in your %IF to treat this formula's as strings then add quotes or something else.

%if ("&Cur_period" = "&srt_period1") ...

or

%if (X&Cur_period = X&srt_period1) ...

View solution in original post

5 REPLIES 5
Tom
Super User Tom
Super User

Of course it is true. 2013-11 is 2002 and 2012-10 is also 2002.

If you want the implied %eval() in your %IF to treat this formula's as strings then add quotes or something else.

%if ("&Cur_period" = "&srt_period1") ...

or

%if (X&Cur_period = X&srt_period1) ...

Astounding
PROC Star

Tom,

Interestingly enough(??), the last variation won't work.  The hyphen still gets interpreted as subtraction.  However, now the presence of "X" causes an error because there is a character value within the arithmetic expression.

Tom
Super User Tom
Super User

Right. Either need to either quote it or macro quote it.  But interesting the presence of the non digit causes SAS to give an error that shows what is really happening.

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

       X2013-11X=X2011-10X

Quentin
Super User

That's a good one for a puzzle book!

Those dashes look like substraction to the macro processor.  There is an implied %EVAL() called by the %IF statement.

So

  %IF 2013-11= 2012-10

Becomes

  %IF (2013-11)=(2012-10)

i.e.

  %if 2002=2002

Solution is to add some macro quoting so that the dash isn't seen as a subsraction sign. Something like:

%macro check(dummy);
  %local cur_period strt_period1;
  %let cur_period=2013-11;
  %let strt_period1=2012-10;

  %put WITHOUT quoting ;
  %if &cur_period=&strt_period1 %then %put &cur_period EQUAL &strt_period1;
  %else %put &cur_period NOT EQUAL &strt_period1;

  %put WITH quoting: ;
  %if %superq(cur_period)=%superq(strt_period1) %then &cur_period EQUAL &strt_period1;
  %else %put &cur_period NOT EQUAL &strt_period1;
%mend check;

%check()
tlyang888
Calcite | Level 5

Thank you all for those hints. It did answer my question. I did forget about some basics in macro.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1684 views
  • 3 likes
  • 4 in conversation