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;
/******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.
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.
/******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.
@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.
@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 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
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!
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.