Reputation: 309
I'm having trouble getting If/Then statements to work correctly with my macro variables inside a datastep. I'm writing a macro to handle two different cases: calculating stat tests with no transformation, and then calculating stat tests after a natural log transformation. If my data fails the test of normality, I log transform and test again. If it passes, I set my global flag, log_flag
, to 1. I then want to test the status of this flag in data steps in order to correctly handle the transformed (or not) variables. I've tried variations of the following:
Data want;
set have;
if symget("log_flag")=1 then do;
if &log_flag. = 1 then do;
if resolve("log_flag")=1 then do;
test=symget("log_flag");
if test=1 then do;
end
No matter what I try, the if/then statement is essentially ignored and all code following it is processed as if the if/then were true, even when it is false. I know that the log_flag
is being correctly assigned a value of zero because the %if
%then
statements work and execute correctly in open code. I'm just having trouble getting it to resolve correctly inside a datastep.
Please let me know if there is any other information you need to help me figure this out. Thanks guys!
Upvotes: 1
Views: 1184
Reputation: 51566
The issue you have identified in the comments is that you do not want to generate the SAS code at all. That is what the macro language processor is for. So use %IF
to conditionally generate the code.
So if you only want to create the variable newvar
when the macro variable log_flag
is one then you could code it this way.
data want ;
set have ;
%if &log_flag. = 1 %then %do;
newvar= x*y ;
%end;
run;
So when &log_flag. = 1
you run this code:
data want ;
set have ;
newvar= x*y ;
run;
And when it isn't you run this code:
data want ;
set have ;
run;
Starting with SAS 9.4 M5 release you can use this in open code, otherwise place it inside a macro definition and execute the macro.
Upvotes: 2
Reputation: 21264
You need to treat them correctly depending on your reference method.
Here's an example of testing each one independently and then you can test them together via nesting if desired.
%let log_flag=1;
Data want;
set sashelp.class;
if symget("log_flag")='1' then do;
put "Test #1 is True";
end;
if &log_flag. = 1 then do;
put "Test #2 is True";
end;
if resolve("&log_flag")="1" then do;
put "Test #3 is True";
end;
test=symget("log_flag");
if test='1' then do;
put "Test #4 is True";
end;
run;
Upvotes: 0