Topics in SAS Programming
The topics in this tutorial have been selected to represent some of the most important tools in SAS for working with complex survey data. The focus is on the SAS data step language and some of the most basic SAS procedures you need to manipulate data in various ways, to manage it, and to prepare it for analysis.
Some knowledge of the SAS data step is assumed and can be reviewed in the first section "Review of the data step and how it works." The remaining topics--retained variables, BY groups, merging, and SAS arrays--are presented without assuming prior knowledge of these concepts. After introducing each concept with the simplest of examples, applications to real data are given. The section "Miscellaneous Tips and Tricks" summarizes useful information on a variety of topics including the two different SAS work environments (batch and interactive), SAS options, and the Output Delivery System.
SAS's tech support site:
Example programs to do data management.
Browse the pages for your desired sample, and then click the "Full code" tab to see the program.
Other useful resources:
CPC's FAQs on SASUNC-CH's introduction to SAS
UCLA's Stat Computing portal SAS page
Review of the data step and how it works
- A simple example
- statements: DATA, INFILE, INPUT, assignment, DATALINES, FILENAME, LIBNAME, RUN
- data step logic and default actions
- temporary/permanent SAS data sets
- the Program Data Vector
- executable/non-executable statements
- Keeping only the variables you want
- statements: KEEP, DROP, SET, IF-THEN-ELSE, OUTPUT
- data set options: KEEP=, DROP=
- Keeping only the observations you want
- statements: subsetting IF, DELETE
Retained variables
- What is a retained variable?
- RETAIN statement
- Simple example using a retained variable
Working with grouped observations
- What is a BY-group?
- Real data examples of grouped observations
- FIRST.byvariable and LAST.byvariable
- Program examples using the FIRST.byvariable and LAST.byvariable
- determining the uniqueness of an identifier variable
- creating a household level variable from person level data
- statements: SET, BY, RETAIN, IF-THEN, IF-THEN-ELSE, OUTPUT
Combining data by merging
- What is a match-merge?
- MERGE statement
- Examples of merging
- The IN= variables
- Program examples using the IN= variables
SAS arrays
- What is a SAS array?
- ARRAY statement to declare one and two-dimensional arrays
- Using arrays
- simple examples
- iterative DO
- DIM function
- restructuring a SAS data set
- simple examples
Miscellaneous "Tips and Tricks"
SAS Work Environment
- Batch
- Windowing Environment (aka Display Manager)
SAS Comments (Notes to explain your code)
- Syntax
- Header style
- Other styles
Frequently Used SAS System Options
- LINESIZE & PAGESIZE
- NONUMBER & PAGENO
- NOCENTER, NODATE & SKIP
- MISSING
SAS Data Library (How to use/open a SAS data set)
- Definition
- Accessing data sets
SAS Data Files
- Version 9 data set
- Version 8 data set
- Version 6 data set
- Transport format
Data Management
- Merge a single observation data set with a multi-observation data set
-
(insert next tip/trick here)
- (insert next tip/trick here)
Basic SAS Procedures with Some Useful Options
Processing a Limited Number of Observations
- OBS and FIRSTOBS - system, data set and proc options
- WHERE a condition is true
- Selecting a Random sample
- The _N_ variable
Output Delivery System (ODS)
- Traditional output
- Output data set
- HTML-formatted file


