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.
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
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.
String1 being searched is '9999 CK APPROVED AT A FIXED PAY PROG'.
For FINDW Modifier 'i' or 'I' ignores the case of the characters.
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.
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.
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.
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.
Then in the FINDW function, should you replace WORD1 with strip(WORD1)?
even when I define Word1??
If Index(UpCase(mlr_data), 'FIXED') > 0 Then Word1 = 'FIXED';
Thanks to your LENGTH statement, there will be 15 trailing blanks after "FIXED".
Or add the R modifier to the last argument of FINDW.
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
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
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!!!
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!!!
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.
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.