Hi there,
I have two variables in my dataset that have similar information, but some times the values are not consistent. I am trying to write an if then statement where if one of the two variables has a value of interest, then both variables should have the same consistent value. I have included a sample code below.
data have;
set want;
if var1="Apples" or var2= "Apples"
then var1="Apples" and var2="Apples";
run;
When I attempted the above code, I have "0" populated for var2, and I am unsure why this is occurring. When I make the following correction to my code (below), it does what I want the code to do.
data have;
set want;
if var1="Apples"
then var2="Apples";
if var2="Apples"
then var1="Apples"
run;
Though my second code worked, it is longer and I am wondering why my initial code didn't work. Does anyone have any insight on why this is occurring?
Thank you!
You need to use the DO BOCK with IF/THEN statement to assign values in multiple variables.
Eg:
data want;
set have;
if var1="Apples" or var2= "Apples" then do;
var1="Apples";
var2="Apples";
end;
run;
@jnivi wrote:
data have;
set want;
if var1="Apples" or var2= "Apples"
then var1="Apples" and var2="Apples";
run;
This code, although valid SAS code, does not do what you want. The IF part tests to see if var1='Apples' OR var2='Apples' and THEN assigns a value to VAR1. It does not assign values to VAR1 and VAR2, as your second code does.
What value does it assign to VAR1? It is the result of the operation to the right of the first equal sign: "Apples" and var2="Apples", which is a Boolean operation resulting in a value of either 0 or 1.
You need to use the DO BOCK with IF/THEN statement to assign values in multiple variables.
Eg:
data want;
set have;
if var1="Apples" or var2= "Apples" then do;
var1="Apples";
var2="Apples";
end;
run;
@A_Kh wrote:
You need to use the DO BOCK with IF/THEN statement to assign values in multiple variables.
Eg:
data have;
if var1="Apples" or var2= "Apples" then do;
var1="Apples";
var2="Apples";
end;
run;
Don't forget the SET statement. Otherwise there are no variable values.
Thank you so much!! This worked !!
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.