SAS Basics-exercises
SAS Basics-exercises
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 .
2
Statistics 448: SAS Basics Exercises
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
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
2 infile 'raw-data-file';
3 input LastName $ 1-20 FirstName $ 21-30
4 JobTitle $ 36-43 Salary 54-59;
5 run;
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
5
Statistics 448: SAS Basics Exercises
6
Statistics 448: SAS Basics Exercises
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
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
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;
Anniv
Obs HireDate EmpID Salary BonusAmt Mo
-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