0% found this document useful (0 votes)
3 views

SAS Basics-exercises

The document provides a comprehensive guide for Statistics 448 students on using SAS, including starting a session, navigating the environment, debugging programs, and importing data. It covers practical exercises such as submitting programs, examining output and logs, and creating and modifying data sets. Additionally, it details the use of the Import Wizard for reading external data files and offers instructions for saving and managing SAS programs and libraries.

Uploaded by

newtondr7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

SAS Basics-exercises

The document provides a comprehensive guide for Statistics 448 students on using SAS, including starting a session, navigating the environment, debugging programs, and importing data. It covers practical exercises such as submitting programs, examining output and logs, and creating and modifying data sets. Additionally, it details the use of the Import Wizard for reading external data files and offers instructions for saving and managing SAS programs and libraries.

Uploaded by

newtondr7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Statistics 448: SAS Basics Exercises

Before you begin:


Login to Illinois Compass (compass.illinois.edu) and download all of the files from the SAS
Basics Review folder. Save them in the folder/drive where you store your Stat 448 files.

Part 1: Exploring Your SAS Environment under Windows


Starting a SAS Session
1. From the Start menu, select All Programs > SAS > SAS 9.2 (English).
How you invoke SAS varies by your operating environment and any customizations in effect
at your site.

2. Open the file example1.sas either by selecting File  Open, or by clicking on .

Enhanced Editor
The Enhanced Editor (the default editor on Windows) provides many helpful features, including
color coding and automatically retaining the program after each submit, eliminating the need to
recall your program.
In the Enhanced Editor, each program you open will open a new Enhanced Editor. You can have
numerous Enhanced Editors open at one time. However, if you are using the Program Editor, you
can only have one Program Editor open at a time.
 The Enhanced Editor is available only on Windows.

 The program contains three steps: a DATA step and two PROC steps.
As you browse the program, notice the following:
• The syntax is color-coded to show
– step boundaries
– keywords
– variable and data set names.
• A section boundary line separates each step.

1
Statistics 448: SAS Basics Exercises

With the Enhanced Editor, you have the ability to minimize and maximize each DATA or
PROC step. A minus sign next to DATA or PROC indicates that the code has been
expanded. To minimize the DATA or PROC step, click on the minus sign. Once the step
has been minimized, the minus sign turns into a plus sign . To maximize the step after
it has been minimized, click on the plus sign.

 You can customize the appearance and functionality of the Enhanced Editor by selecting
Tools  Options  Enhanced Editor.

3. Issue the SUBMIT command or click on or select Run  Submit to submit the
program for execution. The output from the program is displayed in the Output window.
 You can submit the code when it is collapsed. This is helpful if you want to highlight a
portion of the program and submit only that portion. You can highlight the entire line that
is visible for a step and submit it. To highlight the entire line, click to the left of the plus
sign .

Navigating in Your SAS Session


• The Results and Output windows are displayed when you submit a program that generates
output.
• You can use the Ctrl and Tab keys to navigate between windows.
• You can use the SAS window bar at the bottom of the workspace to navigate between all
of the windows in the SAS windowing environment or to minimize and maximize
windows.
• Each window in the workspace has its own menu selections that reflect
the actions you can perform when that window is active. This applies to
pull-down, pop-up, and tool bar menus.
• The Results window lists all the reports that appear in the Output window. You can
double-click and drill down on each procedure in the Results window, which enables you
to go to that report in the Output window.
• Starting in Version 8, you can also use the Results window to erase particular reports from
the Output window. You can delete each individual report by either right-clicking on the
output name and selecting Delete or clicking on the on the tool bar.

2
Statistics 448: SAS Basics Exercises

Examining Your Program Results


The Output window
• is one of the primary windows and is open by default.
• becomes the active window each time it receives output.
• automatically accumulates output in the order in which it is generated. You can issue the
CLEAR command or select Edit  Clear All to clear the contents of the
window, or you can click on the NEW icon .
To scroll horizontally within the Output window, use the horizontal scrollbar or issue the RIGHT
and LEFT commands.
In the Windows environment, the Output window displays the last page of output generated by
the program submitted.

