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 (procedure) step analyzes data, produces output, or manages SAS files. The input for a PROC 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
  • produce descriptive statistics
  • create a summary report
  • produce plots and charts

A Simple SAS Program

This program uses an existing SAS data set to create a new SAS data set containing a subset of the original data set. It then prints a listing of the new data set using PROC PRINT.
data sasuser.admit2;
   set sasuser.admit;
   where age>39;
run;
proc print data=sasuser.admit2;
run;
The sample SAS program contains a DATA step and a PROC step. The DATA step produced a new SAS data set. Only those observations with an age value greater than 39 are written to the new SAS data set.
A DATA step begins with a DATA statement, which begins with the keyword DATA. A PROC step begins with a PROC statement, which begins with the keyword PROC. The sample program contains the following statements:
Table 2.1 SAS Program Statements
Statements
Sample Program Code
DATA statement
data sasuser.admit2;
SET statement
  set sasuser.admit;
Additional programming statements
   where age>39; 
RUN statement
run; 
PROC PRINT statement
proc print data=sasuser.admit2;
RUN statement
run; 

Processing SAS Programs

When you submit a SAS program, SAS begins reading the statements and checking them for errors.
DATA and PROC statements signal the beginning of a new step. The RUN statement (for most procedures and the DATA step) and the QUIT statement (for some procedures) mark step boundaries. The beginning of a new step (DATA or PROC) also implies the end of the previous step. At a step boundary, SAS executes any statements that have not previously executed and ends the step. In the sample program, each step ends with a RUN statement.
data sasuser.admit2;
   set sasuser.admit;
   where age>39;
run;
proc print data=sasuser.admit2;
run;
Tip
The RUN statement is not required between steps in a SAS program. 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 sasuser.admit2;
6        set sasuser.admit;
7        where age>39;
8    run;

NOTE: There were 10 observations read from the data set SASUSER.ADMIT.
      WHERE age>39;
NOTE: The data set SASUSER.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=sasuser.admit2;
NOTE: Writing HTML Body file: sashtml.htm
10   run;

NOTE: There were 10 observations read from the data set SASUSER.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 sasuser.admit2;
	set sasuser.admit;
	where age>39;
run;
When the program is processed, it creates a new SAS data set (sasuser.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, which is displayed in HTML:
data sasuser.admit2;
	set sasuser.admit;
	where age>39;
run;
proc print data=sasuser.admit2;
run;
Note: The default output in SAS Enterprise Guide is SAS Report. To change the default output in SAS Enterprise Guide to HTML, click Tools and select Options>Results>Results General. Then select HTML. Ensure that you have cleared SAS Report.
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=sasuser out=work;  
   select admit;  
run;
Log 2.2 SAS Log: COPY Procedure Output
11   proc copy in=sasuser out=work;
12       select admit;
13   run;

NOTE: Copying SASUSER.ADMIT to WORK.ADMIT (memtype=DATA).
NOTE: There were 21 observations read from the data set SASUSER.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: January 10, 2018
..................Content has been hidden....................

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