BookmarkSubscribeRSS Feed
GreggB
Pyrite | Level 9

is there a more efficient way to use contains than this: where classname contains 'ALGE' or classname contains 'ADV MATH' or classname contains 'MATH' or classname contains 'GEOME';

I tried where classname contains ('ALGE, 'ADV MATH etc) but i get an error.

7 REPLIES 7
art297
Opal | Level 21

Gregg,  Are you possibly looking for something like:

data want;

  set sashelp.class (where=(name in: ('A','H')));

run;

GreggB
Pyrite | Level 9

does that mean it would capture any class with 'A' or 'H' in the string or would it only get those that begin with A or H?

art297
Opal | Level 21

Only begins with.  I apparently misunderstood what you were looking for.

art297
Opal | Level 21

Does it have to be in a where option?  Would the following meet what you want?

data have;

  informat ClassName $20.;

  input ClassName &;

  cards;

BASIC ALGEBRA

ALGEBRA II

BASIC MATH

NON ADV MATH

HISTORY

SCIENCE

;

data want;

  set have;

  retain pattern;

  if _n_ eq 1 then pattern = prxparse("/ALGE|ADV MATH/");

  if prxmatch(pattern,ClassName);

run;

MikeZdeb
Rhodochrosite | Level 12

Hi ... you need repeats of CONTAINS ...

data x;

input classname $30.;

datalines;

ENGLISH

ALGEBRA

ADV MATH

GEOMETRY

PHYSICS

WHAT'S IT ALL ABOUT ALGE

ADV MATERIALS

;

run;

data y;

set x;

where classname contains 'ALGE'

      or

      classname contains 'ADV MAT';

run;


classname

ALGEBRA

ADV MATH

WHAT'S IT ALL ABOUT ALGE

ADV MATERIALS


Ksharp
Super User

You can make a dataset to hold these index key, then use Cartesian Product of SQL.

NOTE: don't forget to use strip() function for index key.

data x;
input classname $30.;
datalines;
ENGLISH
ALGEBRA
ADV MATH
GEOMETRY
PHYSICS
WHAT'S IT ALL ABOUT ALGE
ADV MATERIALS
;
run;
data index;
input key $10.;
cards;
ALGE
ADV MAT
;
run;
proc sql;
 select classname
  from x,index
   where classname contains strip(key);
quit;

Ksharp

Jack2012
Obsidian | Level 7

It's perfectly good and thanks so much

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 7 replies
  • 2009 views
  • 0 likes
  • 5 in conversation