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;
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;
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;
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
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;
You can reduce this to one statement if you use a Boolean expression:
Topbreed = (if breed in ("Bulldog", "Terrier", "Shepherd", "Beagle" ) or breed =:'Lab');
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.