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

I'm doing a case when but I'm getting a NOTE that Case has no ELSE clause.  Should I be using "AND" instead of "THEN" in certain places? Do I have too many END statements?  Any help is appreciated.

92    proc sql;
93         create table work.short2 as
94            select a.*,
95    case when missing(a.date)then .
96         when a.date<="05may2024"d then
97              case when a.m_score >= 0.13 then 1 else 0 end
98    else case when a.date >="06may2024"d then case when
99    a.Days >= 20 then 1 else 0 end
100   end
101   end as T_Flag
102              from short a
103                 left join b.rti b
104                    on a.id= b.id and
105                       a.sflag = b.sflag;
NOTE: A CASE expression has no ELSE clause. Cases not accounted for by the WHEN clauses will result in a missing value for the CASE 
      expression.
1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Proper code formatting will quickly reveal where the ELSE is missing:

case
  when missing(a.date)
then . when a.date <= "05may2024"d then case when a.m_score >= 0.13 then 1 else 0 end else case /* CASE starts here */ when a.date >= "06may2024"d then case when a.Days >= 20 then 1 else 0 end end /* case ends here, but no ELSE after WHEN/THEN */ end as T_Flag

Writing spaghetti code is always bad and makes code unmaintainable. DO NOT DO IT.

View solution in original post

5 REPLIES 5
quickbluefish
Barite | Level 11

 

 

I suspect you could do something much simpler than this. I think it would be helpful if you first wrote out what you want in plain English (i.e., not code) - better yet, draw it (like a tree diagram).   I have literally never seen that degree of nesting / daisy-chaining with CASE statements.  

 

 

 

yabwon
Onyx | Level 15
/* fake data */
libname b (work);
data short b.rti(keep=id sflag);
  id = 1;
  sflag = 2;
  date = "06may2024"d;
  days = 42;
  m_score = 0.16;
run;


proc sql;
create table work.short2 as
select a.*,

case when missing(a.date) then .
     when a.date<="05may2024"d then
          case when a.m_score >= 0.13 then 1 else 0 end

     else 
          case when a.date >="06may2024"d then 
               case when a.Days >= 20 then 1 
                                      else 0 
               end
                                          ELSE .  /* missing ELSE */
          end
end 
as T_Flag

from short a
  left join 
     b.rti b
  on a.id = b.id 
  and a.sflag = b.sflag
;
quit;
_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



SASKiwi
PROC Star

I don't think nesting CASE expressions is what you intended as it doesn't make a lot of sense. Maybe something more like this?

   case 
      when missing(a.date) then .
      when a.date<="05may2024"d and a.m_score >= 0.13 then 1
      when a.date<="05may2024"d and a.m_score <  0.13 then 0  
      when a.date >="06may2024"d and a.Days >= 20 then 1
      when a.date >="06may2024"d and a.Days < 20 then 0 
     else 0
   end as T_Flag

 

Kurt_Bremser
Super User

Proper code formatting will quickly reveal where the ELSE is missing:

case
  when missing(a.date)
then . when a.date <= "05may2024"d then case when a.m_score >= 0.13 then 1 else 0 end else case /* CASE starts here */ when a.date >= "06may2024"d then case when a.Days >= 20 then 1 else 0 end end /* case ends here, but no ELSE after WHEN/THEN */ end as T_Flag

Writing spaghetti code is always bad and makes code unmaintainable. DO NOT DO IT.

bhca60
Quartz | Level 8
Thank you much!!!

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1066 views
  • 2 likes
  • 5 in conversation