Recommended Reading: Explore top academic reference textbooks on Applied Statistics & Informatics. Shop Books on Amazon →

As an Amazon Associate, we earn from qualifying purchases. This comes at no additional cost to you.

Introduction to SAS Programming

Statistical Analysis System (SAS) software became an integral part of any data analysis process especially in the clinical research domain.

SAS programming environment consists of two main steps namely DATA step and PROC step. Data step is the starting point of creating data sets for the SAS programs from the raw data. PROC step helps us to analyze the data and generate the desired output of the analysis.

We will start with the first step namely the Data step.

1. Data Step

Data step has the following the two types of syntax:

First Type

The first type of data step statement reads the data directly from the user input through input and datalines statements.

data <dataset name>; input <variable list>; datalines;
Example 1a
data dataset1; input age name $ weight; datalines; 23 name1 76 ;

Second Type

The second type of data step statement includes an infile statement. It reads the data from the external files sources such as dat, spss and excel files.

data <dataset name>; infile <path of the external data file>; input <variable list of the external file>;
Example 1b
data dataset1; infile 'd:\sasdata.dat'; input age name $ weight; run;

1.1 libname statement

In the above, the data set which is created will be stored temporarily in the SAS working environment. It will be deleted once we exit the SAS. To store the data set permanently "libname" statement is used. The following is the syntax of the statement:

libname <name of the library to be referred termed as libref> <path of directory where the data set is to be stored>;
Example 1.1
libname sdata "d:\sasdataset";

In the above example the 'sdata' is the library reference which will be used to indicate the directory in which the data set is being stored permanently. If we don't use the libname statement then SAS will store the data set in the temporary working directory which is referred the library name 'work' (libref). We don't need to mention it explicitly as it will be taken care by SAS.

Additional options can be used in the file statements with delimiters (',', ';') to indicate that the variables are separated by delimiters not by spaces which is the default option.

1.2 Input Statement

Input statement is the common one in both of the above types of the data step which takes the following types of commonly used formats: list, column and formatted input statement.

a. Input statement with list format

The simplest form of input statement is the list form where the variables are listed and character or string variables are indicated by $ sign wherein the data values represented in the infile statement are simply separated by spaces. In this format the missing values are to be represented by '.' and the string variables cannot have spaces and the maximum default length of the string variable is 8 only.

Example 1.2a
data sdata.dataset1; input age name $ weight; datalines; 28 sn1 56 ;

b. Input statement with column format

This type of input statement overcomes the drawbacks of the list format by indicating the starting column and ending columns of the data values in the infile statement and if the variables contain only one value then only one column is indicated.

Example 1.2b
input age 1-3 name $ 5-25 weight 27-30;

c. Formatted input statement

The variables in the input statement are represented by its input formats called "informats" in SAS. The variables in the input statements are followed by the format and '.'

For example the string variables will be followed by $ sign, number of storage spaces it takes followed by '.'.

The numeric variables will be followed by the number of digits say 5 plus if the numeric variable has 2 decimal places it will be denoted by 5.2.

Similarly the date variables will be represented by date format such as "mmddyyyy" or "ddmmyy".

1.3 Set Statement

If we want to use an existing SAS data set, then the set statement is used in the data step.

Example 1.3
data sdata.dataset2; set sdata.dataset1; run;

In the above statement the new data set 'dataset2' is created from the existing SAS data set 'dataset1'.

1.4 Modifying the SAS data set

Assignment statement helps us to create and modify the existing variables and also it helps us to modify values of the variables in the SAS data set.

1.4.1 Assignment statement

To create a new variable the following syntax is used:

New variable = mathematical expression of existing variable(s);
Example 1.4.1
profit = income - expenditure;

1.4.2 If Then Else statement

If we want to modify the existing values of a variable or assign new values based on values of some other variables, if-then and else statements can be used. The following is the syntax:

if <condition> then <option1>; else <option2>;

Condition in the if statement can include the logical and comparison operators such as:

  • equal to (=)
  • not equal to (ne)
  • less than (<)
  • greater than (>)
  • and (&)
  • or (|)
  • not
Example 1.4.2
if hb > 14 and gender = 'M' then status = 'normal'; else status = 'weak';

The if statements must be used along with the input or set statements otherwise a 'variable uninitialized' error will be shown by the SAS log.

1.4.3 do end statement

If we want to repeat or execute a group of statements it can be placed within the do-end statement. The syntax to execute a group of statements:

if hb > 14 then do; age = 100; gender = 'F'; end;

The above statement will assign age = 100 and gender = 'F' for all the observations where hb > 14.

1.4.4 Arrays

Arrays are used to store variables with similar structures. Arrays can be used to carry out similar actions on the variables using a do-end statement. Array statement has the following syntax:

array <array name>{number of variables} <variable name list>;
Example 1.4.4
array a{10} a1-a10; do i = 1 to 10; a{i} = 4; end;

