- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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);
- why is the output 0 and not 15?
- 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.
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hi @veda8,
Start position -10 means:
- 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.
- 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.]
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Please do not post the same question multiple times (double-post).
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Can you please explain what does -10 exactly mean?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hi @veda8,
Start position -10 means:
- 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.
- 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.]
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
is it same for the scan function?
xyz='have a good day';
scan(xyz,-2) what does this do
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
The logic for SCAN is different: Here the -2 means the second word counted from the right (in your example: "good").
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.