SAS® 101
Based on
Learning SAS by Example:
A Programmer’s Guide
Chapters 3 & 4
C:\Program Files (x86)\Microsoft Office\MEDIA\OFFICE12\Lines\BD14768_.gif
By Tasha Chapman, Oregon Health Authority
Topics covered…
SAS libraries
Reading data from external files
txt and csv
Filename statement
Datalines
SET statement
Basic PROC Print
Basic PROC Contents
Basic PROC Freq
Basic PROC Means
SAS libraries
SAS libraries
LIBNAME statement assigns a libref
Libref (short for “Library Reference”) is an alias ornickname for a directory or folder for SAS datasets
SAS Datasets:
Permanent location of all SAS Datasets
SAS Datasets:
Permanent location of all SAS Datasets
Text and CSV:
Text and CSV data files used to create theSAS Datasets
Text and CSV:
Text and CSV data files used to create theSAS Datasets
Would assignlibref usingLIBNAMEstatement
LIBNAME statement:
Assigns a libref
Libref is an alias for a directoryor folder where you storepermanent SAS datasets
Libref can be anything youchoose
Libref only exists for current SASsession
LIBNAME statement:
Assigns a libref
Libref is an alias for a directoryor folder where you storepermanent SAS datasets
Libref can be anything youchoose
Libref only exists for current SASsession
SAS libraries
SAS libraries
LIBNAME statement assigns a libref
Libref (short for “Library Reference”) is an alias ornickname for a directory or folder for SAS datasets
Dataset references contain two parts:
libref
dataset-name
Looks like: libref.dataset-name
If libref is blank, the default is the Work library
Dataset reference:
Consists of two parts –
Libref.dataset-name
mozart.test_scores is short forc:\books\learning\test_scores
Default is Work
Dataset reference:
Consists of two parts –
Libref.dataset-name
mozart.test_scores is short forc:\books\learning\test_scores
Default is Work
SAS libraries
SAS work library
Work is a temporary library
SAS datasets created in Work only exist during SASsession
Once SAS session ends, datasets are erased
Do not need to assign a libref for Work or specifyit in dataset references
data Test_Scores;  is the same asdata work.Test_Scores;
LIBNAME statement:
Assigns a libref
Use the libref for saving dataand for retrieving data
LIBNAME statement:
Assigns a libref
Use the libref for saving dataand for retrieving data
SAS libraries
Explorer Window:
See libraries andSAS datasets
Explorer Window:
See libraries andSAS datasets
Active Libraries:
Double click on alibrary to see thedatasets in it
Active Libraries:
Double click on alibrary to see thedatasets in it
LIBNAME examples
Oops!
Your password isshowing!
Oops!
Your password isshowing!
LIBNAME trick
Save your commonly used and/or passwordedLIBNAME statements in a text file (using Notepad)
Use a %include statement to reference the text fileat the beginning of every SAS program
SAS will include the code inthe text file as if it werepart of your program.
SAS will include the code inthe text file as if it werepart of your program.
Reading external data
Four variables: Gender, Age, Height (in inches), Weight (in pounds)
Variables separated by blanks
Four variables: Gender, Age, Height (in inches), Weight (in pounds)
Variables separated by blanks
Reading data from a text file
INFILE – where to find the data
INPUT – variable names to associate with each data value
($ indicates character variable.  Otherwise numeric.)
INFILE – where to find the data
INPUT – variable names to associate with each data value
($ indicates character variable.  Otherwise numeric.)
Reading data from a text file
C:\Users\nchapman\AppData\Local\Microsoft\Windows\Temporary Internet Files\Content.IE5\F4ZMF9H0\MC900311014[1].wmf
Results of PROC Print of “Demographics”
Obs – short for “observation” (part of PROC Print output)
Numbers observations from 1 to N
Results of PROC Print of “Demographics”
Obs – short for “observation” (part of PROC Print output)
Numbers observations from 1 to N
Reading data from a text file
Four variables: Gender, Age, Height (in inches), Weight (in pounds)
Variables separated by commas
Four variables: Gender, Age, Height (in inches), Weight (in pounds)
Variables separated by commas
Reading data from a csv file
dsd option (delimiter-sensitive data):
  Changes default delimiter from blank to comma
  If two delimiters in a row, assumes missing value between
  Quotes stripped from character values
