Dr. Alex Dong's Blog

SAS Series 4: Create and run SAS programs

Overview

As you discovered in See an overview of SAS, you can use SAS programs to access, manage, analyze, and present your data. The SAS programming language is both powerful and flexible. You can program any number of analyses and reports with it. The SAS language contains statements, expressions, functions and CALL routines, options, formats, and informats–elements that many programming languages share.

SAS can also simplify programming for you with its library of built-in programs known as SAS procedures. SAS procedures use data values from SAS data sets to produce preprogrammed reports, requiring minimal effort from you. Base SAS procedures enable you to

  • manipulate data
  • store and retrieve information
  • perform statistical analysis
  • create reports.

This task shows you the main characteristics of SAS programs. It also guides you through the basic steps of creating, running, and managing SAS programs and their output.

1

Components of SAS programs

Let’s begin by looking at a simple SAS program. The sample SAS program below contains two steps: a DATA step and a PROC step. These two types of steps, alone or combined, form all SAS programs.

2

DATA steps typically create or modify SAS data sets, but they can also be used to produce custom-designed reports. For example, you can use DATA steps to

  • put your data into a SAS data set
  • compute the values for new variables
  • check for and correct errors in your data
  • produce new SAS data sets by subsetting, merging, and updating existing data sets.

In the sample program, the DATA step creates a new SAS data set ADMIT2 in the CLINIC library using the existing SAS data set ADMIT in the CLINIC library.

3

PROC (procedure) steps typically analyze and process data in the form of a SAS data set, and they sometimes create SAS data sets that contain the results of the procedure. PROC steps control a library of prewritten routines (procedures) that perform tasks on SAS data sets, such as listing, sorting, and summarizing data. For example, you can use PROC steps to

  • print a report
  • produce descriptive statistics
  • create a tabular report
  • produce plots and charts.

The sample program uses the PRINT procedure, which prints the data in a data set. The DATA= option tells SAS what data to use for the procedure.

4

Characteristics of SAS Programs

Next let’s look at the individual statements in our sample program. SAS programs are made up of SAS statements. A SAS statement has two important characteristics:

  • It usually begins with a SAS keyword.
  • It always ends with a semicolon.

A DATA step begins with the keyword DATA, and a PROC step begins with the keyword PROC. Generally, a step ends with a RUN statement or when a new DATA or PROC step begins. A RUN statement tells SAS to process all the preceding lines of the step.

5

SAS statements are free-format. This means that

  • they can begin and end anywhere on a line
  • one statement can continue over several lines
  • several statements can be on a line.

SAS statements are not case sensitive. Blanks or special characters separate the “words” in a SAS statement.

Copy a SAS program into the Editor window

Let’s try running a SAS program that is similar to the sample you just saw. This program creates a new SAS data set from an existing SAS data set and then prints a listing of the new data set.

The Mylib.ProductSales data set was created in Work with SAS data sets. If you didn’t create it, return to the task to define the library and create the data set before proceeding here.

  1. Copy and paste or type the following code into the Editor window:
    data test;
       set mylib.productsales;
    run;
    proc print data=test;
    run;
  2. Notice that when you paste this text into the Editor window, SAS keywords are color-coded and the program steps are separated into expandable blocks.6The DATA step creates a new SAS data set named Test. Notice that Test is a one-level name. Because no library name is specified for the Test data set, SAS uses the Work library to temporarily store the file. The Test data set is created from the ProductSales data set that you stored in your MyLib library. Notice that mylib.productsales is a two-level name.The PROC PRINT step lists all the data in the Test data set.
    If the USER library is assigned, SAS uses the USER library rather than the WORK library for one-level names.

 

Submit the program and view output When you submit a SAS program, SAS compiles and executes the code and returns any results to the Results Viewer window.

 

  1. With the Editor window active, select Run-> Submit.To submit only a portion of a program in the Editor window, highlight the portion you want to submit, right-click the highlighted area, and select Submit Selection.
    You just submitted a program interactively using the Editor window. You can also submit SAS programs using

    • batch or background mode
    • interactive line mode
    • noninteractive line mode.
  2. The Results Viewer window comes to the front. Scroll the window and notice that the PRINT procedure has printed all the data from the ProductSales data set. In addition, the procedure has automatically formatted the data into rows and columns.

7

View the logEach time a step is executed, SAS generates a log of the processing activities and the results of the processing. The SAS log collects messages about the processing of SAS programs and any errors that may occur.

 

  1. Click the Log window to activate it.8
  2. Notice that there are separate messages for the DATA and PROC steps. The messages from the DATA step tell you that SAS read 1440 observations from the ProductSales data set and that there are 1440 observations and 10 variables in the new data set Test. The messages from the PROC step tell you that 1440 observations were read during the PRINT procedure. There are no error messages.
    9 It’s important that you understand how SAS processes DATA steps (one observation at a time) and PROC steps. You should also learn methods and tips for debugging SAS programs. For details on
adminSAS Series 4: Create and run SAS programs