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

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, 

OBSstring
1Toni Morrison   
2Kurt Vonnegut   ,Harper Lee   ,T.S. Eliot,Toni Morrison, Danielle Steel
3Robert 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:

 

OBSstringNEW
1Toni Morrison   1
2Kurt Vonnegut   ,Harper Lee   ,T.S. Eliot,Toni Morrison, Danielle Steel2
3Robert Ludlum   ,James Patterson   ,Tom Clancy   ,Danielle Steel   ,John Grisham   1

 

I am having troubles finding a solution.

 

Any suggestion would be greatly appreciated

 

Eman

1 ACCEPTED SOLUTION

Accepted Solutions
emaneman
Pyrite | Level 9

Thank you! Elegant, and fast.

 

I love this community. 

View solution in original post

4 REPLIES 4
PeterClemmensen
Tourmaline | Level 20

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;
emaneman
Pyrite | Level 9

Thank you! Elegant, and fast.

 

I love this community. 

PeterClemmensen
Tourmaline | Level 20

Anytime 🙂 please mark my reply as the solution. This helps future users navigate the community.

emaneman
Pyrite | Level 9

done, thank you again.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 4 replies
  • 883 views
  • 0 likes
  • 2 in conversation