dsd option (delimiter-sensitive data):
  Changes default delimiter from blank to comma
  If two delimiters in a row, assumes missing value between
  Quotes stripped from character values
Reading data from a csv file
Results of PROC Print of “Demographics”
SAS data results are the same
Results of PROC Print of “Demographics”
SAS data results are the same
Reading data from a csv file
Other delimiters
Use the dlm= (or delimiter=) option to specifydata delimiters other than blanks or commas
Example: infile 'D:\Data\mydata.txt' dlm=':';
Can use dsd and dlm= options together
Performs all functions of dsd, but overrides defaultdelimiter
Filename
FILENAME statement assigns a fileref
Fileref (short for “File Reference”) is an alias ornickname for an external file
Filename
Useful when you need to read two or more fileswith same format (such as quarterly data)
Datalines
Allows dataset to be created within SAS program
Can be useful for creating a quick set of test data
Use either datalines or cards options
Follow with semi-colon after last line of data
SET statement
After you’ve brought your data into a SAS dataset,most of your DATA steps will look like this:
SET statement
 Creates a newdataset called“Females”
 Uses previousdataset “AllData” asthe basis of the newdataset
Applies these modifications to the new dataset
SET statement
The SET statement is similar to an INPUT statement
Except instead of a raw data file, you are readingobservations from a SAS dataset
Can read in temporary or permanent SAS datasets
PROC Print
PROC Print
PROC Print can be used to list the data in a SASdataset
Results of PROC Print of “Demographics”
Results of PROC Print of “Demographics”
PROC Print
PROC Print
Many options to control output of PROC Print
noobs – Suppresses “OBS” column in output
(obs=2) – Only prints the first two observations
Can put in any number: 1 through N
Must be placed in parentheses after data= option
var statement – Only prints listed variables
We’ll discuss other PROC Print options inlater chapters
We’ll discuss other PROC Print options inlater chapters
PROC Print
PROC Contents
PROC Contents
PROC Contents can be used to display themetadata (descriptor portion) of the SAS dataset
PROC Contents
Results of PROC Contents of“Demographics”
Results of PROC Contents of“Demographics”
PROC Contents
Number ofobservationsand variables
Variable list
Dataset name
File name
PROC Contents variable list
# - Variable number (varnum)
Variable – Name of variable
Type – Numeric or Character
Len – Variable length
Format – How the data is displayed
Informat – How the data was read by SAS
Variables listed in alphabetical order by default
Uppercase alphabetized before lowercase (e.g.,“ZZTOP” would be alphabetized before “aerosmith”)
Use the varnum option to list variables in orderthey were created in
PROC Contents variable list
PROC Freq
PROC Freq
PROC Freq can be used to run simple frequencytables on your data
PROC Freq
Results of PROC Freq of“Demographics”
Results of PROC Freq of“Demographics”
Use the table statement to only print selectedvariables
Use the nocum option to suppress cumulativestatistics
Use the nopercent option to suppress percentstatistics
Can use options together or separately
PROC Freq
PROC Freq
Can create simple cross-tabulations
PROC Freq
Use the list option to display cross-tab tables ina list format
PROC Means
PROC Means
PROC Means can be used to run simple summarystatistics on your data
Results of PROC Means of “Demographics”
Results of PROC Means of “Demographics”
PROC Means
PROC Means
Many options to control output of PROC Means
NMiss Mean Median – Examples of statistics thatcan be specified in PROC Means
(see later slide for list of statistical keywords)
class statement – Allows for grouping by categoricalvariables
var statement – Only provides statistics for listedanalysis variables
We’ll discuss other PROC Freq and PROC Means options in laterchapters
We’ll discuss other PROC Freq and PROC Means options in laterchapters
PROC Means
PROC Means
Examples of statisticsthat can be run withPROC Means
Examples of statisticsthat can be run withPROC Means
Read chapters 5 & 6
and sections 3.9 through 3.14
For next week…