Solved
Posts: 2,983

# Need Help Understanding part of FIND command

According to the documentation, the FIND command in this code returns a value of 14

xyz='She sells seashells? Yes, she does.';
startposexp=1-23;
whereisShe_ineg22=find(xyz,'She','i',startposexp);
put whereisShe_ineg22;

And I'm probably missing something obvious. The value of startposexp=–22, and the documentation further states that when the startpos argument of the FIND function is less than zero, "starts the search at position –start-position and the direction of the search is to the left."

Okay, so by my count, position –22 of string xyz is the second letter "s" of seashells. If you then search TO THE LEFT (which is what the documentation says), I can't understand how the result is 14. To me, the search ought to result in an answer of 1, if you go left from the second letter "s" of seashells, and search TO THE LEFT for the word 'She" ignoring case, you find it at position 1 of xyz.

But apparently, I am misunderstanding, and the answer is 14.  Can someone please explain this? Thanks.

--
Paige Miller

Accepted Solutions
Solution
‎10-17-2014 10:13 AM
Super Contributor
Posts: 319

## Re: Need Help Understanding part of FIND command

Hello,

The documentation says:

If startpos is not specified, FIND starts the search at the beginning of the string and searches the string from left to right. If startpos is specified, the absolute value of startpos determines the position at which to start the search. The sign of startpos determines the direction of the search.

Therefore the position is always counted from left to right .

All Replies
Super Contributor
Posts: 319

## Re: Need Help Understanding part of FIND command

Hello,

You have to count from left to right. The minus sign change the behaviour (it searches from right to left), but the starting point is still determined from left to right. SO in your case -22 means the : Starting from the Y (from Yes) searcha to the left therefore the answer is 14.

COnsider the following:

data _null_;

xyz='She sells seashells? Yes, she does.';

startposexp=1-23;

whereisShe_ineg17=find(xyz,'She','i',-17);

whereisShe_ineg14=find(xyz,'She','i',-14);

put whereisShe_ineg17= whereisShe_ineg14=;

run;

whereisShe_ineg17=14 whereisShe_ineg14=1

Posts: 2,983

## Re: Need Help Understanding part of FIND command

```the starting point is still determined from left to right
```

This would seem to give the correct explanation, but why then would the SAS documentation specifically point out that if startpos > 0 then the search "starts the search at position start-position" while if startpos<0 then the search "starts the search at position –start-position"? These are saying different things about where the search starts.

Does this all boil down to incorrect (or confusing) documentation?

--
Paige Miller
Super User
Posts: 10,766

## Re: Need Help Understanding part of FIND command

Hi. I am afraid sas documentation is right . You should count it in this way :

She sells seashells? Yes, she doe s .

^                              ^  ^

-22                            -2 -1

so search it from left, the first letter to left is S

Xia Keshan

Posts: 2,983

## Re: Need Help Understanding part of FIND command

Thanks , but I just am not understanding what the FIND command is doing in this case. Your explanation hasn't helped me.

I don't see how starting from the second "s" in seashells (which I agree is at position -22), you search to the left and find "she" (ignoring case) at position 14. From that letter "s" at position -22 and going left, you don't come to the word "she" ignoring case, until you get to the very first letter of xyz.

To make everything crystal clear, here is the text string, counting letters forward and backward, show me how you get an answer of 14.

letter=S pos=1 backwards=-35

letter=h pos=2 backwards=-34

letter=e pos=3 backwards=-33

letter=  pos=4 backwards=-32

letter=s pos=5 backwards=-31

letter=e pos=6 backwards=-30

letter=l pos=7 backwards=-29

letter=l pos=8 backwards=-28

letter=s pos=9 backwards=-27

letter=  pos=10 backwards=-26

letter=s pos=11 backwards=-25

letter=e pos=12 backwards=-24

letter=a pos=13 backwards=-23

letter=s pos=14 backwards=-22

letter=h pos=15 backwards=-21

letter=e pos=16 backwards=-20

