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

Hello,

I want to understand starting position parameter in find function.

data test;
 xyz='She sells seashells? Yes, she does.';                                                                                             
   startposexp=-22;                                                                                                                      
   whereisShe_ineg22=find(xyz,'She','i',startposexp);                                                                                     
   put whereisShe_ineg22=;  
run;

In this example whereisShe_ineg22=14, Why?

As i understand, because the start position is negative, we start at the end of the string and count 22 places, and get the character s. Then the function start search to left, and to right. Why?  please see 

https://documentation.sas.com/?docsetId=lefunctionsref&docsetTarget=p00ab6ey29t2i8n1ihel88tqtga9.htm... 

 

Thank you

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Your second description is right.

Absolute value of POSITION determines where to start the search.  SIGN of position determines the direction to search. The result is always location when counting from left, and is always positive. (or zero when not found).

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26

The -22 tells SAS to begin the search at the 22nd character starting from the right, which is the first s of "seashells". It then finds the string 'She' (ignoring case) beginning at character 14 of the string (which is in the inside of the word "seashells").

--
Paige Miller
Tom
Super User Tom
Super User

You are not understanding how it uses the absolute value when the starting position is negative.

The first place where 'she' appears when searching backwards from byte 22 is byte 14.

Does this picture help?

She sells seashells? Yes, she does.
----+----0----+----0----+----0----+
             ^       ^
             |       +-> Position 22
             +> Position 14 

actually if you change the search string from SHE to YES you can see that appear to behave as if it truncated the string at that position and then searches from the end of the truncated string backwards.  With a starting position of 24 or larger it finds YES when searching backwards.  But it does not find it when starting in position 22 or 23.

AlexeyS
Pyrite | Level 9
Hi Tom, i am still confused.
This definition i read in sas page : start-position is specified, the absolute value of start-position determines the position at which to start the search. The sign of start-position determines the direction of the search.

I have to solution, which one is correct?
1. because start position is negative, i find position 22 from right to left, and then the search is standard from left to right?
2. to find start position i always use absolute value, i.e i find 22 position from left to right, and then because the direction is negative, i will look for the first occurrence of "she" from right to left?

Thank you
Tom
Super User Tom
Super User

Your second description is right.

Absolute value of POSITION determines where to start the search.  SIGN of position determines the direction to search. The result is always location when counting from left, and is always positive. (or zero when not found).

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 4 replies
  • 1194 views
  • 1 like
  • 3 in conversation