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

I have five diferent words that can have up to five different abreviations that I need to search in String1. Here is an example of 2 of those words that I am searching for:

 

<Inside a Data Step>

IndexFixed = Index(UpCase(String1), 'FIXED');
If Index(UpCase(String1), 'FIXED') > 0 Then Word1 = 'FIXED';
If Index(UpCase(String1), 'FIXD') > 0 Then Word1 = 'FIXD';
If Index(UpCase(String1), 'FXD') > 0 Then Word1 = 'FXD';
If Not Missing(Word1) Then Pos1 = FINDW(String1, Word1,' ','I E');

 

If Index(UpCase(String1), 'PMT') > 0 Then Word2 = 'PMT';
If Index(UpCase(String1), 'PYMNT') > 0 Then Word2 = 'PYMNT';
If Index(UpCase(String1), 'PMNT') > 0 Then Word2 = 'PMNT';
If Index(UpCase(String1), 'PAY') > 0 Then Word2 = 'PAY';
If Index(UpCase(String1), 'PMNT') > 0 Then Word2 = 'PAYMT';

If Index(UpCase(String1), 'PAYMENT') > 0 Then Word2 = 'PAYMENT';
If Not Missing(Word2) Then Pos2 = FINDW(String2, Word2,' ','E I');

This is repeated for the other 3 words and their abreviations.

 

I am finding that only one of the FINDW generated a number to be run. (Pos1-Pos5 only have on value with a number while the other four are zero).

Word1    Word2    Word3    Word4          Word5    Pos1    Pos2    Pos3    Pos4    Pos5

FIXED    PAY        PROG    APPR                           0          0          8          0          0

Why does FINDW only work once for a given observation?

 

Thank you for your help.

 

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19
1479  data _null_;
1480  length word $20;
1481  retain String1 '9999 CK APPROVED FOR A FIXED PAY PROG';
1482  input word;
1483  pos=findw(string1, word, ' ', 'EIR');
1484  put word 'was found in position ' pos;
1485  cards;

APPROVED was found in position 3
FIXED was found in position 6
PAY was found in position 7
PROG was found in position 8

View solution in original post

16 REPLIES 16
ballardw
Super User

You should provide some examples of the values of STRING1 and STRING2 to determine exactly why.

Likely though is that FINDW(String1, Word1,' ','I E'); doesn't use the UPCASE(string1) that was used in the INDEX function.

 

Lochdonan
Fluorite | Level 6

String1 being searched is '9999 CK APPROVED AT A FIXED PAY PROG'.

For FINDW Modifier 'i' or 'I' ignores the case of the characters.

FreelanceReinh
Jade | Level 19

Hi @Lochdonan,

 

It seems odd that you search (the upcased version of) String1 for 'PMT' etc. and then expect to find the word in String2.

Lochdonan
Fluorite | Level 6

That is me trying several things (Copying the exact same string to string1 and string2 to find if FINDW works on seperate string variables). Originally I was using the original string and was finding the same results.

Astounding
PROC Star

Here are a couple of things to consider.

 

First, only blanks are delimiters.  So if you have this:

 

"My problem is fixed.  Thank you."

 

INDEX will find FIXED, but FINDW will not since it is not delimited by blanks.

 

Second, unless the length of WORD2 is otherwise defined earlier, it will have a length of 3 (not enough characters to hold some of the values such as "PYMNT" and "PMNT").  For those cases, FINDW may not find the abbreviated three-character version as a separate word.

Lochdonan
Fluorite | Level 6

I do have:

Length Word1 Word2 Word3 Word4 Word5 $20 Pos1 Pos2 Pos3 Pos4 Pos5 Score 8;

earlier. My bad for not including that in the code example.

Astounding
PROC Star

Then in the FINDW function, should you replace WORD1 with strip(WORD1)?

Lochdonan
Fluorite | Level 6

even when I define Word1??  

If Index(UpCase(mlr_data), 'FIXED') > 0 Then Word1 = 'FIXED';

FreelanceReinh
Jade | Level 19

Thanks to your LENGTH statement, there will be 15 trailing blanks after "FIXED".

FreelanceReinh
Jade | Level 19

Or add the R modifier to the last argument of FINDW.

Lochdonan
Fluorite | Level 6

I did try that and got the same result. I would challenge everyone to try this:
Create a table with one record of Sting1 with value '9999 CK APPROVED FOR A FIXED PAY PROG' and then get the word position of 3 of the words
for example
APPROVED at word 3
FIXED at word 6
PAY at word 7
PROG at word 8

That is what I am trying to accomplish. If there is code that can do this... I would be very appreciative.

Thanks again,
Lochdonan

FreelanceReinh
Jade | Level 19
1479  data _null_;
1480  length word $20;
1481  retain String1 '9999 CK APPROVED FOR A FIXED PAY PROG';
1482  input word;
1483  pos=findw(string1, word, ' ', 'EIR');
1484  put word 'was found in position ' pos;
1485  cards;

APPROVED was found in position 3
FIXED was found in position 6
PAY was found in position 7
PROG was found in position 8
Lochdonan
Fluorite | Level 6

The R worked... I don't know why. I had done it before but obviously I had done it wrong because adding the R work.

 

THANK YOU SO MUCH!!!

Lochdonan
Fluorite | Level 6

The R worked... I don't know why. I had done it before but obviously I had done it wrong because adding the R work.
 
THANK YOU SO MUCH!!!

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 16 replies
  • 1464 views
  • 3 likes
  • 4 in conversation