To scroll vertically within the Output window, use the vertical scrollbar or issue the FORWARD
and BACKWARD commands or use the PAGE UP or PAGE DOWN keys on the keyboard.
 You also can use the TOP and BOTTOM commands to scroll vertically within the Output
window.
1. Scroll to the top to view the output from the PRINT procedure.

2. Issue the LOG command or select Window  Log or click on the log to display the Log
window and browse the messages that the program generated.

3
Statistics 448: SAS Basics Exercises

The Log window


• is one of the primary windows and is open by default.
• acts as an audit trail of your SAS session; messages are written to the log in the order in
which they are generated by the program.
3. To clear the contents of the window, issue the CLEAR command, select
Edit  Clear All, or click on the NEW icon .

Partial Log

The Log window contains the programming statements that are submitted, as well as notes
about
• any files that were read
• the records that were read
• the program execution and results.
In this example, the Log window contains no warning or error messages. If the program
contains errors, relevant warning and error messages are also written to the SAS log.
4. Issue the END command or select Window  Program Editor to return to the Program
Editor window.

4
Statistics 448: SAS Basics Exercises

Part 2: Debugging a SAS Program


Submit a SAS Program with Errors
1. Open the file example2.sas and submit the program.
Examine the SAS log. It should contain error messages and warnings.
1 daat work.staff;
----
14
WARNING 14-169: Assuming the symbol DATA was misspelled as daat.

2 infile 'raw-data-file';
3 input LastName $ 1-20 FirstName $ 21-30
4 JobTitle $ 36-43 Salary 54-59;
5 run;

NOTE: The infile 'raw-data-file' is:


File Name='raw-data-file',
RECFM=V,LRECL=256

NOTE: 18 records were read from the infile 'raw-data-file'.


The minimum record length was 59.
The maximum record length was 59.
NOTE: The data set WORK.STAFF has 18 observations and 4
variables.
NOTE: DATA statement used (Total process time):
real time 0.08 seconds
cpu time 0.07 seconds

