## Finding the word number of a list of abbreviations.

Solved
Occasional Contributor
Posts: 11

# Finding the word number of a list of abbreviations.

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?

Accepted Solutions
Solution
‎05-11-2016 05:54 PM
Posts: 1,246

## Re: Finding the word number of a list of abbreviations.

```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```

All Replies
Super User
Posts: 13,523

## Re: Finding the word number of a list of abbreviations.

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.

Occasional Contributor
Posts: 11

## Re: Finding the word number of a list of abbreviations.

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

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

Posts: 1,246

## Re: Finding the word number of a list of abbreviations.

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.

Occasional Contributor
Posts: 11

## Re: Finding the word number of a list of abbreviations.

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.

Super User
Posts: 6,762

## Re: Finding the word number of a list of abbreviations.

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.

Occasional Contributor
Posts: 11

## Re: Finding the word number of a list of abbreviations.

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.

Super User
Posts: 6,762

## Re: Finding the word number of a list of abbreviations.

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

Occasional Contributor
Posts: 11

## Re: Finding the word number of a list of abbreviations.

even when I define Word1??

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

Posts: 1,246

## Re: Finding the word number of a list of abbreviations.

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

Posts: 1,246

## Re: Finding the word number of a list of abbreviations.

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

Occasional Contributor
Posts: 11

## Re: Finding the word number of a list of abbreviations.

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

Solution
‎05-11-2016 05:54 PM
Posts: 1,246

## Re: Finding the word number of a list of abbreviations.

```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```
Occasional Contributor
Posts: 11

## Re: Finding the word number of a list of abbreviations.

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!!!

Occasional Contributor
Posts: 11

## Re: Finding the word number of a list of abbreviations.

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!!!

🔒 This topic is solved and locked.