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

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;

1 ACCEPTED SOLUTION

Accepted Solutions
Quentin
Super User

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.

BASUG is hosting free webinars Next up: Mike Sale presenting Data Warehousing with SAS April 10 at noon ET. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.

View solution in original post

2 REPLIES 2
Tom
Super User Tom
Super User

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.

Quentin
Super User

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.

BASUG is hosting free webinars Next up: Mike Sale presenting Data Warehousing with SAS April 10 at noon ET. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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