SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
cpmcn19
Calcite | Level 5

Attempting to create new variable top breed based on dog data set. Trying to set top breed to 1 if the breed contains the words in the if statement , and 0 if not. Currently code is: 

 

data_aac_new_variable;

set = dog_data;

if breed = "Bulldog", "Lab", "Terrier", "Shepherd", "Beagle" then Topbreed = 1;

else = 0;

run;

5 REPLIES 5
novinosrin
Tourmaline | Level 20
if breed in ("Bulldog", "Lab", "Terrier", "Shepherd", "Beagle") then

use the IN operator

 

data _aac_new_variable; 

set dog_data;

if breed in ( "Bulldog", "Lab", "Terrier", "Shepherd", "Beagle") then Topbreed = 1;

else = 0;

run;
r_behata
Barite | Level 11

Few Issues With the code corrected:

 

 

data_aac_new_variable;

set  dog_data;

if breed  in ("Bulldog", "Lab", "Terrier", "Shepherd", "Beagle" ) then Topbreed = 1;

else Topbreed = 0;

run;
data_aac_new_variable;

set = dog_data;
if breed = "Bulldog", "Lab", "Terrier", "Shepherd", "Beagle" then Topbreed = 1;
else = 0;

run;

 

 

 

cpmcn19
Calcite | Level 5

Thanks for the reply. Is there anyway to add a contains to the if breed in statement? Some data says Lab whereas other data says Labrador 

r_behata
Barite | Level 11

Try :

 

data _aac_new_variable;

set  dog_data;

if breed  in ("Bulldog",  "Terrier", "Shepherd", "Beagle" ) or breed =:'Lab' then Topbreed = 1;

else Topbreed = 0;

run;
SASKiwi
PROC Star

You can reduce this to one statement if you use a Boolean expression:

Topbreed = (if breed in ("Bulldog", "Terrier", "Shepherd", "Beagle" ) or breed =:'Lab');

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

Register now!

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
  • 5 replies
  • 1037 views
  • 2 likes
  • 4 in conversation