Desktop productivity for business analysts and programmers

Case Statement Like With Multiple Values

Accepted Solution Solved
Reply
Contributor
Posts: 64
Accepted Solution

Case Statement Like With Multiple Values

Hi all,

 

In a case statement, I have multiple values I was to catch in a url code. Since the full urls vary, but with a few strings that are consistently embedded within it, I want to catch them all by asking for like '%string%'. However, I don't know how to do that within a case statement and with multiple possibilities. 

 

case when activity_id = 2 and link_name like in ('%CUSTSERV%' , '%CUSTOMER%', '%SERVICE%') then 1 else 0 end as cust_serv,

Thanks for the help ahead of time. 


Accepted Solutions
Solution
‎01-12-2018 11:53 AM
Super User
Posts: 2,061

Re: Case Statement Like With Multiple Values

case when activity_id = 2 and (link_name like  '%CUSTSERV%' or link_name like '%CUSTOMER%' or link_name '%SERVICE%' ) then 1 else 0 end as cust_serv

View solution in original post


All Replies
Super User
Posts: 13,941

Re: Case Statement Like With Multiple Values

It never hurts to provide some example data in the form of a data step that demonstrates what you have and then what you want the result for that data to be.

 

It may be that a data step is more flexible for multiple OR comparisons. I don't think "in" operator is going to work the way you are thinking.

Solution
‎01-12-2018 11:53 AM
Super User
Posts: 2,061

Re: Case Statement Like With Multiple Values

case when activity_id = 2 and (link_name like  '%CUSTSERV%' or link_name like '%CUSTOMER%' or link_name '%SERVICE%' ) then 1 else 0 end as cust_serv

Contributor
Posts: 64

Re: Case Statement Like With Multiple Values

Posted in reply to novinosrin

This worked, thanks!

Super Contributor
Super Contributor
Posts: 269

Re: Case Statement Like With Multiple Values

[ Edited ]

Just for completeness since OP 1) did not post data, 2) did not provide a lot of details, and 3) those playing along at home may have trouble following things:

 

data records;
   input id activity_id link_name:$200.;
datalines;
1105 1 whodoo_whoodoo_customer.asp
1106 1 whodate_customer_dare.html
1107 1 nothing_here_move_along.css
1108 2 has_a_target_customer_word.html
1109 2 does_not.html
1110 2 service.html
1111 2 this_is_custservice_now.html
1112 2 just_ignore_me
;
run;

proc sql;
	create table customer_ones as
	select 
		id,
		case 
			when activity_id = 2 and (link_name like '%custserv%' or link_name like '%customer%' or link_name like '%service%') 
			then 1 
			else 0 
		end as cust_serv
	from records;
quit;

 

And a note that it is case sensitive so that %CUSTOMER% is different than %customer%.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 1012 views
  • 4 likes
  • 4 in conversation