SAS -Part II ShortCourse Exercises Data Example: Using arithmetic operations data roster; height =(12*feet) + inches; Input First $ Last $ Feet Inches; datalines; Tim Smith 6 2 Alice Young 5 4 ; run; proc print data=roster; run; Exercise#1: Creating a SAS data set “One” data one; input x1-x4; datalines; 1 2 3 4 13.75 . 5 7 0.5 . . 8 ; run; proc print data=one; run; Exercise#2: Using the data set “One” and the SUM Function For a single observation across variables, we can write the following programs: data sums; set one; total1 = x1 + x2 + x3 + x4; total2 = sum(of x1-x4); total3 = sum(x1, x2, x3, x4); run; proc print data=sums; run; Exercise#3: Creating a new data set from an existing data set created in exercise #1 data means; set one; mean1 = (x1+x2+x3+x4)/4; mean2 = mean(of x1-x4); mean3 = mean(x1, x2, x3, x4); run; proc print data = means; run; Heide Mansouri Technology Support Texas Tech University SAS-II ShortCourse Exercises 2/7/16 Page 1 data shirts; medium large large medium small medium small large large medium medium medium small data large large medium small medium large medium large small medium medium large small Fitness; 44 89.47 44.609 11.37 40 75.07 45.313 10.07 44 85.84 54.297 42 68.15 59.571 38 89.02 49.874 8.65 . 8.17 47 77.45 44.811 11.63 40 75.98 45.681 11.95 43 81.19 49.091 10.85 44 81.42 39.442 13.08 38 81.87 60.055 44 73.03 50.541 10.13 45 87.66 37.388 14.03 45 66.45 44.754 11.12 47 79.15 47.273 10.60 54 83.12 51.855 10.33 49 81.42 49.156 51 69.63 40.836 10.95 51 77.91 46.672 10.00 48 91.63 46.774 10.25 49 73.37 57 73.37 39.407 12.63 54 79.38 46.080 11.17 52 76.32 45.441 50 70.87 54.625 9.63 . 8.63 8.95 10.08 8.92 51 67.25 45.118 11.08 54 91.63 39.203 12.88 51 73.71 45.790 10.47 57 59.08 50.545 49 76.32 48 61.24 47.920 11.50 . . 9.93 52 82.78 47.467 10.50 Heide Mansouri Technology Support Texas Tech University SAS-II ShortCourse Exercises 2/7/16 Page 2 data pressure; 120 128 124 131 140 132 128 125 126 118 130 132 Heide Mansouri Technology Support Texas Tech University 130 131 140 141 126 129 118 127 135 137 127 135 SAS-II ShortCourse Exercises 2/7/16 Page 3 SAS -Part II ShortCourse Exercises On your own and from what you have learned work on the following exercise: Exercise #1: Students from two fourth-grade classes are selling candy to earn money for a special field trip. The class earning more money gets a free box of candy. The following are the data for the results of the candy sale. The student’s names, followed by their classroom number, the type of candy (mint patties or chocolate dinosaurs), and the number of boxes sold. The class earns $ 1.25 for each box of candy sold. The data is given in the following table: Name Nathan Matthew Claire Chris Stephen Adriana Caitlin Ian Anthony Erika class 14 14 14 14 14 21 21 21 21 21 Candy Type CD CD CD CD CD MP CD MP MP MP Quantity 19 14 11 6 10 7 9 18 13 17 The teachers want a report giving the money earned for each classroom, the money earned by each student, and the type of candy sold. 1. Write a SAS program that reads the data, computes money earned (Profit), and sorts the data by classroom using Proc Sort. 2. Then, use the Proc Print step and a By statement to print the data by Class and a Sum statement to give the Totals for Profit. 3. Title the output “Candy Sales for Field Trip by Class”. Heide Mansouri Technology Support Texas Tech University SAS-II ShortCourse Exercises 2/7/16 Page 4 Exercise #2: Following data table is given, where: SYSTOLIC BLOOD PRESSURE SBP = AGE = 100(WEIGHT/HEIGHTSQ) = 1, if SMOKER = 0, if NONSMOKER Y= X1 X2 X3 sex Y X1 X2 X3 z f 135 45 2.876 0 0 f 122 41 3.251 0 0 m 130 49 3.1 0 0 f 148 52 3.768 0 0 m 146 54 2.979 1 54 m 129 47 2.79 1 47 m 162 60 3.668 1 60 f 160 48 3.612 1 48 m 144 44 2.368 1 44 m 180 64 4.637 1 64 f 166 59 3.877 1 59 f 138 51 4.032 1 51 f 152 64 4.116 0 0 m 138 56 3.673 0 0 f 140 54 3.562 1 54 f 134 50 2.998 1 50 m 145 49 3.36 1 49 m 142 46 3.024 1 46 f 135 57 3.171 0 0 f 142 56 3.401 0 0 f 150 56 3.628 1 56 f 144 58 3.751 0 0 m 137 53 3.296 0 0 m 132 50 3.21 0 0 Write a SAS program to create a data set called SBP. Use the SAS Explorer to look in the Work Library for the temporary SAS data set named Work.SBP Add a PROC step to obtain a listing of the data set. SORT the this data set BY Sex Create a Summary Statistics for variables using the PROC MEANS, and variables Y, X1, and X2 for each Sex, using the CLASS statement. Title the output, “Summary Statistics for SBP data set”. Heide Mansouri Technology Support Texas Tech University SAS-II ShortCourse Exercises 2/7/16 Page 5 * Answer to SAS-II Exercise #1; data CandySale; input Name $ class CandyType Quantity; Profit = Quantity*1.25; datalines; Adriana 21 MP 7 Nathan 14 CD 19 Matthew 14 CD 14 Claire 14 CD 11 Caitlin 21 CD 9 Ian 21 MP 18 Chris 14 CD 6 Anthony 21 MP 13 Stephen 14 CD 10 Erika 21 MP 17 run; proc sort data = CandySale; by class; run; proc print data = CandySale; by class; Sum profit; var name candytype profit; title " Candy Sales for Field Trip by Class "; run; * Answer to SAS-II Exercise #2: copy/paste the data as plain TEXT to an Excel file, IMPORT to SAS, and then use the following program ; data SBP; Set SBP; Z=X1*X3; run; proc print data=SBP; run; Proc Sort data=SBP Out=sorted_SBP; by Sex; run; proc means data=sorted_SBP; var Y X1 X2; class sex; title 'Summary Stats'; run; Heide Mansouri Technology Support Texas Tech University SAS-II ShortCourse Exercises 2/7/16 Page 6