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);
Hi @veda8,
Start position -10 means:
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.]
Please do not post the same question multiple times (double-post).
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.
Can you please explain what does -10 exactly mean?
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.
Hi @veda8,
Start position -10 means:
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.]
The logic for SCAN is different: Here the -2 means the second word counted from the right (in your example: "good").
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 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.