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

Hi, i am currently facing a problem regarding the if statement, i don't no why the result keep showing the class column is all 7 instead of mix with other class number.

 

However if i remove the last part of the code which is the else, the class column result will be mix class number depend on the risktype.

 

Here's my code below

 

 

/******Houseowners******/

if risktype='FHO' or risktype='FHW' or risktype='FHZ' then

CLASS=1;

 

 

/******Householders*****/

if risktyp='FHH' or risktype='FHI' or risktype='FHY' then

CLASS=2;

 

/*****Marital Damage*****/

if risktype='FDP' or risktype='FFZ' or risktype='FHX' or risktype='FMD' or risktype='FMP' then

CLASS=3;

 

/*****Consequential Loss*****/

if risktype='FCL' or risktype='FCZ' or risktype='FLP' then

CLASS=5;

 

/*****Industrial All Risks*****/

if risktype='FAZ' or risktype='FDA' or risktype='FIA' then

CLASS=6;

 

 

/*****Others*****/

else class=7;

 

1 ACCEPTED SOLUTION

Accepted Solutions
KachiM
Rhodochrosite | Level 12
/******Houseowners******/

if risktype='FHO' or risktype='FHW' or risktype='FHZ' then

CLASS=1;

 

 

/******Householders*****/

if risktyp='FHH' or risktype='FHI' or risktype='FHY' then

CLASS=2;

 

/*****Marital Damage*****/

if risktype='FDP' or risktype='FFZ' or risktype='FHX' or risktype='FMD' or risktype='FMP' then

CLASS=3;

 

/*****Consequential Loss*****/

if risktype='FCL' or risktype='FCZ' or risktype='FLP' then

CLASS=5;

 

/*****Industrial All Risks*****/

if risktype='FAZ' or risktype='FDA' or risktype='FIA' then

CLASS=6;

 

 

/*****Others*****/

else class=7;

You have used 5 IF conditions before the ELSE statement. I think all the conditions are mutually exclusive. So use ELSE IF for the 2nd to 5th IFs.

View solution in original post

6 REPLIES 6
Kurt_Bremser
Super User

else is the "other" branch for the if/then statement immediately preceding it. So you get 7 for all values not in ('FAZ','FDA','FIA').

In your case, the select() statement is the tool of choice:

select (risktype);
  when ('FHO','FHW','FHZ') class = 1;
  when ('FHH','FHI','FHY') class = 2;
  when ('FDP','FFZ','FHX','FMD','FMP') class = 3;
  when ('FCL','FCZ','FLP') class = 5;
  when ('FAZ','FDA','FIA') class = 6;
  otherwise class=7;
end;

You can see how this cleans up the code quite nicely.

If you have to do this transformation more than once, I recommend to create an informat.

KachiM
Rhodochrosite | Level 12
/******Houseowners******/

if risktype='FHO' or risktype='FHW' or risktype='FHZ' then

CLASS=1;

 

 

/******Householders*****/

if risktyp='FHH' or risktype='FHI' or risktype='FHY' then

CLASS=2;

 

/*****Marital Damage*****/

if risktype='FDP' or risktype='FFZ' or risktype='FHX' or risktype='FMD' or risktype='FMP' then

CLASS=3;

 

/*****Consequential Loss*****/

if risktype='FCL' or risktype='FCZ' or risktype='FLP' then

CLASS=5;

 

/*****Industrial All Risks*****/

if risktype='FAZ' or risktype='FDA' or risktype='FIA' then

CLASS=6;

 

 

/*****Others*****/

else class=7;

You have used 5 IF conditions before the ELSE statement. I think all the conditions are mutually exclusive. So use ELSE IF for the 2nd to 5th IFs.

koyelghosh
Lapis Lazuli | Level 10

@Kayla_Tan222 I think you should leave the first IF statement as it is and the subsequent IF statement should be ELSE IF rather. However without seeing the data, I might be wrong.

ChrisNZ
Tourmaline | Level 20

@Kurt_Bremser is 100% right as usual.

 

These 2 statements void the prior statements:

 

/*****Industrial All Risks*****/

if risktype='FAZ' or risktype='FDA' or risktype='FIA' then CLASS=6;

/*****Others*****/

else CLASS=7;

 

 

andreas_lds
Jade | Level 19
There is an alternative to if and select: you could use a format. That way you would move the logic setting class to the format definition, some people think, that this is just code obfuscation, but you should consider it.
Kurt_Bremser
Super User

@andreas_lds wrote:
There is an alternative to if and select: you could use a format. That way you would move the logic setting class to the format definition, some people think, that this is just code obfuscation, but you should consider it.

My rule-of-thumb: if you only do it once, do it in the code; otherwise move it to a format, document that, and Bob's your uncle.

 

Example for using a format:

data have;
input risktype $3.;
datalines;
FHO
FHW
FHZ
FHH
FHI
FHY
FDP
FFZ
FHX
FMD
FMP
FCL
FCZ
FLP
FAZ
FDA
FIA
XXX
;

proc format library=work;
invalue risktype
  'FHO','FHW','FHZ' = 1
  'FHH','FHI','FHY' = 2
  'FDP','FFZ','FHX','FMD','FMP' = 3
  'FCL','FCZ','FLP' = 5
  'FAZ','FDA','FIA' = 6
  other = 7
;
run;

data want;
set have;
class = input(risktype,risktype.);
run;

proc print data=want noobs;
run;

Result:

risktype    class

  FHO         1  
  FHW         1  
  FHZ         1  
  FHH         2  
  FHI         2  
  FHY         2  
  FDP         3  
  FFZ         3  
  FHX         3  
  FMD         3  
  FMP         3  
  FCL         5  
  FCZ         5  
  FLP         5  
  FAZ         6  
  FDA         6  
  FIA         6  
  XXX         7  

 

 

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

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
  • 6 replies
  • 1146 views
  • 3 likes
  • 6 in conversation