DATA Step, Macro, Functions and more

How to use wildcards and like operators for multiple values?

Accepted Solution Solved
Reply
Super Contributor
Posts: 426
Accepted Solution

How to use wildcards and like operators for multiple values?

I would like extract the data like below. But it is creating table with all rows from maps.uscity instead of required records.

It looks I need to tweak the code in like operator to get only the records that has city like below.

proc sql;

create table FI_Locations as select * from maps.uscity where city like  '%Atlanta%' or '%Cary%' or '%Hartford%' ;

quit;


Accepted Solutions
Solution
‎11-14-2014 07:48 AM
Super User
Posts: 9,662

Re: How to use wildcards and like operators for multiple values?

LIKE only can handle one expression one time.

proc sql;

create table FI_Locations as select * from maps.uscity where city like  '%Atlanta%' or  city like '%Cary%' or  city like  '%Hartford%' ;

quit;

Xia Keshan

View solution in original post


All Replies
Super Contributor
Posts: 426

Re: How to use wildcards and like operators for multiple values?

any thoughts?

Trusted Advisor
Posts: 1,141

Re: How to use wildcards and like operators for multiple values?

Hi Babloo,

depending on your database, maybe you can use the "contains" comparator, instead of  "like", then you won;t need the wildcards.

Super Contributor
Posts: 426

Re: How to use wildcards and like operators for multiple values?

I would like to know if i can combine the below data step into one step.

proc sql;

create table FI_Locations_at as select * from maps.uscity where city contains  'Atlanta';

quit;

proc sql;

create table FI_Locations_ca as select * from maps.uscity where city contains  'Cary';

quit;

Solution
‎11-14-2014 07:48 AM
Super User
Posts: 9,662

Re: How to use wildcards and like operators for multiple values?

LIKE only can handle one expression one time.

proc sql;

create table FI_Locations as select * from maps.uscity where city like  '%Atlanta%' or  city like '%Cary%' or  city like  '%Hartford%' ;

quit;

Xia Keshan

Super Contributor
Posts: 336

Re: How to use wildcards and like operators for multiple values?

.. if you need to combine your where-statement with and-statements you might prefer ..

proc sql;

create table FI_Locations as select * from maps.uscity where

  (Case When city like '%Atlanta%' or

             city like '%Cary%' or

             city like '%Hartford%' Then 1

             Else 0

   End)=1;

quit;

☑ This topic is SOLVED.

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

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