SAS – Basic Syntax and Program Structure

Last updated on Dec 13 2021
Vaidehi Reddy

Table of Contents

SAS – Basic Syntax and Program Structure

Like any other programming language, the SAS language has its own rules of syntax to create the SAS programs.

The three components of any SAS program – Statements, Variables and Data sets follow the below rules on Syntax.

SAS Statements

  • Statements can start anywhere and end anywhere. A semicolon at the end of the last line marks the end of the statement.
  • Many SAS statements can be on the same line, with each statement ending with a semicolon.
  • Space can be used to separate the components in a SAS program statement.
  • SAS keywords are not case sensitive.
  • Every SAS program must end with a RUN statement.

SAS Variable Names

Variables in SAS represent a column in the SAS data set. The variable names follow the below rules.

  • It can be maximum 32 characters long.
  • It can not include blanks.
  • It must start with the letters A through Z (not case sensitive) or an underscore (_).
  • Can include numbers but not as the first character.
  • Variable names are case insensitive.

Example

# Valid Variable Names
REVENUE_YEAR
MaxVal
_Length
# Invalid variable Names
Miles Per Liter       #contains Space.
RainfFall%      # contains apecial character other than underscore.
90_high                  # Starts with a number.

SAS Data Set

The DATA statement marks the creation of a new SAS data set. The rules for DATA set creation are as below.

  • A single word after the DATA statement indicates a temporary data set name. Which means the data set gets erased at the end of the session.
  • The data set name can be prefixed with a library name which makes it a permanent data set. Which means the data set persists after the session is over.
  • If the SAS data set name is omitted then SAS creates a temporary data set with a name generated by SAS like – DATA1, DATA2 etc.

Example

# Temporary data sets.

DATA TempData;
DATA abc;
DATA newdat;

# Permanent data sets.
DATA LIBRARY1.DATA1
DATA MYLIB.newdat;

SAS File Extensions

The SAS programs, data files and the results of the programs are saved with various extensions in windows.

  • *.sas − It represents the SAS code file which can be edited using the SAS Editor or any text editor.
  • *.log − It represents the SAS Log File it contains information such as errors, warnings, and data set details for a submitted SAS program.
  • *.mht / *.html −It represents the SAS Results file.
  • *.sas7bdat −It represents SAS Data File which contains a SAS data set including variable names, labels, and the results of calculations.

Comments in SAS

Comments in SAS code are specified in two ways. Below are these two formats.

*message; type comment

A comment in the form of *message; cannot contain semicolons or unmatched quotation mark inside it. Also, there should not be any reference to any macro statements inside such comments. It can span multiple lines and can be of any length. Following is a single line comment example −

* This is comment;

Following is a multiline comment example −

* This is first line of the comment

* This is second line of the comment;

/*message*/ type comment

A comment in the form of /*message*/ is used more frequently and it can not be nested. But it can span multiple lines and can be of any length. Following is a single line comment example −

/* This is comment */

Following is a multiline comment example −

/* This is first line of the comment

* This is second line of the comment */

SAS – Program Structure

The SAS Programming involves first creating/reading the data sets into the memory and then doing the analysis on this data. We need to understand the flow in which a program is written to achieve this.

SAS Program Structure

The below diagram shows the steps to be written in the given sequence to create a SAS Program.

image001 14
written

Every SAS program must have all these steps to complete reading the input data, analyzing the data and giving the output of the analysis. Also, the RUN statement at the end of each step is required to complete the execution of that step.

DATA Step

This step involves loading the required data set into SAS memory and identifying the variables (also called columns) of the data set. It also captures the records (also called observations or subjects). The syntax for DATA statement is as below.

Syntax

DATA data_set_name;                         #Name the data set.
INPUT var1,var2,var3;                          #Define the variables in this data set.
NEW_VAR;                                           #Create new variables.
LABEL;                                                   #Assign labels to variables.
DATALINES;                                           #Enter the data.
RUN;

Example

The below example shows a simple case of naming the data set, defining the variables, creating new variables and entering the data. Here the string variables have a $ at the end and numeric values are without it.

DATA TEMP;
INPUT ID $ NAME $ SALARY DEPARTMENT $;
comm = SALARY*0.25;
LABEL ID = 'Employee ID' comm = 'COMMISION';
DATALINES;
1 Rick 623.3 IT
2 Dan 515.2 Operations
3 Michelle 611 IT
4 Ryan 729 HR
5 Gary 843.25 Finance
6 Nina 578 IT
7 Simon 632.8 Operations
8 Guru 722.5 Finance
;
RUN;

