BookmarkSubscribeRSS Feed
ZRick
Obsidian | Level 7

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

9 REPLIES 9
DonH
Lapis Lazuli | Level 10

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.

ZRick
Obsidian | Level 7

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?

art297
Opal | Level 21

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

ZRick
Obsidian | Level 7

Hi, Art297,

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

art297
Opal | Level 21

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.

Linlin
Lapis Lazuli | Level 10

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

art297
Opal | Level 21

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.

Linlin
Lapis Lazuli | Level 10

Thank you! I just emailed you.   - Linlin

ZRick
Obsidian | Level 7

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

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 9 replies
  • 1749 views
  • 3 likes
  • 4 in conversation