BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
robAs
Fluorite | Level 6

Hi all, This one is quite frustrating. I want to return the Nth word of a string which matches (NOT case-sensitive)  my 'search term'.
Illustrated with example code using sashelp dataset.
My code :   

*Create string which will be searched;
%let allowedTeams=montreal Cleveland ATLANTA houston Pittsburgh; 

*Test dataset; 
data work.baseballTest (drop=allowedT);
  set sashelp.baseball(keep=team );
  if _n_ < 20;

  allowedT='montreal Cleveland ATLANTA houston Pittsburgh';

  *Various styles of findw None of which appear to work;
  findwRes1 = findw("&allowedTeams",team,' ','ei');  
  findwRes2= findw(allowedT,team,' ','e i');  
  findwRes3= findw("montreal Cleveland ATLANTA houston Pittsburgh",team,' ','e i');  
  findwRes4= findw(upcase("&allowedTeams"),upcase(team),' ','E');  
  findwRes5= findw("&allowedTeams",team,' ','i');  
  findwRes6= findw("&allowedTeams",team);

  *Indexw locates the string. But extra work is needed to get the ;
  *number of the word in the string; 
  indexwRes= indexw(upcase("&allowedTeams"), upcase(team));
  if indexwres > 0 then do;
    indexwRes1= countw(substr("&allowedTeams", 1, indexwRes), ' ', 'i');
  end;
run;

On running this code I attempt several different ways to get the FINDW to work for me (findwRres1-6) . Looking in the resultant dataset none of them do as they are all =0.
I am able to use a workaround using indexw and countw (as shown) which does work correctly  but is not as slick as the FINDW solution would be.  Can anyone suggest what is going wrong with my findw attempts?
 

    

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Please very explicitly provide what you expect for at least one observation.

 

Perhaps you want this:

data work.baseballTest (drop=allowedT);
  set sashelp.baseball(keep=team );
  if _n_ < 20;

  allowedT='montreal Cleveland ATLANTA houston Pittsburgh';

  *Various styles of findw None of which appear to work;
  findwRes1 = findw("&allowedTeams",strip(team),' ','ei');  
  findwRes2= findw(allowedT,strip(team),' ','e i');  
  findwRes3= findw("montreal Cleveland ATLANTA houston Pittsburgh",strip(team),' ','e i');  
  findwRes4= findw(upcase("&allowedTeams"),upcase(strip(team)),' ','E');  
  findwRes5= findw("&allowedTeams",strip(team),' ','i');  
  findwRes6= findw("&allowedTeams",strip(team));

run;

The default behavior expands the values of the variable Team from the data set to the defined length with spaces so the "team" is not found in your target string.

View solution in original post

5 REPLIES 5
robAs
Fluorite | Level 6

Clarification.. I want the NUMBER of the word in the string, not the word itself  🙂

ballardw
Super User

Please very explicitly provide what you expect for at least one observation.

 

Perhaps you want this:

data work.baseballTest (drop=allowedT);
  set sashelp.baseball(keep=team );
  if _n_ < 20;

  allowedT='montreal Cleveland ATLANTA houston Pittsburgh';

  *Various styles of findw None of which appear to work;
  findwRes1 = findw("&allowedTeams",strip(team),' ','ei');  
  findwRes2= findw(allowedT,strip(team),' ','e i');  
  findwRes3= findw("montreal Cleveland ATLANTA houston Pittsburgh",strip(team),' ','e i');  
  findwRes4= findw(upcase("&allowedTeams"),upcase(strip(team)),' ','E');  
  findwRes5= findw("&allowedTeams",strip(team),' ','i');  
  findwRes6= findw("&allowedTeams",strip(team));

run;

The default behavior expands the values of the variable Team from the data set to the defined length with spaces so the "team" is not found in your target string.

robAs
Fluorite | Level 6
Thank you, yes, adding the strip( ) function does the trick in each of the above and now gives the desired results!
Patrick
Opal | Level 21

The following should return what you're after:

  findwRes1 = findw("&allowedTeams",strip(team),' ','ei');  

There is something "weird" with the t modifier. I'm not sure if I'm just missing something or if there is something not working as it should when using the t modifier with a blank separated string. ...if feels as if the t modifier also removes the blank as defined word separator. 

FreelanceReinh
Jade | Level 19

@Patrick wrote:

There is something "weird" with the t modifier. I'm not sure if I'm just missing something or if there is something not working as it should when using the t modifier with a blank separated string. ...if feels as if the t modifier also removes the blank as defined word separator. 


The t modifier does indeed trim the third argument (cf. https://communities.sas.com/t5/SAS-Procedures/FINDW-Function/td-p/171304). The simplest workaround might be to add an "odd" second delimiter to the character list which doesn't occur anywhere else:

findwRes = findw("&allowedTeams",team,' §','eit');

Trimming the second argument with a function is, of course, the cleaner approach.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 282 views
  • 1 like
  • 4 in conversation