6
7 proc print data=work.staff
8 run;
---
22
-
200
ERROR 22-322: Syntax error, expecting one of the following: ;,
(, DATA, DOUBLE, HEADING, LABEL, N, NOOBS, OBS,
ROUND, ROWS, SPLIT, STYLE, UNIFORM, WIDTH.
ERROR 200-322: The symbol is not recognized and will be ignored.
9

NOTE: The SAS System stopped processing this step because of


errors.
NOTE: PROCEDURE PRINT used (Total process time):
real time 0.06 seconds
cpu time 0.06 seconds

10 proc means data=work.staff average max;


------- ---
22 202
ERROR 22-322: Syntax error, expecting one of the following: ;,

5
Statistics 448: SAS Basics Exercises

(, ALPHA, CHARTYPE, CLASSDATA, CLM,


COMPLETETYPES, CSS, CV, DATA, DESCEND,
DESCENDING, DESCENDTYPES, EXCLNPWGT, EXCLNPWGTS,
EXCLUSIVE, FW, IDMIN, KURTOSIS, LCLM, MAX,
MAXDEC, MEAN, MEDIAN, MIN, MISSING, N, NDEC,
NMISS, NONOBS, NOPRINT, NOTHREADS, NOTRAP, NWAY,
ORDER, P1, P10, P25, P5, P50, P75, P90, P95, P99,
PCTLDEF, PRINT, PRINTALL, PRINTALLTYPES, PRINTIDS,
PRINTIDVARS, PROBT, Q1, Q3, QMARKERS, QMETHOD,
QNTLDEF, QRANGE, RANGE, SKEWNESS, STDDEV,
STDERR, SUM, SUMSIZE, SUMWGT, T, THREADS, UCLM,
USS, VAR, VARDEF.
ERROR 202-322: The option or parameter is not recognized and
will be ignored.
11 class JobTitle;
12 var Salary;
13 run;

NOTE: The SAS System stopped processing this step because of


errors.
NOTE: PROCEDURE MEANS used (Total process time):
real time 0.05 seconds
cpu time 0.05 seconds

Debugging Your Program


The log indicates that SAS
• assumed the keyword DATA was misspelled and executed the DATA step
• interpreted the word RUN as an option in the PROC PRINT statement (because there was a
missing semicolon), so PROC PRINT was not executed
• did not recognize the word AVERAGE as a valid option in the PROC MEANS statement, so
the PROC MEANS step was not executed.
2. Edit the program.
a. Correct the spelling of DATA.
b. Put a semicolon at the end of the PROC PRINT statement.
c. Change the word AVERAGE to MEAN in the PROC MEANS statement.
data work.staff;
infile 'raw-data-file';
input LastName $ 1-20 FirstName $ 21-30
JobTitle $ 36-43 Salary 54-59;
run;

proc print data=work.staff;


run;

proc means data=work.staff mean max;


class JobTitle;
var Salary;
run;
3. Submit the program. It runs successfully without errors and generates output.

6
Statistics 448: SAS Basics Exercises

Saving Your Program


You can use the FILE command to save your program to a file. The program must be in the
Enhanced Editor or Program Editor before you issue the FILE command. If the code is not in the
Program Editor, recall your program before saving the program.
You can also select File  Save As.
A note appears that indicates the statements are saved to the file.

Part 3: Exploring SAS Libraries and Files

1. Select the tab on the SAS window bar to open the Explorer window.

The functionality of the SAS Explorer is similar to explorers for Windows-based systems. In
addition to the single-pane view of folders and files that opens by default, you can specify a
tree view.
2. You can also select View  Explorer.

You can change the size of the windows by positioning the cursor on the window divider so
that the cursor becomes a double-arrow. Drag the window to the size you prefer.
3. Expand and collapse directories on the left. Drill-down and open specific files on the right.

7
Statistics 448: SAS Basics Exercises

4. Toggle this view off by selecting View  Show Tree.


In addition to the tree view, you can view directories and files
• as large and small icons
• in a list format
• by their detail information.
5. Double-click on the work library to show all members of that library.

6. Right-click on the staff data set and select Properties.

This default view provides general information about the data set, such as the library in which
it is stored, the type of information it contains, its creation date, the number of observations
and variables, and so on. You can request specific information about the columns in the data
table by using the Columns tab at the top of the Properties window.
7. Select to close the Properties window.

8
Statistics 448: SAS Basics Exercises

8. You can view the data portion of a data set by double-clicking on the file or
right-clicking on the file and selecting Open. This opens the data set in a VIEWTABLE
window. A view of work.staff is shown below.

In addition to browsing SAS data sets, you can use the VIEWTABLE window to edit data
sets, create data sets, and customize your view of a SAS data set. For example, you can
• sort your data
• change the color and fonts of variables
• display variable labels versus variable names
• remove and add variables.
9. Select to close the VIEWTABLE window.
10. Create the libref st that references the folder ‘D:\Stat 448’ using a LIBNAME statement. Use
the Explorer window to check whether the library has been created.

9
Statistics 448: SAS Basics Exercises

Part 4: Reading External Data Files with the Import


Wizard
Use the Import Wizard to import the file inventory.xls into SAS. This is an Excel file that
contains information on products in stock. Name the resulting data set work.inventory.
1. Select File  Import Data….

2. From the list box, select Microsoft Excel 97, 2000 or 2002 Workbook.
3. Select Next >. The Connect to MS Excel window opens.

10
Statistics 448: SAS Basics Exercises

Select Browse… to specify a workbook file to import from the Open window. After you
select the pathname, select Open to complete your selections and return to the Connect to MS
Excel window.
4. Select OK. The Import Wizard – Select Table window opens:

11
Statistics 448: SAS Basics Exercises

5. Select Sheet_1, the name of the worksheet (or named range) to be imported.
 You can select Options… to change default import settings through the SAS Import:
Spreadsheet Options window.

6. Select Next > to open the Import Wizard – Select library and member window, where you
specify the storage location for the imported file.

7. In the Library box on the left, leave the library as WORK. In the Member box on the right, type
inventory.
You can also select the down arrow in the Library box and select a different library. You can
select the down arrow in the Member box to select an existing data set. If you select an
existing data set, you will be asked later to verify that you want to replace it.

12
Statistics 448: SAS Basics Exercises

8. Select Next > to move to the next window or Finish to create the SAS data set from the
Excel spreadsheet.
If you select Finish and you select the name of an existing SAS data set for the name of your
new SAS data set (in the Import Wizard – Select library and member window), you are
prompted to determine whether or not you want to replace the existing data set. Select OK or
Cancel.
If you select Next >, you are taken to the Import Wizard – Create SAS Statements window.

9. Select Browse… to specify a location from the Save As window. After you select the
pathname, select Save to complete your selections and return to the Import Wizard – Create
SAS Statements window.
If the file already exists, you are prompted to replace the existing file, append to the existing
file, or cancel the save.
10. Select Finish.
11. Check the log to see that the SAS data set is successfully created.
NOTE: WORK.INVENTORY was successfully created.

12. Go to the Program Editor window and write SAS code to print the data set.
proc print data=work.inventory;
run;

13
Statistics 448: SAS Basics Exercises

13. Go to the Program Editor window and open the SAS code created by the Import Wizard.
PROC IMPORT OUT=WORK. inventory
DATAFILE="D:\Stat 440\inventory.xls"
DBMS=EXCEL REPLACE;
SHEET="Sheet_1";
GETNAMES=YES;
MIXED=NO;
SCANTEXT=YES;
USEDATE=YES;
SCANTIME=YES;
RUN;

Part 5: Modifying SAS Data Sets


1. Use the st.fltattnd data set to create a temporary SAS data set named bonus.
• Create a variable named BonusAmt that contains an annual bonus amount for each
employee calculated as 8% of Salary.
• Create a variable named AnnivMo that contains the employment month for each
employee. Hint: Determine the month portion of the employee’s date of hire
(HireDate).
• The bonus data set should contain only the variables EmpID, Salary, BonusAmt,
HireDate, and AnnivMo.
2. Use the PRINT procedure to display the data portion of the bonus data set. Display the
values of Salary and BonusAmt with dollar signs, commas, and no decimal places.
SAS Output
The SAS System

Anniv
Obs HireDate EmpID Salary BonusAmt Mo

1 23MAY1982 E01483 $30,000 $2,400 5


2 19MAY1986 E01384 $38,000 $3,040 5
3 02JUN1983 E00223 $18,000 $1,440 6
4 09OCT1981 E00632 $40,000 $3,200 10
5 22NOV1991 E03884 $38,000 $3,040 11
6 02AUG1984 E00034 $28,000 $2,240 8
7 14JAN1980 E03591 $43,000 $3,440 1
8 18FEB1980 E04064 $37,000 $2,960 2
9 06DEC1984 E01996 $20,000 $1,600 12
10 12MAY1992 E04356 $34,000 $2,720 5
11 25SEP1980 E01447 $35,000 $2,800 9
12 02JAN1981 E02679 $31,000 $2,480 1
13 09JAN1981 E02606 $26,000 $2,080 1
14 10DEC1987 E03323 $22,000 $1,760 12

-The End-

14
Statistics 448: SAS Basics Exercises

Portions Copyright  2007 by SAS Institute Inc., Cary, NC, USA. All rights reserved.
Reproduced with permission of SAS Institute Inc., Cary, NC, USA. SAS Institute Inc. makes no
warranties with respect to these materials and disclaims all liabilities thereof.

15

You might also like