I have an SQL statement in SAS which generates many variables from a large dataset. In some cases, I only want some of the variables to generate. I could easily put %IF %THEN conditions around each line / blocks of lines inside the macro, However, I was hoping to make my code less messy by having the %IF %THEN inside a macro variable &AA. ,sum(debt) as debt &BB. instead of %IF &TVAR. = Y %THEN %DO; ,sum(debt) as debt %end; However, when I try to run such code, I get aSyntax error: NOTE: Line generated by the macro variable "AA".
36 %if &TVAR. = Y %then %do; Here is an example of the code that should work but doesn't. Is SAS simply unable to resolve %IF %THEN macro functions from Macro varibales? %let TVAR = Y;
%let AA = %nrstr(%if &TVAR. = Y %then %do;);
%let BB = %nrstr(%end;);
%put &AA.;
%macro ttestm;
Data Test; A = 5; run;
proc sql;
create table Test2 as
select
1 as A
,2 as B
&AA. ,3 as C &BB.
from Test
;quit;
%mend;
%ttestm; There are workarounds I can create but i'd rather have &AA. instead of long %IF &THENs all over the place. Thanks Tomek EDIT: I need to add that the above code is an example. In reality i have code like: ,sum(case when debt_age is between 0 and 3 then debt else 0 end) as Debt3m
,max(write_off date) as latest_wod
,sum(case when payment > 0 and DOCTYPE = 'AR' then BILL else 0 end) as latest_payment Again, this is example code, but when you have tens of these types of variables and you need some to run always, and others to only run sometimes, I wanted neat code. However, It seems like SAS simply can't execute macro code from a variable.
... View more