0

I am designing a generic macro to do update a table using a macro variable (say X) which is used as a column name. The problem I have is that there are ocassions where this variable X (which is used as a column name in the where clause) is null. Can someone help please

2 Answers 2

1

I you don't want a where clause when &X is blank, then do something like:

%if %length(&X) > 0 %then where &X = 'blahblah';

So that the where clause is only created if &X has text in it.

Sign up to request clarification or add additional context in comments.

Comments

1

If &x is guaranteed to be defined but may be blank, then DWal's solution is the way to go. This is useful basically when the macro variable is a parameter, and will be defined every time the macro is run.

If it may not be defined at all, then you need to use %symexist() to check whether the macro variable is defined at all, possibly in addition to the %length check.

%macro test;
  %if %symexist(X) %then %if %length(&x)>0 %then %do; 
    %put &=x;
  %end;
%mend test;

%symdel x; *deletes x if it exists;

%test;

%let x=;
%test;

%let x=XValue;
%test;

I nest the %if to avoid an unintialized note.

2 Comments

It should be %put &x= ?
@rbet No, on 9.3+ this syntax is correct. On earlier versions the equal sign won't work anywhere (except to put the character itself).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.