PROC Step

This step involves invoking a SAS built-in procedure to analyse the data.

Syntax

PROC procedure_name options; #The name of the proc.

RUN;

Example

The below example shows using the MEANS procedure to print the mean values of the numeric variables in the data set.

PROC MEANS;
RUN;

The OUTPUT Step

The data from the data sets can be displayed with conditional output statements.

Syntax

PROC PRINT DATA = data_set;
OPTIONS;
RUN;

Example

The below example shows using the where clause in the output to produce only few records from the data set.

PROC PRINT DATA = TEMP;
WHERE SALARY > 700;
RUN;

The complete SAS Program

Below is the complete code for each of the above steps.

image002 18
code

Program Output

The output from above code is seen in the RESULTS tab.

image003 11
RESULTS

So, this brings us to the end of blog. This Tecklearn ‘SAS – Basic Syntax and Program Structure’ blog helps you with commonly asked questions if you are looking out for a job in SAS. If you wish to learn SAS and build a career in Data Analytics domain, then check out our interactive, SAS Training for SAS BASE Certification Training, that comes with 24*7 support to guide you throughout your learning period. Please find the link for course details:

https://www.tecklearn.com/course/sas-training-for-sas-base-certification/

SAS Training for SAS BASE Certification Training

About the Course

SAS Certification Training is intended to make you an expert in SAS programming and Analytics. You will be able to analyse and write SAS code for real problems, learn to use SAS to work with datasets, perform advanced statistical techniques to obtain optimized results with Advanced SAS programming.  In this SAS online training course, you will also learn SAS macros, Machine Learning, PROC SQL, procedure, statistical analysis and decision trees. You will also work on real-life projects and prepare for the SAS Certified Base Programmer certification exam. Upon the completion of this SAS online training, you will have enough proficiency in reading spreadsheets, databases, using SAS functions for manipulating this data and debugging it.

Why Should you take SAS Training?

  • The average salary for a Business Intelligence Developer skilled in SAS is $100k (PayScale salary data)
  • SAS, Google, Facebook, Twitter, Netflix, Accenture & other MNCs worldwide are using SAS for their Data analysis activities and advance their existing systems.
  • SAS is a Leader in 2017 Gartner Magic Quadrant for Data Science Platform.

What you will Learn in this Course?

Introduction to SAS 

  • Introduction to SAS
  • Installation of SAS
  • SAS windows
  • Working with data sets
  • Walk through of SAS windows like output, search, editor etc

SAS Enterprise Guide

  • How to read and subset the data sets
  • SET Statement
  • Infile and Infile Options
  • SAS Format -Format Vs Informat

SAS Operators and Functions

  • Using Variables
  • Defining and using KEEP and DROP statements
  • Output Statement
  • Retain Statement
  • SUM Statement

Advanced SAS Procedures

  • PROC Import
  • PROC Print
  • Data Step Vs Proc
  • Deep Dive into Proc

Customizing Datasets

  • SAS Arrays
  • Useful SAS Functions
  • PUT/INPUT Functions
  • Date/Time Functions
  • Numeric Functions
  • Character Functions

SAS Format and SAS Graphs

  • SAS Format statements
  • Understanding PROC GCHART, various graphs, bar charts: pie, bar

Sorting Techniques

  • NODUP
  • NODUKEY
  • NODUP Vs NODUKEY

Data Transformation Function

  • Character functions, numeric functions and converting variable type
  • Use functions in data transformation

Deep Dive into SAS Procedures, Functions and Statements

  • Find Function
  • Scan Function
  • MERGE Statement
  • BY Statement
  • Joins
  • Procedures Vs Function
  • Where Vs If
  • What is Missover
  • NMISS
  • CMISS

PROC SQL

  • SELECT statement
  • Sorting of Data
  • CASE expression
  • Other SELECT statement clauses
  • JOINS and UNIONS

Using SAS Macros

  • Benefits of SAS Macros
  • Macro Variables
  • Macro Code Constituents and Macro Step
  • Positional Parameters to Macros

Got a question for us? Please mention it in the comments section and we will get back to you.

0 responses on "SAS - Basic Syntax and Program Structure"

Leave a Message

Your email address will not be published. Required fields are marked *