The above example will assign value 4 to all the 10 variables.

1.4.5 Deleting Variables

If we want to delete a variable in the data set, drop or keep statements can be used. The drop statement is used to drop the variables from the data set and its syntax is as follows:

drop <variable1 variable2 ...>;

The keep statement does the same in reverse, keeping only the variables explicitly needed in the data set:

keep <variable1 variable2 ...>;

Both statements must be used within the data step block.

Example 1.4.5.1
data sdata.dataset2; set sdata.dataset1; keep age weight; run;
Example 1.4.5.2
data sdata.dataset2; set sdata.dataset1; drop name; run;

1.4.6 Deleting observations

We can delete observations of a particular variable using the delete statement. Delete statement is used mostly with an if statement.

Example 1.4.6
if age < 1 then delete;

Here the observations with age less than 1 will be deleted. It has to be used with the data step statements.

1.4.7 Sub setting or splitting data sets

If we want to split the data set into groups, we can use the set statement along with the if condition to parse data sets into specific groups.

Example 1.4.7
data sdata1.diabetic; set sdata1.dataset1; if dstatus = 'y'; run;

1.4.8 Concatenating or combining data sets

If we want to combine two data sets vertically, it can be done with the help of the set statement.

Example 1.4.8
data sdata1.overall; set sdata1.diabetic sdata1.nondiabetic; run;

1.4.9 Merging data sets or adding variables to the existing data sets

If we want to merge two data sets, the two data sets should have at least one common variable. The syntax of the merge statement is as follows:

data <new dataset>; merge <dataset1> <dataset2>; by <identifier variable>;
Example 1.4.9
data dataset5; merge dataset3 dataset4; by patientno; run;

2. PROC STEP

Proc step is the next component of the SAS programming environment. It helps us to analyze the data sets which are brought into the SAS environment. It has the following syntax:

proc <name of the procedure> data=<name of the dataset>; <procedure statement 1>; <procedure statement 2>; ... <procedure statement n>; run;

If we don't specify the data set, SAS will consider the latest created data set for the analysis purpose.

SAS has a number of statistical procedures which are used to do the analysis at the univariate and multivariate levels. Procedures help us to carry out descriptive, inferential analysis, predictive analytics, and building different statistical models.

The following are some of the primary baseline parameters used in procedure statements:

  • var statement: Specifies variables to be used by the procedure step.
    var <variable name1 variable name2 ...>;
    Example 2.1
    var name age weight;
  • where statement: Specifies which observations to be used by the procedure step based on the logical conditions provided.
    where <logical expression>;
    Example 2.2
    where age > 30 and weight < 60;
  • by statement: Used to analyze data in groups. The variable in the by statement should be categorical. The data set must be sorted by this variable first.
    by <variable name1 variable name2 ...>;
    Example 2.3
    proc means data=dataset1; var weight; by gender; run;
  • class statement: Similar to the by statement, except it can handle numeric variables with fewer groups, and the output will be displayed cleanly inside a single unified table (whereas by statements separate tables for each group).
    class <variable name1 variable name2 ...>;
    Example 2.4
    proc means data=dataset1; var age; class weight; run;

3. SAS Procedures

The following categories outline some of the commonly used SAS procedures related to data preparation, descriptive analysis, inferential analysis, model building, graphing, and output generation:

3.1 Data Preparation Level Procedures

  • proc sort - Sorts observations by one or more variables.
  • proc format - Creates user-defined formats to map value labels.
  • proc transpose - Restructures datasets from wide-to-long or long-to-wide formats.

3.2 Descriptive Analysis Procedures

  • proc freq - Produces frequency counts and cross-tabulation tables.
  • proc means - Computes descriptive statistics for numeric variables.
  • proc univariate - Delivers detailed high-level data distributions and normality metrics.
  • proc summarize - Produces summary statistics structures cleanly.
  • proc tabulate - Generates complex descriptive summary tables in block styles.
  • proc corr - Computes correlation coefficients.

3.3 Inferential Analysis & Modeling

  • proc anova - Performs Analysis of Variance for balanced designs.
  • proc reg - Computes multivariate linear regressions.
  • proc ttest - Conducts independent and paired group t-tests.
  • proc glm - Fits General Linear Models (handles unbalanced designs).
  • proc genmod - Fits Generalized Linear Models.
  • proc mixed - Performs analysis on mixed linear models (fixed/random effects).
  • proc npar1way - Performs non-parametric tests on one-way classifications.

3.4 Report, Graph, and Output Preparation

  • proc print - Prints listed data observations into view windows.
  • proc content - Describes internal descriptor structures of a dataset.
  • proc plot - Generates traditional low-resolution line/scatter plots.
  • proc gplot - Produces high-resolution graphics charts.
  • proc boxplot - Creates side-by-side distribution box plots.