Help using Base SAS procedures

Using the contains condition

Reply
Super Contributor
Posts: 279

Using the contains condition

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.

PROC Star
Posts: 7,467

Using the contains condition

Gregg,  Are you possibly looking for something like:

data want;

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

run;

Super Contributor
Posts: 279

Using the contains condition

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?

PROC Star
Posts: 7,467

Using the contains condition

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

PROC Star
Posts: 7,467

Using the contains condition

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;

Valued Guide
Posts: 765

Re: Using the contains condition

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


Super User
Posts: 10,020

Re: Using the contains condition

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

Frequent Contributor
Posts: 88

Using the contains condition

It's perfectly good and thanks so much

Ask a Question
Discussion stats
  • 7 replies
  • 223 views
  • 0 likes
  • 5 in conversation