WK_83
WK_83

Reputation: 1

Checking existence of SAS prompt variable

What I have to do is to make a simple SAS EG process which asks user for two parameters - a date and an identifier. Both are optional.

The problem is, that if user does not fill one of them, the following line:

table.insert_date = "&my_date."d 

returns

ERROR: Invalid date/time/datetime constant ""d

I've worked on some code which could be inserted before all code in project (Tools -> options -> Custom code -> Insert custom SAS code before task and query code). I've tried this, and it does not work.

data _null_;
   if not(%symexist(my_date)) 
   then %let my_date='01jan01'd;
run;

Do you have any idea how to handle this problem?

Upvotes: 0

Views: 414

Answers (2)

WK_83
WK_83

Reputation: 1

Dmitry, thank you for your answer. It still doesn't work the way I'd like to... and now I know why.

I figured out (I believe if someone is better at SAS he'd be able to explain it better than me) that prompt variables are created when the prompt box appears. So, the problem is that they're empty, and the problem shown above appears.

Below is the answer which works the way I'd like to.

data _null_;
 tmp = symget("my_date");  /* Need to assign my_date value to temp variable. */

 if tmp eq '' then /* because my_date eq '' doesn't work */
      do;
            call symput("my_date", '01jan01' ); /* assigning target variable */
      end;
      else call symput ("my_date", '01jan14'); /* assigning other value */
run;

Upvotes: 0

Dmitry Shopin
Dmitry Shopin

Reputation: 1763

The problem is that you are trying to use macro-code as a part of open code. It's not the way macros in SAS work - they are executed BEFORE any open code and usually serve to generate the latter.

In your case you don't need to generate any open code, actually, but anyway you need to wrap macro-statements into %MACRO-block and then execute it as a whole envoking this macro:

%macro mymacro;
    %if not (%symexist(my_date)) %then %let my_date='01jan01'd;
%mend mymacro;

%mymacro

Upvotes: 2

Related Questions