Hello
I want to check if string contain substring.
I do it with Find function.
What is the reason that example 1 is working 100% but example 2 is not working well?
30000 exist in 40000,80000,30000
data example1_find;
string = "40000,80000,30000";
substring=30000;
char_substring=compress(put(substring,best.));
pos1 = find(string,char_substring);
IF POS1>0 then ind=1;else ind=0;
run;
data example2_find;
string = "I Love- Banana";
substring='Banana';
pos1 = find(string,substring);
IF POS1>0 then ind=1;else ind=0;
run;
Read the documentation on the FIND() function, in particular look at the various modifiers that it offers.
Your first example does not work because of the lengths of your character variables. Since you did not tell SAS explicitly what length to use for them it followed its internal rules for guessing what length to use. So STRING is defined as $17 since the first value you assigned to it was 17 bytes long. And CHAR_SUBSTRING was defined as $12 since the default width for the BEST format is 12. So that means FIND() was unable to located a 3 followed by 4 zeros and 7 spaces in "40000,80000,30000".
Here are some things you could try:
Note: Since you seem to be searching for words you might want to use the FINDW() function instead.
How about this? It works! If you want to compare string to another character string, don't make the variable numeric and then convert it to a character string. As @Tom points out, this can be a problem. Make the variable character to begin with.
data example1_find;
string = "40000,80000,30000";
substring='30000';
ind = find(string,substring)>0;
run;
... but wait, that fails to give the correct answer when string = "40000,80000,330000"
So, using FINDW as suggested by @Tom is definitely the way to go.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.