BookmarkSubscribeRSS Feed
AlexeyS
Pyrite | Level 9

Hello, i try to know if specific words are within text. i use function findw.

the problem is that i defined words inside array and when i run the function findw(text,word(i)) i didn't get answer, but when i write specific words by myself, i get right answer. for example findw(text,'sea').

 

data for example is :

          text                       word1    word2    word3

the car is beautiful           best        car         

blue sea                           sea        blue        exist

 

i saw that length,format and informat of array word is length 8, maybe this is problem? Whta i should do?

 

my program look like :

 

data test;

set text;

array word (3) $ word1-word3;

array exist_ind (3);

do i=1 to 3;

    ind(i)=findw(text,word(i));

end;

run;

 

 

2 REPLIES 2
gergely_batho
SAS Employee
You are right: the search is unsuccessful, because in the array "word" the strings are padded with blanks.
You can add the 'r' modifier in the findw function to remove leading and trailing blanks(delimiters):

data text;
infile datalines missover;
input text $20. (word1 word2 word3) (:$8.);
datalines;
the car is beautiful best car
blue sea sea blue exist
;
run;
data test;
set text;
array word (3) $ word1-word3;
array ind (3);
do i=1 to 3;
ind(i)=findw(text,word(i),' ' ,'r' );
end;
run;
Loko
Barite | Level 11

Hello,

 

use the strip function to eliminate blanks from word{*} variables.

 

data want;
set have;
array word (3) $ word1-word3;
array exist_ind (3);

do i=1 to 3;

    exist_ind(i)=findw(text,strip(word(i)));
put _all_;
end;

run;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 2 replies
  • 2268 views
  • 2 likes
  • 3 in conversation