Document 10216198

advertisement
SAS-Part I ShortCourse Exercises
Exercise #1: Working with SAS Table Editor

To open the ViewTable window, from the Tools menu select Table Editor.

Click on the column heading A and type the word Coffee; then tab to column
heading B and type Price.

o
Right-click on column name Coffee to open the Column Attributes window
o
Type Name of Coffee for the label, and then click on Apply button.
o
When finished, close this window.
Type in the data, using the following table. SAS will automatically figure out if your
columns are numeric or character based on the first row of data that you enter.
Heide Mansouri
Technology Support
Texas Tech University
SAS-I ShortCourse Exercises
Updated: 3/24/16
Page 1

From the Save As dialog box, select a library (Work or Sasuser for example) and
then specify the member name (file name) of your data (table) - coffee for
example.

To Print the Content of this Table write the following program and then submit it:
PROC print data=coffee;
Run;
Exercise #2: Basic DATA Steps
Type the following program into the Editor window and submit it:
data backpain;
input Gender $ Age LostDays Cost;
datalines;
Female
35 10 995
Male
45 10 1115
Female
34 12 1225
Male
23 2
225
Male
50 1
175
;
run;
Proc Print data=backpain;
run;
Exercise #3: Adding Label and Format Statement
Data backpain;
input Gender $ Age LostDays Cost;
label LostDays =‘Number of missed workdays’
Cost
= ‘Cost of treatment in US dollars’;
datalines;
Female 35 10
995
Male
45 10
1115
Female 34 12
1225
Male
23 2
225
Male
50 1
175
;
run;
Proc print data =backpain label;
Format Cost Dollar10.2;
Run;
Heide Mansouri
Technology Support
Texas Tech University
SAS-I ShortCourse Exercises
Updated: 3/24/16
Page 2
Exercise #4: Performing Means Procedure (Proc Means)
PROC means data=backpain;
class Gender;
var Age LostDays Cost;
run;
Exercise #5: Performing Frequency Procedure (PROC Freq)
Proc Freq data=backpain;
tables Gender Cost Gender*cost;
run;
Exercise #6: Performing Sort and Contents Procedures
Proc sort data = backpain out = sorted_backpain;
by Gender;
Run;
Proc print data=sorted_backpain;
Run;
Proc contents data = sorted_backpain;
Run;
Exercise #7: Performing Univariate Procedure (Proc Univariate)
Proc UNIVARIATE data = backpain plots;
Var Age LostDays Cost;
Title ‘Summary Statistics For Backpain’;
Run;
Title;
Exercise #8: Importing an Excel file
Suppose that you have the results of two tests for a group of five students in the following
table:
Student
Exam1
Exam2
1
80
84
2
85
90
3
70
55
4
94
94
5
88
84

Create this table in Microsoft Excel with no formatting.

Save and close the excel file before you can import it into SAS.

From the File menu, select Import Data…
Heide Mansouri
Technology Support
Texas Tech University
SAS-I ShortCourse Exercises
Updated: 3/24/16
Page 3

Locate the Input File (Excel workbook for example).

Check that appropriate options have been selected.

Select a location to store the imported file, SAS Library (Default: Work).

Specify Member (a SAS data set name), Roster for example.

Click Next > Finish.

You may Save the generated PROC IMPORT code also (optional).

You may print the file also by submitting the following program:
Proc print data=roster noobs;
Run;
Exercise #9: Computing With SAS
Data Myclass;
SET Roster;
Final=(Exam1+Exam2)/2;
If 65>final>=0 then grade='F';
If 75>final>=65 then grade='C';
If 85>final>=75 then grade='B';
If final >= 85 then grade='A';
Run;
Proc print data=myclass noobs;
var student final grade;
title 'Base SAS SC Exercise';
title3 'Texas Tech University';
Run;
Title;
Exercise #10: Importing and printing sub-sample of MORG

From the File menu, select Import Data…

Click on Next

Click on Browse and select File of type: .xlsx format (the default is .xls), and then
select the file located at My Computer > ShortCourses Materials > SAS and
SPSS > SAS > Base SAS > morg05s1.xlsx

Click on Next

Enter a name MORG for the data set

Click on Finish.

Display the data set by typing the following program in the Editor window, and
submitting it:
Proc Print data=MORG;
Run;
Heide Mansouri
Technology Support
Texas Tech University
SAS-I ShortCourse Exercises
Updated: 3/24/16
Page 4
Exercise #11: Using conditional IF statements
data try1;
set morg;
if earnwke < 500;
run;
proc print data=try1;
var hhid earnwke;
run;
Exercise #12: Using conditional IF-THEN statements
data try2;
set morg;
if earnwke < 500 then ewk_group = 1;
run;
proc print data=try2;
var hhid earnwke ewk_group;
run;
Exercise #13: Using conditional IF-THEN-ELSE statements
data try3;
set morg;
if earnwke < 500 then ewk_group = 1;
else if earnwke < 1000 then ewk_group = 2;
else ewk_group = 3;
run;
proc print data=try3;
var hhid earnwke ewk_group;
run;
Heide Mansouri
Technology Support
Texas Tech University
SAS-I ShortCourse Exercises
Updated: 3/24/16
Page 5
Exercise #14: Appending SAS Datasets
Data CLASS1;
input name $ age height;
datalines;
Andrew 15
67
Philip
14
70
Robert 15
78
Stephen 17
72
;
run;
Data CLASS2;
input name $ age height;
datalines;
Andrea 16
60
Linda 13
55
Sandra 17
65
;
run;
proc print data=class1;
run;
proc print data=class2;
run;
PROC APPEND BASE=class2 DATA=class1(WHERE=(age=15));
RUN;
proc print data=class2;
run;
Heide Mansouri
Technology Support
Texas Tech University
SAS-I ShortCourse Exercises
Updated: 3/24/16
Page 6
On your own
On your own and from what you have learned work on the following exercise:
Exercise #15:
Three fertilizers (X, Y, and Z) have been applied to three species of trees (Pine,
Oak, and Maple) and the following data is collected:
Pine
Oak
Maple
Oak
Maple
Maple
Pine
Pine
Oak
Oak
62.1
43.1
51.6
41.3
41.0
62.7
33.4
44.3
52.4
74.0
X
X
Y
Z
X
Y
X
X
Y
Y
86.0
76.9
56.5
53.7
61.9
63.4
66.9
72.3
36.6
76.2
The variables are:
1. Tree species.
2. Initial circumference of the tree (in centimeters) when the study began.
3. The type of fertilizer used.
4. Circumference of the tree when the study ended.
Create a SAS program which does the following:
1. Reads the data into a SAS data set called "Tree".
2. Prints the data values to make sure that data values are being read in correctly.
3. Computes the means for each of the numeric variables for each of the fertilizer
applications (class variable).
Heide Mansouri
Technology Support
Texas Tech University
SAS-I ShortCourse Exercises
Updated: 3/24/16
Page 7
Download