DATA Step, Macro, Functions and more

why %if %then does not work?

Reply
Frequent Contributor
Posts: 133

why %if %then does not work?

In a macro program, if, then statement, you have to use %if %then

but following macro program it says:

ERROR: Required operator not found in expression: symlocal('c')

ERROR: The macro TEST will stop executing.

Rather, if, then statement works,

I can't wrap my head around it.

%let c=yes;

%macro test;

  %let d=yes;

  data _null_;

   %if symlocal('c') %then %put '**** c a is local';

   %else %put '**** c is global';

   if symlocal ('d') then put '**** d is local';

   else put '**** d is global';

run;

%mend test;

%test

Frequent Contributor
Posts: 148

Re: why %if %then does not work?

Macro is a text processing facility and so you don't quote character strings. Plus it need not run in a data step. See:

%let c=yes;

%macro test;

  %let d=yes;

  %if %symlocal(c) %then %put **** c a is local;

  %else %put **** c is global;

run;

%mend test;

%test

Also note that sumlocal is both a macro function and not a data step function. So you need to use %symlocal when running it as macro code.

If you are new to macro I would suggest that you do a Google search for any papers by Ian Whitlock on the macro facility. His papers do a great job of explaining the background behind why what you tried did not work.

Frequent Contributor
Posts: 133

Re: why %if %then does not work?

Well, it confuses me because I don't know how to tell if it is macro variable or data step variable.

The example in my question is from: http://www2.sas.com/proceedings/sugi31/107-31.pdf

originally it is written like this:

%let c=yes;

%macro test;

%let d=yes;

data _null_;

if symlocal('c') then put '**** c a is local';

else put '**** c is global';

if symlocal ('d') then put '**** d is local';

else put '**** d is global';

run;

%mend test;

%test

so, you see, c and d are quoted in the symlocal function, and in the macro, %if %then statement are not used but only if then, it confuses me.

Can someone help explain?

PROC Star
Posts: 7,363

Re: why %if %then does not work?

Actually, unless you changed the original, you had them mixed (i.e., used symlocal once and %symlocal once.

I agree with Don that Ian's papers would be a good way to start.  If you are serious about wanting to learn, I have a spreadsheet that contains links to all of his SAS-L posts.

In the meantime, here is a different mixture that all work:

%let c=yes;

%macro test;

  %let d=yes;

  %if %symlocal(c) %then %put '**** c a is local';

  %else %put '**** c is global';

  data _null_;

    %if %symlocal(c) %then %put '**** c a is local';

    %else %put '**** c is global';

    if symlocal ('d') then put '**** d is local';

    else put '**** d is global';

run;

%mend test;

%test

Frequent Contributor
Posts: 133

Re: why %if %then does not work?

Hi, Art297,

Can you please share the links you have? I really love to check it out.

PROC Star
Posts: 7,363

Re: why %if %then does not work?

Send me an email at atabachneck at gmail dot com.  I can't post it here because Ian never gave me permission to post it on the web.  I wish he would, because there are numerous gold nuggets in those posts.

Super Contributor
Posts: 1,636

Re: why %if %then does not work?

Are you going to email me too if I ask for it?   Thanks - Linlin

PROC Star
Posts: 7,363

Re: why %if %then does not work?

Sure, just send me an email.  I don't have any problem sharing the spreadsheet, I just don't feel comfortable putting it on the web without Ian's permission.

Super Contributor
Posts: 1,636

Re: why %if %then does not work?

Thank you! I just emailed you.   - Linlin

Frequent Contributor
Posts: 133

Re: why %if %then does not work?

Fantastic, I just sent an email to you, really appreciate your help, Art

Ask a Question
Discussion stats
  • 9 replies
  • 444 views
  • 3 likes
  • 4 in conversation