letter=l pos=17 backwards=-19

letter=l pos=18 backwards=-18

letter=s pos=19 backwards=-17

letter=? pos=20 backwards=-16

letter=  pos=21 backwards=-15

letter=Y pos=22 backwards=-14

letter=e pos=23 backwards=-13

letter=s pos=24 backwards=-12

letter=, pos=25 backwards=-11

letter=  pos=26 backwards=-10

letter=s pos=27 backwards=-9

letter=h pos=28 backwards=-8

letter=e pos=29 backwards=-7

letter=  pos=30 backwards=-6

letter=d pos=31 backwards=-5

letter=o pos=32 backwards=-4

letter=e pos=33 backwards=-3

letter=s pos=34 backwards=-2

letter=. pos=35 backwards=-1

--
Paige Miller
Solution
‎10-17-2014 10:13 AM
Super Contributor
Posts: 319

## Re: Need Help Understanding part of FIND command

Hello,

The documentation says:

If startpos is not specified, FIND starts the search at the beginning of the string and searches the string from left to right. If startpos is specified, the absolute value of startpos determines the position at which to start the search. The sign of startpos determines the direction of the search.

Therefore the position is always counted from left to right .

Posts: 2,983

## Re: Need Help Understanding part of FIND command

Yes , I think that is the piece of information I overlooked.

To confirm, I ran the following code:

data _null_;

file log;

xyz='She sells seashells? Yes, she does.';

length letter \$ 3;

do pos=1 to length(xyz);

backwards=pos-36;

letter=substr(xyz,pos,1);

result=find(xyz,'She','i',backwards);

put letter= pos= backwards= result=;

end;

run;

which gives the following result, completely consistent with the documentation, especially now that you have pointed out the information that I was overlooking

letter=S pos=1 backwards=-35 result=27

letter=h pos=2 backwards=-34 result=27

letter=e pos=3 backwards=-33 result=27

letter=  pos=4 backwards=-32 result=27

letter=s pos=5 backwards=-31 result=27

letter=e pos=6 backwards=-30 result=27

letter=l pos=7 backwards=-29 result=27

letter=l pos=8 backwards=-28 result=14

letter=s pos=9 backwards=-27 result=14

letter=  pos=10 backwards=-26 result=14

letter=s pos=11 backwards=-25 result=14

letter=e pos=12 backwards=-24 result=14

letter=a pos=13 backwards=-23 result=14

letter=s pos=14 backwards=-22 result=14

letter=h pos=15 backwards=-21 result=14

letter=e pos=16 backwards=-20 result=14

letter=l pos=17 backwards=-19 result=14

letter=l pos=18 backwards=-18 result=14

letter=s pos=19 backwards=-17 result=14

letter=? pos=20 backwards=-16 result=14

letter=  pos=21 backwards=-15 result=1

letter=Y pos=22 backwards=-14 result=1

letter=e pos=23 backwards=-13 result=1

letter=s pos=24 backwards=-12 result=1

letter=, pos=25 backwards=-11 result=1

letter=  pos=26 backwards=-10 result=1

letter=s pos=27 backwards=-9 result=1

letter=h pos=28 backwards=-8 result=1

letter=e pos=29 backwards=-7 result=1

letter=  pos=30 backwards=-6 result=1

letter=d pos=31 backwards=-5 result=1

letter=o pos=32 backwards=-4 result=1

letter=e pos=33 backwards=-3 result=1

letter=s pos=34 backwards=-2 result=0

letter=. pos=35 backwards=-1 result=0

--
Paige Miller
Super User
Posts: 10,766

## Re: Need Help Understanding part of FIND command

Hi. I might not explain more clear.

She sells seashells? Yes, she doe s .

^                              ^  ^

-22                            -2 -1

So search from left the first letter is S , and found she at that position. But this position you should count it from right .

in other words, from the right to count it until she . you will get number 14 .

Xia Keshan

🔒 This topic is solved and locked.