Solved
New Contributor
Posts: 2

# Need hints on %IF Statement in EG 5.1

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!

Accepted Solutions
Solution
‎02-17-2014 03:32 PM
Super User
Posts: 8,272

## Re: Need hints on %IF Statement in EG 5.1

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) ...

All Replies
Solution
‎02-17-2014 03:32 PM
Super User
Posts: 8,272

## Re: Need hints on %IF Statement in EG 5.1

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) ...

Super User
Posts: 6,921

## Re: Need hints on %IF Statement in EG 5.1

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.

Super User
Posts: 8,272

## Re: Need hints on %IF Statement in EG 5.1

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

PROC Star
Posts: 1,471

## Re: Need hints on %IF Statement in EG 5.1

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()
```
New Contributor
Posts: 2