BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
jnivi
Calcite | Level 5

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!

 

1 ACCEPTED SOLUTION

Accepted Solutions
A_Kh
Lapis Lazuli | Level 10

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;

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26

@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.

--
Paige Miller
A_Kh
Lapis Lazuli | Level 10

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;
ballardw
Super User

@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.

jnivi
Calcite | Level 5

Thank you so much!! This worked !!

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 573 views
  • 2 likes
  • 4 in conversation