Posted by : Netbloggy Friday, September 18, 2015

The most important part of any programming language is conditional processing. The general syntax goes like this:

if condition then
   --statements;
else if condition then
    -- more statements;
else if condition then
    -- more statements;
...
else
    -- other statements;

Problem 1 : 

Create a temporary SAS data set called School. Using IF and ELSE IF statements, compute two new variables as follows: Grade (numeric), with a value of 6 if Age is 12 and a value of 8 if Age is 13. The quiz grades have numerical equivalents as follows: A = 95, B = 85, C = 75, D = 70, and F = 65. Using this information, compute a course grade (Course) as a weighted average of the Quiz (20%), Midterm (30%) and Final (50%).

 Input Data:

12 A 92 95
12 B 88 88
13 C 78 75
13 A 92 93
12 F 55 62
13 B 88 82

Solution:


data A01_school;
 input Age Quiz : $1. Midterm Final;
 if Age = 12 then Grade = 6;
 else if Age = 13 then Grade = 8;
 Select(Quiz);
  when ('A') Quiz_Grade = 95; 
  when ('B') Quiz_Grade = 85;
  when ('C') Quiz_Grade = 75;
  when ('D') Quiz_Grade = 70;
  when ('F') Quiz_Grade = 65;
  otherwise;
 end;
 Course_Grade = Quiz_Grade*.2 + Midterm*.3 + Final*.5;
 drop Quiz_Grade;  
datalines;
12 A 92 95
12 B 88 88
13 C 78 75
13 A 92 93
12 F 55 62
13 B 88 82
;
run;
 
proc print data=A01_school;
run;

Output:


Learning:



  • Setting Length of a character when it's less than 8 bytes to save memory
  • How to use IF ELSE
  • How to efficiently use SELECT to reduce repetitive IF ELSE
  • Necessary to drop Not-Required Variables 

Leave a Reply

Subscribe to Posts | Subscribe to Comments

Popular Post

Blogger templates

Total Pageviews

Powered by Blogger.

- Copyright © nulldata -Metrominimalist- Powered by Blogger - Designed by Johanes Djogan -