I'm trying to do a condition for my data sets one of two bases. For that I'm using the if statement, but it's not working. For example:
data work.test;
if &var. = 'yes' then set lib1.base_1;
else set lib2.base_2;
run;
It is always setting lib2.base_2 and creating a variable called yes.
How can I do to automatically sets one of this two bases only changing the variable &var.?
Thanks!
First, please understand the difference between IF and %IF.
IF works only on data step variables or constants. %IF works only on macro variables. So this line uses IF when it should use %IF
if &var. = 'yes'
To fix this, you need appropriate macro statements and syntax: %IF %THEN %DO and %ELSE %DO and the appropriate %END; after each.
%let var=yes;
data test;
%if &var=yes %then %do; set lib1.base_1; %end;
%else %do; set lib2.base_2; %end;
run;
Note: the above code works in SAS 9.4M5 or later.
First, please understand the difference between IF and %IF.
IF works only on data step variables or constants. %IF works only on macro variables. So this line uses IF when it should use %IF
if &var. = 'yes'
To fix this, you need appropriate macro statements and syntax: %IF %THEN %DO and %ELSE %DO and the appropriate %END; after each.
%let var=yes;
data test;
%if &var=yes %then %do; set lib1.base_1; %end;
%else %do; set lib2.base_2; %end;
run;
Note: the above code works in SAS 9.4M5 or later.
Also note, macro language merely substitutes for &VAR to generate a SAS language statement. Your SAS program includes:
if yes = 'yes' then set lib1.base_1;
Hopefully, if you saw this statement in your program you would realize that it does the wrong thing. (This also explains why SAS sees the need to create a variable named YES.)
The solutions already suggested are best. But recognize that you could also get the right result (but at the cost of a longer-running program) by adding double quotes:
if "&var" = 'yes' then set lib1.base_1;
This would generate a clumsy but workable program containing:
if "yes" = 'yes' then set lib1.base_1;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.