Reputation: 1396
The need is to perform valuation of a macro variable outside datastep and depending on the existence of the variable, perform insertion:
data my_dataSet;
set ...
....
if %SYMEXIST(Variable_from_prior_code) = 1 then do;
dataset_variable = &Variable_from_prior_code.;
end;
else do;
dataset_variable = &Some_default_value_from_prior_code;
end;
However, this fails at compiler when trying to run it as the " Apparent symbolic reference &Variable_from_prior_code." has not been resolved. Ie. the compiler checks the contents of the if statement even as the condition is not met.
I came up with silly work-around: to approach this from opposite directon, but feels more stupid than bag of badgers:
if %SYMEXIST(Variable_from_prior_code) = 0 then do;
dataset_variable = &Some_default_value_from_prior_code
%let Variable_from_prior_code=0; /*Dummy value*/
end;
else do;
dataset_variable = &Variable_from_prior_code.;
end;
Any way to restrict the compiler from evaluating content, which it shouldn't due to condition? Or alternatively, more elegant work-around, which do not require creation of the variable?
Upvotes: 0
Views: 641
Reputation: 12691
I'd say, avoid macro logic unless necessary! Here's a pure data step approach:
%symdel Variable_from_prior_code; /* make sure variable does not exist */
%let Some_default_value=test; /* populate macro variable */
data my_dataSet;
if SYMEXIST('Variable_from_prior_code') = 1 then do; /* use data step function */
/* note variable name is quoted, else would reference a data step variable value */
dataset_variable = symget('Variable_from_prior_code');
end;
else do;
/* had to shorten this name to less than max allowed 32 chars */
dataset_variable = symget('Some_default_value');
end;
run;
As Tom mentions, you are currently mixing up macro and data step logic. Macro is used to write data step code (so is essentially a program generator), and that resultant data step code is executed long after the macro statements are compiled / resolved / executed.
Upvotes: 1