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 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 10 replies
  • 2146 views
  • 4 likes
  • 4 in conversation