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:
Browse the pages for your desired sample, and then click the "Full code" tab to see the program.
Other useful resources:
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
- What is a retained variable?
- RETAIN statement
- Simple example using a retained variable
- 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
- What is a match-merge?
- MERGE statement
- Examples of merging
- The IN= variables
- Program examples using the IN= variables
- What is a SAS array?
- ARRAY statement to declare one and two-dimensional arrays
- Using arrays
Miscellaneous "Tips and Tricks"
- Windowing Environment (aka Display Manager)
- Header style
- Other styles
- LINESIZE & PAGESIZE
- NONUMBER & PAGENO
- NOCENTER, NODATE & SKIP
- Accessing data sets
- Version 9 data set
- Version 8 data set
- Version 6 data set
- Transport format
- Merge a single observation data set with a multi-observation data set
Basic SAS Procedures with Some Useful Options
- OBS and FIRSTOBS - system, data set and proc options
- WHERE a condition is true
- Selecting a Random sample
- The _N_ variable
- Traditional output
- Output data set
- HTML-formatted file