Professional Documents
Culture Documents
Unit 4
SAS for Data Management
Welcome.
This reading is an introduction to getting in and out of SAS together with a set of
guidelines for navigation, programming, and documentation.
2. To learn how to navigate the windows environment in SAS, including the creation
of user defined toolbar buttons;
3. To appreciate (and know the principles of writing) code that is organized and
clear;
week 07 7.1
Week 7 Introduction to SAS – The Basics
week 07 7.2
Week 7 Introduction to SAS – The Basics
SAS version 8 operates through a windowing environment that lets you view and
work with programming statements, data files, results and file directories in a variety
of ways.
Start Æ
Programs Æ
The SAS System Æ
The SAS System for Windows V8
Explorer
Results
Program Editor
Log
Output
week 07 7.3
Week 7 Introduction to SAS – The Basics
Your screen will look as follows. The Results window will be hidden behind the
Explorer, and the Output window will be hidden behind the Program editor and log
windows. All five windows are seen to be open on the SAS task bar at the bottom of
the windows:
All the basic tasks for managing data, programs and program output can be
accomplished in these windows. Other SAS features will use additional windows.
week 07 7.4
Week 7 Introduction to SAS – The Basics
Window Purpose
Explorer View and manage SAS files; perform
most file management tasks such as
moving, copying, opening and deleting
files.
Program Editor Enter, edit, and submit SAS programs
Log Displays messages about SAS session
and programs that you submit
Output Browse output from SAS programs that
have been run
Results Manage output (results) from SAS
programs that have been run – view,
save and print segments of output.
• The user “writes” (“codes”) a set of instructions. These are strung together in a
program, and executed together in a batch, submitted for processing from the
PROGRAM window.
• The SAS system then displays Information on the action of the commands
• The program
• The log
• The output
week 07 7.5
Week 7 Introduction to SAS – The Basics
Tip: Save the three elements of a SAS program (program, log, output) as a
group as follows.
• Use the same first level name, with extensions to match the contents. For
example, the executable program statements can be saved in a file called
file1.SAS to indicate that these are SAS program statements. The log and
output should be saved in files with the names: file1.LOG and file1.LST to
indicate their relationship to the program that created them.
• The .SAS, .LOG, and .LST extensions will be added automatically when you
save files from the program, log and output windows, respectively.
• The SAS system “looks” for and recognizes these extensions, and has
assigned icons to them. For example:
week 07 7.6
Week 7 Introduction to SAS – The Basics
• Pull down menus: These operate like the pull down menus in MSWord. The
particular menus that are available depend upon which is the active window
(indicated by the top bar highlighted in blue).
• The toolbar: A limited set of common commands are available on the toolbar –
again, this changes with the active window. You can program your own
buttons to add to the toolbar.
• The SAS command bar: You can type SAS commands directly in this bar and
hit <enter> to execute the command. Tip: This is generally not
recommended, except for commands such as “clear” or “submit”.
• Function keys: Commands can also be submit through the function keys. For
example,
o <F5> (pgm) moves you to the program editor window – makes it the
active window
o <F6> (log) moves you to the log window
o <F9> (keys) brings up the Keys window (shown below), which lists the
function key commands that are programmed.
o The keys window can also be found from the pull down menu:
Tools Æ Options Æ Keys.
week 07 7.7
Week 7 Introduction to SAS – The Basics
Pulldown menus
Toolbar
Command Bar
week 07 7.8
Week 7 Introduction to SAS – The Basics
You may find it simplest to use the mouse to select, open and save files, and to move
• It is useful to know some of the commands so that you can program buttons, or
use SAS commands in within SAS programs (e.g., programs that write other
programs.)
• The CLEAR command clears the contents of the currently active window.
• The SUBMIT submits the contents of the program window for execution.
• Tip: Take care to understand the SAVE command. The SAVE command is
executed from either the File menu, the command bar, the button on the
toolbar, or a function key. It saves the contents of the active window, not
your data. You can use this button to save a program, log or output file. Only
when you have a data file window open, does the SAVE command or clicking
the SAVE icon save your data file.
week 07 7.9
Week 7 Introduction to SAS – The Basics
Within the SAS system, it is possible to create buttons to add to the toolbar that make
working with windows and files easier. The buttons are created using the TOOLS pull
down menu.
1. Make your Program Editor the Active Window by clicking inside the
Program Window.
week 07 7.10
Week 7 Introduction to SAS – The Basics
o log;clear;output;clear;
o Clear log/out
(The ‘tip text’ is the text that will appear in the rollover box when you
move your cursor over the button, but do not click).
• Click on the Change Icon button: and select an icon for your button.
week 07 7.11
Week 7 Introduction to SAS – The Basics
• With the new button highlighted in the box, click on the up or down arrow
buttons to position the button where you would like it to appear on the
toolbar.
• Click on OK when it is located where you would like.
• Click on Yes to SAVE the new version of the toolbar.
Note: The toolbar in the PROGRAM window has been modified in this example. You
must repeat the steps if you would like the same button to appear on the toolbar in
the LOG or OUTPUT windows -- since these windows have a different toolbar. Start
the process with the LOG or OUTPUT window as the active window.
week 07 7.12
Week 7 Introduction to SAS – The Basics
SAS
S t
SAS Programming
AS/Solutions
• Analysis
• Development
and SAS Windowing Batch
Programming Environment
• Reporting
• Accessories
• ASSIST
• Desktop
• Application
Builder
• A user of the solutions mode does not write programs. He or she works
• This mode of operation is not the focus in this course, because it does not lend
week 07 7.13
Week 7 Introduction to SAS – The Basics
• However, for many of the Solutions applications, Version 8 of SAS does offer
the option of saving a program file based upon menu selections. The
Solutions menu may become a good way to explore and learn some new
• SAS programming requires that the user write or edit a series of SAS statements,
• In the windowing environment, programs are written or opened and edited in the
PROGRAM EDITOR Window, and then submitted for processing from that
window. The LOG window displays the executed code and messages, and the
• Optionally, results can be written in HTML format to a browser window instead of,
or in addition to the standard SAS output window. If you are planning to display
results on the web, or if you prefer the table format created for browser output this
is a good choice.
week 07 7.14
Week 7 Introduction to SAS – The Basics
Check “Create HTML” if you wish to have an HTML version of your results
written. Note that there is a style box, which allows you to select the format for the
results tables. Color and shading are used in various combinations. I recommend
the choice “Minimal” to avoid color, shading and font changes. This is easier on
printing, and is preferable if you are likely to Xerox results. Additionally, you may
select the internal SAS browser or a different web browser for viewing results.
produce results in the standard output window. When both options are checked you
get 2 versions of your results displayed, one in the output window, and one in a
browser window.
week 07 7.15
Week 7 Introduction to SAS – The Basics
There are a variety of choices for writing and editing SAS code.
tip: If you are using MS Word, it is important that you save your file as a
TEXT ONLY type of document and that you name it with the extension
The main advantage to working in a text editor or word processor is that you can
write or edit programs on a computer that does not have SAS loaded, and later run
week 07 7.16
Week 7 Introduction to SAS – The Basics
The SAS PROGRAM EDITOR is a reasonably flexible editor, with cut, copy and
dividing lines to help keep track of SAS syntax features and program flow.
The new enhanced editor has the potential of reducing programming errors
colon – but which can be very time-consuming to find when proofing and
testing programs. I strongly recommend that the new user work with the
• When SAS is opened from the Start menu, the default active window is the
can be opened in the window, using the ‘Open’ button or Open option from the
• Alternatively, SAS and a program can be opened directly from the Windows
• Programs can also be submitted and run in Batch Mode. When a program has
been written and saved with a .SAS extension, the program can be run without
opening the SAS windows. To run a program in batch mode: from the
the options on the menu is “Batch Submit”. Selecting this will submit the
program to the SAS processor. Files with the same first level name and the
extensions .LOG and .LST will be written to the same drive and directory as
week 07 7.17
Week 7 Introduction to SAS – The Basics
the program (.SAS) file. These will contain information on the execution of the
Using batch mode submission has the advantage of increased speed when working
with large data sets, or when working on a machine with the minimum RAM
requirement for SAS. Loading the SAS window system itself uses a considerable
week 07 7.18
Week 7 Introduction to SAS – The Basics
An online help system is available with SAS and is accessible from the menu bar.
The help feature includes a set of tutorials for learning to use the features of the SAS
windowing environment, along with a full reference feature for DATA step and
week 07 7.19
Week 7 Introduction to SAS – The Basics
The SAS system is a structured programming language in which there are two types
of “building blocks”, the DATA step and the PROCEDURE (PROC) step.
• Each of the steps is limited in scope and has defined input and output. As a
modularization.
SAS DATA steps are used to create SAS data sets, and are used for data
manipulation. DATA steps create SAS datasets by giving instructions for reading
data into SAS, or for merging, sub-setting or updating existing SAS datasets, along
SAS PROCs (procedures) read SAS datasets and perform specific functions, such as
listing the data (PROC PRINT), or producing charts or plots (PROC CHART, PLOT),
week 07 7.20
Week 7 Introduction to SAS – The Basics
SAS procedures produce either results in the OUTPUT and/or browser window, or
• Several short SAS statements may be written on the same line, with program
span several lines, where a semi-colon indicates the end of the statement.
statements is ignored. TIP: This is not true for data, which is case
sensitive.
week 07 7.21
Week 7 Introduction to SAS – The Basics
There are three self-documentation features of the SAS system: comments, titles,
and footnotes.
Introduction to Comments
• Comments can be placed in SAS programs to help explain the code or identify
modules. Comments appear in the LOG, but do not appear in the output. The
syntax for comments can take two forms:
• In the Enhanced Program editor, comments appear in green. If you forget the
end semi-colon or */, the statements following the comment will still appear in
green.
• When the comment is in the first form (as a SAS statement), statements
immediately following the semi-colon will be read and executed. The second
form of comment can surround a block of SAS statements that include semi-
colons, but statements will not be executed until after the concluding */. This
can be useful when rerunning a program, when some section of the program
does not need to be rerun. Surrounding a section of code that is to be skipped
by /* and */ is called “commenting out” a section of code.
week 07 7.22
Week 7 Introduction to SAS – The Basics
Introduction to TITLES
• TITLES and FOOTNOTES are special SAS statements that print a title with
results, following the step containing the title statement. The syntax for title
statements is illustrated below. You are allowed up to 10 titles.
• The title that will be printed on each page of your results is contained in the
single or double quotes that follows the TITLE instruction.
• Following is an example
Note: Double quotes must be used when the title itself contains a question
mark.
• A useful feature of the new enhanced editor is that: ‘All information within
quotes appears in purple.’ If you neglect to close a quote, the SAS
processor assumes the next lines are part of the quote – even semi-colons are
ignored. If several lines appear in purple – you may have a problem with
unbalanced quotes. This is a very common programming error.
• Capitalization appears on the output just as you typed it within the quotes.
Several titles can be specified, with the order of the titles indicated by the title
number. Titles stay in effect for all output until you write a new title statement.
• To get rid of all previous titles you can simply use the line
TITLE1;
• To keep the same first 2 title lines and replace the third use
TITLE3 ‘New Third Title’;
week 07 7.23
Week 7 Introduction to SAS – The Basics
• To replace an early title and keep subsequent ones, you must copy and repeat
all titles with higher numbers.
• The first title (Title1) will appear in the RESULTS window next to the
procedure name used to produce the output, as well as in the OUTPUT
window. The first title, therefore, can be a useful aid in navigating through
your output.
Introduction to FOOTNOTES
• Footnotes are similar to titles except that they will be printed on the bottom of
the output page. Footnotes have a similar syntax to titles as illustrated below:
• One useful practice is to reserve the first title or first footnote for the program
name, for easy reference to the program that produced the output. For
example, at the beginning of your SAS program, you could include a line:
week 07 7.24
Week 7 Introduction to SAS – The Basics
• This would appear beneath all subsequent output, as long as you don’t replace
it with a new footnote1 statement. This can be a tremendous aid in
documentation – linking a data table or graph directly to the program that
produced it.
week 07 7.25
Week 7 Introduction to SAS – The Basics
Key among these – take time to plan. When writing programs, take time to
write a detailed step-by-step outline of what you want to accomplish first. You
can always revise as you go, but it helps save time wasted from missing
Using your outline, write your program in SAS language on paper first. Initially,
and even as you become more proficient, it is hard to keep track of the editing
setting at a later date. “Talk to yourself” with comments as you go. Make your
programs user-friendly.
week 07 7.26
Week 7 Introduction to SAS – The Basics
3. Use a program header to keep track of when, where, and why the program
was written, what data sets were used, and what data sets were created.
A program header is merely a block of comments at the start of the program that
label your results. To reiterate, reserving the first title or footnote for the
program filename helps link results to the program that produced them.
*_______________________________________________________________
*
* Corinne Jurgens 2003
*
* code: p02.sas
* path: z:\bigelow\consulting\jurgens 2003\sascode
* prog: carol bigelow
* date: February 15, 2003
*
* input: z:\bigelow\consulting\jurgents 2003\sasdata\jurgens.sas7bdat
* output: none
*
* title:
* What is the relationship between symptom awareness (HFSAS vars)
* and delay in care seeking (ACUTESXS, DOEDUR, DYSPDUR,
* ACUTEDYS,ORTHPDUR,PNDDUR,COUGHDUR,FATIGDUR,WTGAIN,EDEMA,ABDGIRTH,
* TACHYDUR,CPDUR,NAUSEA)
*
* summary:
* 1. For all 14 Y=delay in care variables, the distribution
* of Y is compared across strata defined by HFSASTOT
* Strata define quintiles of HFSASTOT, approx.
*
* 2. For the 6 Y=delay in care variables that are essential
* a Kruskal Wallis nonparametric anova is performed to
* see if Y=delay in care varies appreciably by
* HFSASTOT quintile
*
* The 6 delay in care seeking Y variables are ACUTESXS,
* DYSPDUR, ORTHDUR, ACUTEDYS, PNDDUR, and DOEDUR
*
*_________________________________________________________________;
week 07 7.27
Week 7 Introduction to SAS – The Basics
A header need not be this elaborate, but the essential information should be
available.
4. Use the OPTIONS Statement at the start of each program to control the
The SAS defaults for line size and page size are designed to fit on a computer
screen, rather than a page of paper. Printing this way wastes paper as well as
breaking up tables and lists unnecessarily. To set output for 8.5 X 11 inch
paper, use values in the range of 55 to 60 for pagesize, and 78 to 128 for
linesize. If you select a larger number for linesize, you will need to print output
in “landscape” format or else select a small size font when printing. This saves
example:
can be used for most programming. The OPTIONS statement also gives you
dates (NODATE turns date off). These and other options can be reset during the
numbers on output, starting the numbering within a SAS session, not within a
program. Typically the numbers are irrelevant to a report. In addition, the date is
printed on the first title line, and output is always centered (side-to-side) on the
week 07 7.28
Week 7 Introduction to SAS – The Basics
page. I prefer to turn off these features, so a commonly used options line at the
5. It is good practice to always save your program before you run (submit) it,
If you find you need to make changes in your program after reviewing the log
and output, first clear the log and output windows, return to the program
window, edit, and again save the revised version before resubmitting. The
enhanced editor has a nice feature: if you have modified the contents of the
program editor, but not saved it, an asterisk (*) appears next to the program
name in the task bar, as a reminder to save the program. If you fail to clear the
log and output windows, the new version will be appended to the old. You will
have a lot of garbage to deal with in the results window, as you try to figure out
which is the old and which the new version. You can easily save the contents of
the log and output windows from the final version, without a lot of other junk, as
week 07 7.29
Week 7 Introduction to SAS – The Basics
Check for error messages, other notes and messages on how the program ran,
including the number of observations in the data sets read and created.
program may be valid, but it just doesn't do what you want it to do. Often, this
checking for the number of variables and observations in a data set, when the
problem might be missed if you look only at the output. SO READ YOUR LOG!
7. Last but not least, as always, make back-up copies of your programs and
data sets.
Log and output files can be easily reproduced by re-running a program on the
week 07 7.30
Week 7 Introduction to SAS – The Basics
It is easy to lose track of all the data management and analysis activities associated
an easy system for documenting, indexing and filing your work. The following are
suggestions only -- you need to develop a system of your own, one that you like
Use program headers, and make generous use of comments within your
programs. If you have a copy of the program, you will then have (detailed)
information on its purpose, and when and where the program was used.
Spending an extra $1 per disk can be cheap compared to the time and effort
needed to replace lost data or analyses. For each project you should keep on
- Grant proposal
- Study protocols, forms
- Data and associated files (e.g. .rec, .chk, .qes files in EpiInfo)
- SAS program files (*.sas files)
- SAS data sets (*.sas7bdat files in SAS V8)
- Reports and memos
- Manuscripts or presentation files (include final analysis data set)
Always save SAS data files and SAS program files on disk.
You may choose to save log and output files on disk, and/or to print hard
copies. The log and output can be recreated as long as you have the data and
SAS code.
week 07 7.31
Week 7 Introduction to SAS – The Basics
Disk label
This label should then be written on the stick-on disk label, in addition to your name
and any other information you deem pertinent. In your documentation, keep track of
Be sure to label all your disks with your name, and perhaps phone and/or office
number. The chances are great that at some point in your career here you will leave
week 07 7.32
Week 7 Introduction to SAS – The Basics
Always use the .SAS extension for SAS program files, .LOG for the
associated log and .LST for the associated output. The SAS system
submission of programs, and use of the SAS Viewer to review log and output
program
(.sas) log
(.log)
output (.lst)
Choose a naming convention for the first part of the filename that is easy
for you to understand. Since the number of programs that can be developed
study, the prefix WCP was used and programs were and then numbered
If programs were modified or rerun, this was always noted in the header. If the
week 07 7.33
Week 7 Introduction to SAS – The Basics
Choose names that allow you this flexibility. Another suggestion is that
programmers on a study use their own set of names (so that two programmers
don’t produce two different files having the same name). One suggestion is to
use programmer’s initials in the name, e.g. for the Warm/Cold study, one set
of files were WCP*.sas, one student used WCS*.sas, another WCM*.sas, etc,
Create an index of all management and analysis activities. This index should
describe the name and location (name of disk, where stored) of every data set;
the name, purpose and location (name of disk and notebook or file drawer) of
every program file. This index should be updated on a regular basis. For
week 07 7.34