Solved
Contributor
Posts: 20

# finding specific words and creating a new variable

Hi,

I have a dataset that lists various ways a student has been contacted by the school. I want to create a separate variable called “personal” which includes only those methods that have the word “personal” in them.

I used a if find () then syntax to create separate columns for personal phone call and personal letter and then used catt function to combine then into one variable “personal.”

Is there a more efficient way to get this done?

Thank you.

Data Have

 ID Method1 Method2 Method3 1 Text Email Personal phone call 2 Email Personal letter 3 Personal letter Personal phone call Text 4 Email Text

Data Want

 ID Method1 Method2 Method3 Personal 1 Text Email Personal phone call Personal phone call 2 Email Personal letter Personal letter 3 Personal letter Personal phone call Text Personal letter Personal phone call 4 Email Text

Accepted Solutions
Solution
‎02-18-2015 08:14 AM
Super User
Posts: 9,599

## Re: finding specific words and creating a new variable

Well, its pretty straight forward, assuming there is only one occurence of "Personal":

data have;

length method1-method3 \$20;

id=1; method1="Text"; method2="Email"; method3="Personal phone call"; output;

id=2; method1="Email"; method2="Personal letter"; method3=""; output;

run;

data want (drop=i);

set have;

length personal \$20;

array method{3};

do i=1 to 3;

if index(method{i},"Personal")>0 then personal=method{i};

end;

run;

All Replies
Solution
‎02-18-2015 08:14 AM
Super User
Posts: 9,599

## Re: finding specific words and creating a new variable

Well, its pretty straight forward, assuming there is only one occurence of "Personal":

data have;

length method1-method3 \$20;

id=1; method1="Text"; method2="Email"; method3="Personal phone call"; output;

id=2; method1="Email"; method2="Personal letter"; method3=""; output;

run;

data want (drop=i);

set have;

length personal \$20;

array method{3};

do i=1 to 3;

if index(method{i},"Personal")>0 then personal=method{i};

end;

run;

Contributor
Posts: 20

## Re: finding specific words and creating a new variable

Thank you

Regular Contributor
Posts: 171

## Re: finding specific words and creating a new variable

I am considering the RW9 approach,

data have;

length method1-method3 \$20;

id=1; method1="Text"; method2="Email"; method3="Personal phone call"; output;

id=2; method1="Email"; method2="Personal letter"; method3=""; output;

id=3;method1="Personal letter"; method2="Personal phone call"; method3="Text"; output;

id=4;method1="Email"; method2="Text"; method3=""; output;

run;

data want(drop=i);

set have;

length personal \$50.;

array method(3);

do i=1 to 3;

if index(method{i},"Personal")>0 then personal=catx(' ',personal,method{i});

end;

run;

Thanks

Sam

Contributor
Posts: 20

## Re: finding specific words and creating a new variable

Thank you

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
• 4 replies
• 321 views
• 3 likes
• 3 in conversation