Hello all,
respondents to a survey indicated a list of names (fiction authors), and they were recorded in a single variable, separated by a comma.
Thus,
| OBS | string |
| 1 | Toni Morrison |
| 2 | Kurt Vonnegut ,Harper Lee ,T.S. Eliot,Toni Morrison, Danielle Steel |
| 3 | Robert Ludlum ,James Patterson ,Tom Clancy ,Danielle Steel ,John Grisham |
I need to create a new variable (NEW) that has the frequency of CERTAIN authors. That is, the NEW should not simply count the number of words in STRING. For instance, I want NEW to count Morrison and Steel. So to obtain the following:
| OBS | string | NEW |
| 1 | Toni Morrison | 1 |
| 2 | Kurt Vonnegut ,Harper Lee ,T.S. Eliot,Toni Morrison, Danielle Steel | 2 |
| 3 | Robert Ludlum ,James Patterson ,Tom Clancy ,Danielle Steel ,John Grisham | 1 |
I am having troubles finding a solution.
Any suggestion would be greatly appreciated
Eman
One way
data have;
input OBS string : $ 3-203;
infile datalines4 dlm='|';
datalines;
1|Toni Morrison
2|Kurt Vonnegut ,Harper Lee ,T.S. Eliot,Toni Morrison, Danielle Steel
3|Robert Ludlum ,James Patterson ,Tom Clancy ,Danielle Steel ,John Grisham
;
data want(drop=i);
set have;
new=0;
do i=1 to countw(string);
if propcase(scan(string, i)) in ('Morrison', 'Steel') then new+1;
end;
run;
Thank you! Elegant, and fast.
I love this community.
Anytime 🙂 please mark my reply as the solution. This helps future users navigate the community.
done, thank you again.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.