Thursday, September 10, 2009

Check variable exist in DATA step run time

First off, there is NO way to know exactly which variable exist in DATA step run time.

We can check the variable existence before DATA step run time. The popular method is Macro technology with File I/O functions or Dictionary table.

How to avoid creating this variable if it does not exist and to get the formatted value of the variable if it exist.
Here ia another way:

* Sample;
data dummy;
set sashelp.class (obs=0);
run;

data test;
set sashelp.class;

if _n_ = 1 then do;
drop varexist dsid rc;
retain varexist;
dsid = open('dummy');
varexist = varnum(dsid, "name");
rc = close(dsid);
end;

length tmp $ 20;
if varexist then do;
tmp = vvaluex("name");
end;
else tmp = '';
run;

No comments: