BookmarkSubscribeRSS Feed
Babloo
Rhodochrosite | Level 12

Appreciate if someone of you guide me to combine the following if clauses to 'case when...' statement. I want to calculate the variable 'knkbty' and it should have values 1,3,4,5,6.

 

 

 

3 REPLIES 3
s_lassen
Meteorite | Level 14

That's kinda messy code, and not workable (there are two unclosed DO blocks). Disregarding the hanging DO statements, I think that what you want may be something like this:

case
  when knkatd ne knkatd_v then '6'
  when agi ne agi_v then '1'
  when knbjpn > knbjpn_v then '3'
  when knkvpb in ('1','2','3') and knkvpb_v in ('4','5') then '3'
  when knkvpb in ('4','5') and knkvpb_v in ('1','2','3') then '3'
  when knbjpn < knbjpn_v then '4'
  else '5'
end
Babloo
Rhodochrosite | Level 12

Thank you. How to add the below condition in the following case when code to derive the value for KNKBTY=2 apart from 1,3,4,5 and 6?

 

  if agi ne agi_v           
    then do;                
      knkbty = '1';         
      knkatv = '' ;         
      knkath = '' ;         
      agiteg = agi;         
      link schrijfv;        
   else                        
      knkbty = '2';         
      knkatv = '' ;         
      knkath = '' ;         
      agiteg = agi_v;       

Case when statement which I have right now is as below. I've to add the above condition in the case when to derive value for '2' as well.

 

case
  when knkatd ne knkatd_v then '6'
  when agi ne agi_v then '1'
  when knbjpn > knbjpn_v then '3'
  when knkvpb in ('1','2','3') and knkvpb_v in ('4','5') then '3'
  when knkvpb in ('4','5') and knkvpb_v in ('1','2','3') then '3'
  when knbjpn < knbjpn_v then '4'
  else '5'
end
mkeintz
PROC Star

Your first if group tests on the relationship between knbjpn and knbjpn_v, and it exhausts all possible relations (you have one <, one > and one for =).

 

So your subsequent tests on AGI and KNKATD, if true, will supersede results based on knbjpn.  Is that intentional?

 

Assuming it is, that's why @s_lassen's response reverses the order of the 3 main IF test groups, but maintains the order within the first group (if, else if, else).

 

 

 

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 3 replies
  • 1095 views
  • 0 likes
  • 3 in conversation