MET MA 603: SAS Programming and Applications
MET MA 603:
SAS Programming and Applications
Logical Statements
1
1
IF-THEN Statements
IF-THEN statements allow for statements to be executed conditionally. In other words, a statement may be executed for some observations, but not for others.
IF-THEN statements have two parts: a condition and an action. The condition is a logical expression that is evaluated for each observation, and has a result that is either true or false. The condition uses characters such as =, <, >, <=, >=, ~= (not equal). The action is a statement that will only be executed when the condition is true.
data occupancy;
set mydata1.occupancy;
if DogBreed = “” then DogBreed = “NO DOGS” ;
run ;
2
2
ELSE Statement
In the previous example, the action statement is either executed, or it is not. There are situations where one statement should be executed if the condition is true, and a different statement should be executed if the condition is false. The ELSE statement allows for a statement to be executed when the IF condition is false.
data occupancy;
set mydata1.occupancy;
if dogs > residents then House_Type = “More Dogs” ;
else House_Type = “More People” ;
run ;
3
3
ELSE IF
The previous example used one condition. There are situations where more than one condition needs to be evaluated. ELSE IF allows for additional conditions to be tested. Note that an ELSE IF condition is only evaluated when all preceding IF and ELSE IF conditions are false. Once a true condition is found, the corresponding action is executed and the statement is finished.
data occupancy;
set mydata1.occupancy;
if dogs > residents then House_Type = “More Dogs” ;
else if dogs
else Lonely Person = “Yes”;
run ;
8
8
Logical Operators (continued)
Example of the IN logical operator:
data occupancy;
set mydata1.occupancy;
if dogbreed in (“NONE OF THE ABOVE”,
“None of the above”)
then BreedType = “Safe Breed”;
run ;
Example of the NOT IN logical operator:
data occupancy;
set mydata1.occupancy;
if dogbreed not in (“NONE OF THE ABOVE”,
“None of the above”, “”)
then BreedType = “Unsafe Breed”;
run ;
9
9
Graphical Representation of Logical Operators
AND IN
OR NOT IN
10
10
DO-END Statements
Sometimes more than once action statement needs to be executed when a condition is met. The DO and END statements indicate to SAS that all statements inside this structure are to be executed when the condition is met.
data occupancy;
set mydata1.occupancy;
length DogInd $2;
if dogs > 0 then DogInd = “1+” ;
else do;
DogInd = “0” ;
DogBreed = “No Dogs”;
end;
run ;
11
11
Practice
Use the policy_info.sas7bdat dataset.
Create a variable called RoofQuality based on the following mapping:
(see the next slide for the rest of the problem)
12
12
Practice (continued)
Based on each policy’s characteristics, assign the Roof and Alarm factors based on the following criteria:
Your result should match the discount.sas7bdat dataset.
13
13
Readings
Textbook sections 3.5, 3.6
14
14
Score RangeGrade
90-100A
80-89B
70-79C
60-69D
0-59F
Grade Mapping
Roof MaterialRoof Quality
asphaltNormal
built up/tar or gravelPoor
clay tileGood
compositionGood
concrete tileGood
otherNormal
slateGood
spanish tileGood
tin/membraneNormal
wood shakePoor
wood shinglesPoor
Roof Material Mapping
Roof QualityHO3HO4HO6
Poor1.101.001.05
Normal1.001.001.00
Good0.901.000.90
RoofQuality Factor
Burglar AlarmFire AlarmFactor
CentralCentral0.85
CentralLocal0.90
CentralNone0.95
LocalCentral0.90
LocalLocal0.97
LocalNone0.99
NoneCentral0.95
NoneLocal0.99
NoneNone1.00
Alarm Factor
/docProps/thumbnail.jpeg