BookmarkSubscribeRSS Feed
Ronein
Onyx | Level 15

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;
5 REPLIES 5
Tom
Super User Tom
Super User

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:

  • Use the TRIM(), STRIP() or even CATS() function to removing leading/trailing spaces.
  • Use the T modifier in the FIND() function call.

 

Note: Since you seem to be searching for words you might want to use the FINDW() function instead.

 

PaigeMiller
Diamond | Level 26

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.

 

--
Paige Miller
Ksharp
Super User
As Tom pointed out ,using strip to get rid of trailing blanks:
pos1 = find(string, strip( char_substring ));

Otherwise ,sas would take 3000 as 3000blankblank..... Not 3000
PaigeMiller
Diamond | Level 26

In my opinion, any solution involving the FIND function will fail for the reasons I gave above. You really want the FINDW function.

 

But to go a step beyond the actual code, best practices are to treat numbers as numeric variables, and not as a character string that you have to pull apart somehow. So even if @Ronein received the data as a character string, they should be converted to numbers for any future use, including logical testing like comparing to the number 30000. If these are numbers and not character strings, then these questions about how to find 30000 never arise.

--
Paige Miller
Ksharp
Super User
Yeah. Agree.
It is all depend on what OP want. This example maybe is just an example, only OP know the real demand and real data .

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

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!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 5 replies
  • 211 views
  • 0 likes
  • 4 in conversation