BookmarkSubscribeRSS Feed
Angel_Saenz
Quartz | Level 8

I need to compare data to one variable to modify other.

For example:

data data2;

set data1;

IF VAR1 = 0 THEN VAR2 ='STATUS0';

RUN;

Last sas code works the but not like when I need to compare a string

 

data data2;

set data1;

IF VAR1 like '%zero%' THEN VAR2 ='STATUS0';

RUN;

this code does not work because IF command does not recognize the LIKE function

IF VAR1 like 0 THEN VAR2 ='STATUS0';

             ____

ERROR 388-185: Expecting an arithmetic operator.

 

 

what can I do?

4 REPLIES 4
Reeza
Super User

LIKE is not a valid SAS function, except in WHERE or SQL.

 

Look at using either the FINDW or INDEXW functions instead that will search the string for zero. Note that the search is case sensitive so you may want to use a modifier that ignores case.

 

PGStats
Opal | Level 21

Or use SQL :

 

proc sql;
create table data2 as
select VAR1, .... ,
	case when VAR1 like '%zero%' THEN 'STATUS0'
	else VAR2 end as VAR2
from DATA1;
quit;
PG
PGStats
Opal | Level 21

Or use two WHERE clauses in a data step

 

data data2;
set DATA1 (where=(VAR1 like '%zero%') in=isZero)
	DATA1 (where=(VAR1 not like '%zero%'));
if isZero then VAR2 = 'STATUS0';
run;

(untested)

PG
Kurt_Bremser
Super User
ERROR 388-185: Expecting an arithmetic operator

makes me think that your variable VAR1 is a numeric variable, and therefore using the like operator (which is for character values) makes no sense.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 803 views
  • 1 like
  • 4 in conversation