BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ubshams
Quartz | Level 8

Hello,
I am trying to use the “if” and “like” statement to create a new column called “new_column” based on the character values of “column1”. But I get an error (see below). I am trying to use the “Like” statement but does not seem to work with “if” statement.
Please advice  on an alternative approach. Thanks!

Column1 is a character column and I am trying to create another character column called “column1” based on column1 values values.

 

---current code---
data want;
set have;
if upcase(column1) like "SPEC%"
then new_column = "Specialty";
run;

 

 

I get the following error:

Syntax error, expecting one of the following: !, !!, &, *, **, +, -, /, ;, <, <=, <>, =, >, ><, >=, AND, EQ, GE, GT,
2 The SAS System 11:46 Friday, April 3, 2020

IN, LE, LT, MAX, MIN, NE, NG, NL, NOT, NOTIN, OR, THEN, ^, ^=, |, ||, ~, ~=.

ERROR 76-322: Syntax error, statement will be ignored.

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

@ubshams wrote:

Thanks. But the values in  column1 are like this:

Text1_Text2_Text3_Spec_Text5_Text6

Text1_Text2_Text3_Spec2_Text5_Text6

...

Text1_Text2_Text3_Spec6_Text5_Text6

 

So the ":=" is usfeful, but not in this case. Is there a CONTAINS OR LIKE equivalent I can use with an "IF" statement to create a new column? 

 


You may need to actually provide real, or more realistic example values. Your initial LIKE example would "work" only if the string starts with "SPEC".

data have;
    input column1 $;
datalines;
Text1
Spec_
Cspec
x_SPEC_
;
run;
data want;
   set have;
   where upcase(column1) like "SPEC%";
   new_column = "Specialty";
run;

Only the value that starts with Spec is kept.

Which is why I translated your example like to =: .

 

 

View solution in original post

6 REPLIES 6
PaigeMiller
Diamond | Level 26

LIKE works in PROC SQL. It does not work in a DATA step.

 

To fix this, try

data want;
set have;
if upcase(column1) =: "SPEC" then new_column = "Specialty";
run;

The =: operator (note the colon after the equal sign) finds cases where upcase(column1) starts with SPEC

 

In the future, please do not disconnect the log from the code. Copy the log (including the code and the warnings and error messages) with nothing chopped out and paste it into the window that appears when you click on the </> icon. This preserves the formatting of the log and makes it more readable and more meaningful. DO NOT SKIP THIS STEP.

 

--
Paige Miller
ballardw
Super User

LIKE can only be used in a Where expression in a data step.

 

Example:

data want;
   set sashelp.class;
   where name like 'J%';
run;

You might try

data want;
   set have;
   if upcase(column1) =: "SPEC"
       then new_column = "Specialty";
run;

The =: is "begins with".

 

There are also number of other functions to search text like FIND, FINDW, INDEX, INDEXW and the PRX functions.

 

ubshams
Quartz | Level 8

Thanks. But the values in  column1 are like this:

Text1_Text2_Text3_Spec_Text5_Text6

Text1_Text2_Text3_Spec2_Text5_Text6

...

Text1_Text2_Text3_Spec6_Text5_Text6

 

So the ":=" is usfeful, but not in this case. Is there a CONTAINS OR LIKE equivalent I can use with an "IF" statement to create a new column? 

 

 

 

 

 

 

ballardw
Super User

@ubshams wrote:

Thanks. But the values in  column1 are like this:

Text1_Text2_Text3_Spec_Text5_Text6

Text1_Text2_Text3_Spec2_Text5_Text6

...

Text1_Text2_Text3_Spec6_Text5_Text6

 

So the ":=" is usfeful, but not in this case. Is there a CONTAINS OR LIKE equivalent I can use with an "IF" statement to create a new column? 

 


You may need to actually provide real, or more realistic example values. Your initial LIKE example would "work" only if the string starts with "SPEC".

data have;
    input column1 $;
datalines;
Text1
Spec_
Cspec
x_SPEC_
;
run;
data want;
   set have;
   where upcase(column1) like "SPEC%";
   new_column = "Specialty";
run;

Only the value that starts with Spec is kept.

Which is why I translated your example like to =: .

 

 

DavePrinsloo
Pyrite | Level 9

As mentioned by others use a function like FIND or INDEX and for more complex logic you could use PRXMATCH

data want;
set have;
if find(upcase(column1),"SPEC")
then new_column = "Specialty";
run;

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 6 replies
  • 1485 views
  • 2 likes
  • 5 in conversation