The Basics of the SAS Language

SAS Statements

A SAS statement is a type of SAS language element that is used to perform a particular operation in a SAS program or to provide information to a SAS program. SAS statements are free-format. This means that they can begin and end anywhere on a line, that one statement can continue over several lines, and that several statements can be on the same line. Blank or special characters separate words in a SAS statement.
Tip
You can specify SAS statements in uppercase or lowercase. In most situations, text that is enclosed in quotation marks is case sensitive.
Here are two important rules for writing SAS programs:
  • A SAS statement ends with a semicolon.
  • A statement usually begins with a SAS keyword.
There are two types of SAS statements:
  • statements that are used in DATA and PROC steps
  • statements that are global in scope and can be used anywhere in a SAS program

Global Statements

Global statements are used anywhere in a SAS program and stay in effect until changed or canceled, or until the SAS session ends. Here are some common global statements: TITLE, LIBNAME, OPTIONS, and FOOTNOTE.

DATA Step

The DATA step creates or modifies data. The input for a DATA step can be of several types, such as raw data or a SAS data set. The output from a DATA step can be of several types, such as a SAS data set or a report. A SAS data set is a data file that is formatted in a way that SAS can understand.
For example, you can use DATA steps to do the following:
  • put your data into a SAS data set
  • compute values
  • check for and correct errors in your data
  • produce new SAS data sets by subsetting, supersetting, merging, and updating existing data sets

PROC Step

The PROC step analyzes data, produces output, or manages SAS files. The input for a PROC (procedure) step is usually a SAS data set. The output from a PROC step can be of several types, such as a report or an updated SAS data set.
For example, you can use PROC steps to do the following:
  • create a report that lists the data
  • analyze data
  • create a summary report
  • produce plots and charts

SAS Program Structure

A SAS program consists of a sequence of steps. A program can be any combination of DATA or PROC steps. A step is a sequence of SAS statements.
Here is an example of a simple SAS program.
Example Code 2.1 A Simple SAS Program
title1 'June Billing for Patients Older Than 39';     /*#1*/
data work.junefee;                                    /*#2*/
  set cert.admitjune;
  where age>39;
run;                                                  /*#3*/
proc print data=work.junefee;                         /*#4*/
run;
1 The TITLE statement is a global statement. Global statements are typically outside steps and do not require a RUN statement.
2 The DATA step creates a new SAS data set named Work.JuneFee. The SET statement reads in the data from Cert.AdmitJune. The new data set contains only those observations whose value for Age is greater than 39.
3 If a RUN or QUIT statement is not used at the end of a step, SAS assumes that the beginning of a new step implies the end of the previous step. If a RUN or QUIT statement is not used at the end of the last step in a program, SAS Studio and SAS Enterprise Guide automatically submit a RUN and QUIT statement after the submitted code.
4 The PROC PRINT step prints a listing of the new SAS data set. A PROC step begins with a PROC statement, which begins with the keyword PROC.
Output 2.1 PRINT Procedure Output
PRINT procedure output from a simple SAS program example.

Processing SAS Programs

When a SAS program is submitted for execution, SAS first validates the syntax and then compiles the statements. DATA and PROC statements signal the beginning of a new step. The beginning of a new step also implies the end of the previous step. At a step boundary, SAS executes any statement that has not been previously executed and ends the step.
Example Code 2.2 Processing SAS Programs
data work.admit2;               /*#1*/
  set cert.admit;
  where age>39;
proc print data=work.admit2;    /*#2*/
run;                            /*#3*/
1 The DATA step creates a new SAS data set named Work.Admit2 by reading Cert.Admit. The DATA statement is the beginning of the new step. The SET statement is used to read data. The WHERE statement conditionally reads only the observations where the value of the variable Age is greater than 39.
2 The PROC PRINT step prints the new SAS data set named Work.Admit2. The PROC PRINT statement serves as a step boundary in this example because a RUN statement was not used at the end of the DATA step. The PROC step also implies the end of the DATA step.
3 The RUN statement ends the PROC step.
Tip
The RUN statement is not required between steps in a SAS program. However, it is a best practice to use a RUN statement because it can make the SAS program easier to read and the SAS log easier to understand when debugging.

Log Messages

The SAS log collects messages about the processing of SAS programs and about any errors that occur. Each time a step is executed, SAS generates a log of the processing activities and the results of the processing.
When SAS processes the sample program, it produces the log messages shown below. Notice that you get separate sets of messages for each step in the program.
Log 2.1 SAS Log Messages for Each Program Step
5    data work.admit2;
6        set cert.admit;
7        where age>39;
8    run;

NOTE: There were 10 observations read from the data set CERT.ADMIT.
      WHERE age>39;
NOTE: The data set WORK.ADMIT2 has 10 observations and 9 variables.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds

9    proc print data=work.admit2;
NOTE: Writing HTML Body file: sashtml.htm
10   run;

NOTE: There were 10 observations read from the data set WORK.ADMIT2.
NOTE: PROCEDURE PRINT used (Total process time):
      real time           0.35 seconds
      cpu time            0.24 seconds

Results of Processing

The DATA Step

Suppose you submit the sample program below:
data work.admit2;
  set cert.admit;
  where age>39;
run;
When the program is processed, it creates a new SAS data set, Work.Admit2, containing only those observations with age values greater than 39. The DATA step creates a new data set and produces messages in the SAS log, but it does not create a report or other output.

The PROC Step

If you add a PROC PRINT step to this same example, the program produces the same new data set as before, but it also creates the following report:
data work.admit2;
  set cert.admit;
  where age>39;
run;
proc print data=work.admit2;
run;
Figure 2.1 PRINT Procedure Output
PRINT procedure output

Other Procedures

SAS programs often invoke procedures that create output in the form of a report, as is the case with the FREQ procedure:
proc freq data=sashelp.cars;
  table origin*DriveTrain;
run;
Figure 2.2 FREQ Procedure Output
FREQ Procedure Output
Other SAS programs perform tasks such as sorting and managing data, which have no visible results except for messages in the log. (All SAS programs produce log messages, but some SAS programs produce only log messages.)
proc copy in=cert out=work;  
   select admit;  
run;
Log 2.2 SAS Log: COPY Procedure Output
11   proc copy in=cert out=work;
12       select admit;
13   run;

NOTE: Copying CERT.ADMIT to WORK.ADMIT (memtype=DATA).
NOTE: There were 21 observations read from the data set CERT.ADMIT.
NOTE: The data set WORK.ADMIT has 21 observations and 9 variables.
NOTE: PROCEDURE COPY used (Total process time):
      real time           0.02 seconds
      cpu time            0.01 seconds

Last updated: August 23, 2018
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.201.168