Using wildcard characters with the LIKE() function.

Accepted Solution Solved
Reply
New Contributor
Posts: 2
Accepted Solution

Using wildcard characters with the LIKE() function.

Hi,

I'm fairly new to SAS and I am having trouble using the wildcard character % in a like function (as part of an SQL WHERE clause) when referencing a specific field.  I am searching for one string within another and in other languages I would have the syntax similar to

WHERE a.Target_String LIKE %b.Search_String%

I've tried various combinations of quotes but I can not get this to work.  In the end I have used the find function instead but it's not a concise.

Can anyone tell me if there is a way for this to work and give an example?

Thanks


Accepted Solutions
Solution
‎05-29-2013 07:01 AM
Super User
Super User
Posts: 7,076

Re: Using wildcard characters with the LIKE() function.

If the string with wildcards was a constant you would do:

WHERE a.Target_String LIKE '%FRED%'

So if the string is within another variable you have two choices.

1) put the wildcards in the value when you create the other variable

WHERE a.Target_String LIKE b.search_string

2) build the search string you want from the variable content and constant text.

WHERE a.Target_String LIKE cats('%',b.search_string,'%')


View solution in original post


All Replies
Solution
‎05-29-2013 07:01 AM
Super User
Super User
Posts: 7,076

Re: Using wildcard characters with the LIKE() function.

If the string with wildcards was a constant you would do:

WHERE a.Target_String LIKE '%FRED%'

So if the string is within another variable you have two choices.

1) put the wildcards in the value when you create the other variable

WHERE a.Target_String LIKE b.search_string

2) build the search string you want from the variable content and constant text.

WHERE a.Target_String LIKE cats('%',b.search_string,'%')


New Contributor
Posts: 2

Re: Using wildcard characters with the LIKE() function.

Thanks very much Tom, number 2 is exactly what I need.

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 3494 views
  • 0 likes
  • 2 in conversation