DATA Step, Macro, Functions and more

Check to see if macro exists

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 12
Accepted Solution

Check to see if macro exists

I'm trying to check to see if a macro variable exists. I use a %WINDOW to allow users to enter values. The user does not have to enter a value for all options, since in my actual program there are >40 options. I'm trying to find a way to see which ones the user enters a value for.

When testing this, I entered values for the first 2, and left the third one blank (IBCP_SCP). I researched the internet and stumbled across %SYMEXIST and %SYSMACEXIST, but those didn't seem to help (I included the code I tried below).

I appreciate any help you can provide. Thanks!

%WINDOW VOLADJST

#3        @35 "Volume Adjustment Inputs (eg. For 5%, enter 5, NOT .05)" COLOR = BLUE

#6        @10 "Inbound Competitive" COLOR = RED //

            @15 "EMS" COLOR = BLACK +2 IBCP_EMS 4 COLOR = GREEN REQUIRED = NO ATTR = UNDERLINE /

            @15 "Air" COLOR = BLACK +2 IBCP_ACP 4 COLOR = GREEN REQUIRED = NO ATTR = UNDERLINE /

            @15 "Suface" COLOR = BLACK +2 IBCP_SCP 4 COLOR = GREEN REQUIRED = NO ATTR = UNDERLINE

;

%DISPLAY VOLADJST;

/*I got all true's when I ran the step below*/

%macro test;

        %if %symexist(IBCP_EMS) %then %put %nrstr(%symexist(IBCP_EMS)) = TRUE;

                         %else %put %nrstr(%symexist(IBCP_EMS)) = FALSE;

        %if %symexist(IBCP_ACP) %then %put %nrstr(%symexist(IBCP_ACP)) = TRUE;

                         %else %put %nrstr(%symexist(IBCP_ACP)) = FALSE;

        %if %symexist(IBCP_SCP) %then %put %nrstr(%symexist(IBCP_SCP)) = TRUE;

                         %else %put %nrstr(%symexist(IBCP_SCP)) = FALSE;

%mend;

%test;

/*I got all 0's when I ran the step below*/

DATA X;

      A = %sysmacexist(IBCP_EMS);

      B = %sysmacexist(IBCP_ACP);

      C = %sysmacexist(IBCP_SCP);

RUN;


Accepted Solutions
Solution
‎11-08-2012 10:55 AM
PROC Star
Posts: 1,322

Re: Check to see if macro exists

Hi,

As Tom pointed out, %symexist() tests whether or not a macro variable (symbol) exists.  %sysmacexist() tests whether a macro exists.

In your case, looks like you are using the %window statement to gather input values from users.  As I understand it, the %window statement you have will always create three macro variables.  But, the user may have left some of the values blank.

Even if a user does not input a value for &IBCP_EMS, the macro variables still exists, and the value is blank.  So %symexist() can't help you.

What you need is a way to test if a macro symbol is blank or not.  Luckily for you, Chang Chung and John King wrote a great paper on methods for doing this:

http://support.sas.com/resources/papers/proceedings09/022-2009.pdf

And they concluded with a suggestion for a simple macro, %isBlank, which should work for you, e.g.:

%macro isBlank(param) ;
  %sysevalf(%superq(param)=,boolean)
%mend isBlank ;

%put %isBlank(&IBCP_EMS) ;
%put %isBlank(&IBCP_ACP) ;
%put %isBlank(&IBCP_SCP) ;

HTH,

--Q.

View solution in original post


All Replies
Super User
Super User
Posts: 7,042

Re: Check to see if macro exists

You seem to be confusing macros with macro variables.  In your example code TEST is macro and IBCP_EMS and the others are macro variables.  So of course the test for whether the macro IBCP_EMS exists will return 0 (false) unless you have defined it previously with a %MACRO statement.

Solution
‎11-08-2012 10:55 AM
PROC Star
Posts: 1,322

Re: Check to see if macro exists

Hi,

As Tom pointed out, %symexist() tests whether or not a macro variable (symbol) exists.  %sysmacexist() tests whether a macro exists.

In your case, looks like you are using the %window statement to gather input values from users.  As I understand it, the %window statement you have will always create three macro variables.  But, the user may have left some of the values blank.

Even if a user does not input a value for &IBCP_EMS, the macro variables still exists, and the value is blank.  So %symexist() can't help you.

What you need is a way to test if a macro symbol is blank or not.  Luckily for you, Chang Chung and John King wrote a great paper on methods for doing this:

http://support.sas.com/resources/papers/proceedings09/022-2009.pdf

And they concluded with a suggestion for a simple macro, %isBlank, which should work for you, e.g.:

%macro isBlank(param) ;
  %sysevalf(%superq(param)=,boolean)
%mend isBlank ;

%put %isBlank(&IBCP_EMS) ;
%put %isBlank(&IBCP_ACP) ;
%put %isBlank(&IBCP_SCP) ;

HTH,

--Q.

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 2253 views
  • 0 likes
  • 3 in conversation