BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Angi
Obsidian | Level 7

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

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

View solution in original post

4 REPLIES 4
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

Angi
Obsidian | Level 7

Thank you Smiley Happy

sam369
Obsidian | Level 7

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

Angi
Obsidian | Level 7

Thank you Smiley Happy

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 2334 views
  • 3 likes
  • 3 in conversation