I need help writing the case when with multiple conditions to use in 'expression' in DI Job. I'm getting an syntax error if I use below. How to keep adding the conditions in case when? Shouldn't we use 'else' to seperate the condition?
case when ENTITY_ID_SF in ('1000','1111') and SEGMENT='NL' then (sum(GR_INPUT)/(sum(PREM_RES_GR)/3)) end else case when ENTITY_ID_SF in ('1000','1111') and SEGMENT='PL' then (sum(GR_INPUT)/(sum(PREM_RES_GR)/3)) end
Why would you but an ELSE between them? ELSE is for catching all of the observations that do not meet any of the earlier WHEN conditions. If you have multiple WHEN conditions just list then one after the other.
case when ENTITY_ID_SF in ('1000','1111') and SEGMENT='NL' then sum(GR_INPUT)/(sum(PREM_RES_GR)/3)
when ENTITY_ID_SF in ('1000','1111') and SEGMENT='PL' then sum(GR_INPUT)/(sum(PREM_RES_GR)/3)
end
Right now both of your two different conditions will return the same result when they are true. So you could collapse them into one WHEN condition. Like this:
case when (ENTITY_ID_SF in ('1000','1111') and SEGMENT='NL')
or (ENTITY_ID_SF in ('1000','1111') and SEGMENT='PL') then sum(GR_INPUT)/(sum(PREM_RES_GR)/3)
end
or like this:
case when (ENTITY_ID_SF in ('1000','1111') and SEGMENT in ('NL','PL')) then sum(GR_INPUT)/(sum(PREM_RES_GR)/3)
end
But what are you actually trying to do? Did you expect to get different results based on the value of SEGMENT? If so then show some simple input data and what output you want.
try the below code
CASE WHEN condition1 THEN result1 WHEN condition2 THEN result2 WHEN conditionN THEN resultN ELSE result END as variable_name
se
ca
when ENTITY_ID_SF in ('1000','1111') and SEGMENT='NL' then (sum(GR_INPUT)/(sum(PREM_RES_GR)/3)) when ENTITY_ID_SF in ('1000','1111') and SEGMENT='PL' then (sum(GR_INPUT)/(sum(PREM_RES_GR)/3)) else .
end as variable_name
Please. Anytime you get an ERROR, copy the entire procedure with the messages from the LOG and show the code plus the errors. Please.
Include the entire procedure because the cause could be something like a missed or extra quote or parentheses in a previous statement than the place that SAS indicates the error.
Why would you but an ELSE between them? ELSE is for catching all of the observations that do not meet any of the earlier WHEN conditions. If you have multiple WHEN conditions just list then one after the other.
case when ENTITY_ID_SF in ('1000','1111') and SEGMENT='NL' then sum(GR_INPUT)/(sum(PREM_RES_GR)/3)
when ENTITY_ID_SF in ('1000','1111') and SEGMENT='PL' then sum(GR_INPUT)/(sum(PREM_RES_GR)/3)
end
Right now both of your two different conditions will return the same result when they are true. So you could collapse them into one WHEN condition. Like this:
case when (ENTITY_ID_SF in ('1000','1111') and SEGMENT='NL')
or (ENTITY_ID_SF in ('1000','1111') and SEGMENT='PL') then sum(GR_INPUT)/(sum(PREM_RES_GR)/3)
end
or like this:
case when (ENTITY_ID_SF in ('1000','1111') and SEGMENT in ('NL','PL')) then sum(GR_INPUT)/(sum(PREM_RES_GR)/3)
end
But what are you actually trying to do? Did you expect to get different results based on the value of SEGMENT? If so then show some simple input data and what output you want.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Use this tutorial as a handy guide to weigh the pros and cons of these commonly used machine learning algorithms.
Find more tutorials on the SAS Users YouTube channel.