Tladoo
Tladoo

Reputation: 21

Using macro variable as column name

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

Upvotes: 0

Views: 824

Answers (2)

Joe
Joe

Reputation: 63424

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.

Upvotes: 1

DWal
DWal

Reputation: 2762

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.

Upvotes: 1

Related Questions