Thursday, 17 September 2015

Chapter 7 to 15

After referring the book 'Learning SaS by Example' of Ron Cody and reading chapters from 7 to 15, i have tried solving a few exercise problems. I will demonstrate 10 problems with proper output. The screen shot and the code of the program will be attached hereby.

Q 7.1




.   Run the program here to create a temporary SAS data set called School:
  data school;      input Age Quiz : $1. Midterm Final;      /* Add you statements here */   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
  ;

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%).
 


In this particular program, we have created a library B14035 and thus we refereed the program calling the permanent library everytime that we have created. We have then used if else statement to determine the Quiz Grade. For instance if he got grade A then its numerical equivalent will be 95 else if B.... We then considered the data lines and then came up with a print statement executing the result by writing proc print data=....

Q 8-3




     Modify the program here so that each observation contains a subject number (Subj), starting with 1:
   data test;       input Score1-Score3;       /* add your line(s) here */    datalines;    90 88 92
   75 76 88
88  82 91
   72 68 70
   ;  



 
In this program we have put up the line Subj+1 which means an increment. For the first set of data lines it says 1, for the 2nd set of data lines it shows subject 2 and then it stops on the 4th line.

Q 8-5



 Create and print a data set with variables N and LogN, where LogN is the natural log of N (the function is LOG). Use a DO loop to create a table showing values of N and LogN for values of N going from 1 to 20

 .  

In this Question we have created a do loop which runs from 1 to 20 and equivalently finds log value of individual number. For instance if we have taken N=1, we found out Log(1) which gives us a value and that continued till 20.



Q 8-11.



   80 81 82 83 84 84 87 88 89 89 
   91 93 93 95 96 97 99 95 92 90 88
   86 84 80 78 76 77 78
   80 81 82 82 86 
88  90 92 92 93 96 94 92 90
   88 84 82 78 76 74

The first 24 values represent temperatures from Hour 1 to Hour 24 for Dallas and the next 24 values represent temperatures for Hour 1 to Hour 24 for Austin. Using the appropriate DO loops, create a data set (Temperature) with 48 observations, each observation containing the variables City, Hour, and Temp. 
Note: For this problem, you will need to use a single trailing @ on your INPUT statement




 In this program again we have used do statement to count 1st 24 temperature for Dallas and then Houston. In the input we have used @ which is called trailing to read the datalines sequentially 

Q 9-9



Repeat Problem 8, except use the following data. If there is a missing value for the day, substitute the 15th of the month. 
25  12 2005
   .  5  2002
   12 8     2006





In this program we checked if there is any missing day. If yes that will be replaced by 15 else it will print in the normal format it is assigned 

Q 11-7



Given values of x, y, and z, compute the following (using a DATA _NULL_ step):
a.       AbsZ = absolute value of z
b.       Expx = e raised to the x power
c.       Circumference = 2 times pi times y

Use values of x, y, and z equal to 10, 20, and –30, respectively. Round the values for b and c to the nearest .001. 






In this Program first we have assigned values to x, y and z as asked in the Question. Then we found out the absolute Z, circumference  and Exponential X. We have used the round function to take it to a nearby value of .001

Q 8-1

Run the program here to create a temporary SAS data set called Vitals:
data vitals;       input ID    : #3.             Age      
 Pulse    
 SBP             DBP;
 label SBP = "Systolic Blood Pressure"             DBP = "Diastolic Blood Pressure";    datalines;    001 23 68 120 80
   002 55 72 188 96
   003 78 82 200 100
   004 18 58 110 70



   005 43 52 120 82
   006 37 74 150 98
   007  . 82 140 100
   ;

Using this data set, create a new data set (NewVitals) with the following new variables:
 For subjects less than 50 years of age:
 If Pulse is less than 70, set PulseGroup equal to Low;  otherwise, set PulseGroup equal to High 
If SBP is less than 130, set SBPGroup equal to Low;  otherwise, set SBPGroup equal to High.
 For subjects greater than or equal to 50 years of age:
 If Pulse is less than 74, set PulseGroup equal to Low; otherwise, set PulseGroup equal to High. If SBP is less than 140, set SBPGroup equal to Low; otherwise, set SBPGroup equal to High.
 You may assume there are no missing values for Pulse or SBP. 



In this case we have created two data set. One is the master data set and then we have created a subset called NewVitals. When we displayed the output, we used the Proc Print to call the subset from the main set.

Q 8-9

You have the following seven values for temperatures for each day of the week,starting
with Monday: 70, 72, 74, 76, 77, 78, and 85. Create a temporary SAS data    set (Temperatures) with a variable (Day) equal to Mon, Tue, Wed, Thu, Fri, Sat, and    Sun and a variable called Temp equal to the listed temperature values. Use a DO    loop to create theDay variable


      As we have put @, which means it will run the datalines sequentially. Now in the do loop we have assigned the days of the week. So for each day it will assign the temperature sequentially. As the days are over and the reading of the datalines are over, it will stop.  

Q 12-1


 Look at the following program and determine the storage length of each of the variables:

  data storage;      length A $ 4 B $ 4;      Name = 'Goldstein';
     AandB = A || B;      Cat = cats(A,B);
     if Name = 'Smith' then Match = 'No';         else Match = 'Yes';           Substring = substr(Name,5,2);   run;

    A       
_________________
    B     
_________________
    Name
_________________
    AandB
_________________
    Cat    
_________________
    Match
_________________
    Substring 



_________________

Q13-5


1.   The passing score on each of five tests is 65, 70, 60, 62, and 68. Using the data here,         use a temporary array to count the number of tests passed by each student.
ID
Test 1
Test 2
Test 3
Test 4
Test 5
001
90
88
92
95
90
002
64
64
77
72
71
003
68
69
80
75
70
004
88
77
66
77
67



































In this program we have taken an array of size 5. The scores are mentioned in the Question. In the input Id we have taken character of maximum length 3. Then we have taken a do loop and have run from 1 to 5. After that we have checked the conditions.