BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Caíque
Calcite | Level 5

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!

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

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.

 

--
Paige Miller

View solution in original post

2 REPLIES 2
PaigeMiller
Diamond | Level 26

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.

 

--
Paige Miller
Astounding
PROC Star

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;
How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1225 views
  • 0 likes
  • 3 in conversation