xicicora
xicicora

Reputation: 13

Why in SAS data step where works but not if

Working code:

data t2;

 set t1; 
 
 where a like "%SR";

run;

Code errored:

data t2;

 set t1; 

 if a like "%SR";

run;

Error message:
ERROR 388-185: Expecting an arithmetic operator. ERROR 202-322: The option or parameter is not recognized and will be ignored.

It complained about 'like'

Any ideal?

Upvotes: 1

Views: 449

Answers (3)

Richard
Richard

Reputation: 27508

The LIKE operator is not understood by the DATA Step IF statement.

LIKE is available to DATA Step in the WHERE statement, the WHERE= data set option, or PROC SQL WHERE clause.

data have;
  input text $CHAR20.;
  datalines;
ABCEFG
YESSR
Mark JR
Mark SR
;

data want;
  set have;
  where text like '%SR';  /* where statement */
run;

data want;
  set have(where=(text like '%SR')); /* where= option */
run;

proc sql;
  create table want as
  select text from have
  where text like '%SR'     /* where clause */
  ;

Upvotes: 0

Tom
Tom

Reputation: 51566

LIKE is not an operator that SAS code understands. The only reason it works in WHERE is because WHERE statement supports SQL syntax such as LIKE and BETWEEN to make it easier to push the WHERE condition into a remote database.

Use some other way to test if the last two letters are SR. Here are two methods.

if 'SR' = substrn(a,length(a)-1);
if 'RS' =: left(reverse((a)) ;

Upvotes: 3

Joe
Joe

Reputation: 63424

The most similar solution is to use prxmatch:

data t2;
 set t1; 
 if prxmatch("/.*SR/ios",a);
run;

Note that this is much slower than WHERE with LIKE, and Tom's solutions are faster if there's a reasonable way to do them (as there is in the example).

Upvotes: 0

Related Questions