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

I'm trying to create a variable 'Baby' which contains either 'baby' or 'infant' or 'toddler' from a variable 'Brand'. I figured it out (after a long time, SAS novice here) how to do a single word (baby) but I would like to add two more (infant and toddler). Can you please help?? thank you 🙂

---here is my code---

Data baby ;
Set baby;
if find (Brand, "baby" , "i") then Baby = 1; else baby = 0; run;

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26
if find (Brand, "baby" , "i")>0 or find(Brand,'infant','i')>0 or find(Brand,'toddler','i')>0 then Baby = 1; else baby = 0;
--
Paige Miller

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26

@Lena_PA wrote:

I'm trying to create a variable 'Baby' which contains either 'baby' or 'infant' or 'toddler' from a variable 'Brand'. I figured it out (after a long time, SAS novice here) how to do a single word (baby) but I would like to add two more (infant and toddler). Can you please help?? thank you 🙂

---here is my code---

Data baby ;
Set baby;
if find (Brand, "baby" , "i") then Baby = 1; else baby = 0; run;


You say you want a variable which contains 'baby' or 'infant' or 'toddler', but then you show code that assigns 0 or 1 to this variable. Please clarify what you want.

--
Paige Miller
Lena_PA
Fluorite | Level 6

Oh sorry. I want to create a new variable name "BABY" = 1 if the existing variable "Brand" contains "baby" or "infant" or "toddler".  If Brand does not include any of these three words, then BABY =0. 

PaigeMiller
Diamond | Level 26
if find (Brand, "baby" , "i")>0 or find(Brand,'infant','i')>0 or find(Brand,'toddler','i')>0 then Baby = 1; else baby = 0;
--
Paige Miller
Lena_PA
Fluorite | Level 6

Thank you so much!!!!!!

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