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 | Personal phone call | |
2 | Personal letter |
| |
3 | Personal letter | Personal phone call | Text |
4 | Text |
|
Data Want
ID | Method1 | Method2 | Method3 | Personal |
1 | Text | Personal phone call | Personal phone call | |
2 | Personal letter |
| Personal letter | |
3 | Personal letter | Personal phone call | Text | Personal letter Personal phone call |
4 | Text |
|
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;
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;
Thank you
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
Thank you
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.