DATA Step, Macro, Functions and more

How can I compare data from one variable to modify other in a data step?

Reply
Contributor
Posts: 43

How can I compare data from one variable to modify other in a data step?

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?

Super User
Posts: 19,768

Re: How can I compare data from one variable to modify other in a data step?

Posted in reply to Angel_Saenz

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.

 

Respected Advisor
Posts: 4,919

Re: How can I compare data from one variable to modify other in a data step?

Posted in reply to Angel_Saenz

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
Respected Advisor
Posts: 4,919

Re: How can I compare data from one variable to modify other in a data step?

Posted in reply to Angel_Saenz

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
Super User
Posts: 7,758

Re: How can I compare data from one variable to modify other in a data step?

Posted in reply to Angel_Saenz
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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Ask a Question
Discussion stats
  • 4 replies
  • 274 views
  • 1 like
  • 4 in conversation