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!!!!!!

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

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
  • 790 views
  • 0 likes
  • 2 in conversation