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

DATA TOUNGE;
STRING='How much WOOD would a woodchuck chuck';

num2=find(string,'much','i',-10);

run;

the output of this is 5

but when i use num2=find(string,' wo','i',-10); 

  1. why is the output 0 and not 15? 
  2. when the postion reaches -startposition does it starts counting again from 1 ? and why do we get a positive no. and not negative no in output for position.
1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

Hi @veda8,

 

Start position -10 means:

  1. The search starts at the 10th character of string (counted from the left, as usual). In your example this is the capital W. If lengthc(string)<10, the search would start at the end of string.
  2. The direction of the search is to the left (because of the negative sign).

The result is always the position where the substring starts (counted from the left) or zero if the substring is not found after reaching the end (beginning) of the string in case of a positive (negative) start position.

 

This explains both of your results. In particular, there is no " wo" (or " WO" or " wO" or " Wo") contained in the part "How much W" of string which the search is restricted to, whereas "much" is found in position 5 ff.

 

[Edit: Replaced length with lengthc to be more precise.]

View solution in original post

10 REPLIES 10
veda8
Fluorite | Level 6
 
Tom
Super User Tom
Super User

The result is always a positive number since it is the index into the string for the starting character of the substring it found.

 

When you give a starting position the sign gives the direction and the magnitude gives the starting location.

 

So MUCH starts in position 5.

But ' WO' does NOT exist between position 1 and 10 since the O is in position 11.

veda8
Fluorite | Level 6

Can you please explain what does -10 exactly mean?

Tom
Super User Tom
Super User

The sign of -10 is NEGATIVE and the magnitude is 10.  So you are asking to search from right to left starting with the 10th position.  The substring has to fit completely into the area you are searching. So if the substring you are searching for is 3 bytes long then the first place (largest position) where you could possibly find it is position 8.

veda8
Fluorite | Level 6
okay thank you so much
FreelanceReinh
Jade | Level 19

Hi @veda8,

 

Start position -10 means:

  1. The search starts at the 10th character of string (counted from the left, as usual). In your example this is the capital W. If lengthc(string)<10, the search would start at the end of string.
  2. The direction of the search is to the left (because of the negative sign).

The result is always the position where the substring starts (counted from the left) or zero if the substring is not found after reaching the end (beginning) of the string in case of a positive (negative) start position.

 

This explains both of your results. In particular, there is no " wo" (or " WO" or " wO" or " Wo") contained in the part "How much W" of string which the search is restricted to, whereas "much" is found in position 5 ff.

 

[Edit: Replaced length with lengthc to be more precise.]

veda8
Fluorite | Level 6
thank you so much
is it same for the scan function?
xyz='have a good day';
scan(xyz,-2) what does this do
FreelanceReinh
Jade | Level 19

The logic for SCAN is different: Here the -2 means the second word counted from the right (in your example: "good").

Tom
Super User Tom
Super User

SCAN() is not "searching".  It is indexing.  So for scan negative numbers mean your count from right to left.  If there are 5 "words" in the string then you can get the 4th one using 4 or -2.

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
  • 10 replies
  • 1407 views
  • 4 likes
  • 4 in conversation