SAS - Basic Syntax

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.
Statements can start anywhere and end anywhere. A semicolon at the end of the last line marks the end of the statement.
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.
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.
Space can be used to separate the components in a SAS program statement.
SAS  keywords are not case sensitive.
SAS  keywords are not case sensitive.
Every SAS program must end with a RUN statement.
Every SAS program must end with a RUN statement.
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 be maximum 32 characters long.
It can not include blanks.
It can not include blanks.
It must start with the letters A through Z (not case sensitive) or an underscore (_).
It must start with the letters A through Z (not case sensitive) or an underscore (_).
Can include numbers but not as the first character.
Can include numbers but not as the first character.
Variable names are case insensitive.
Variable names are case insensitive.
# 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.
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.
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.
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.
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.
# Temporary data sets.
DATA TempData;
DATA abc;
DATA newdat;

# Permanent data sets.
DATA LIBRARY1.DATA1
DATA MYLIB.newdat;
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.
*.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.
*.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.
*.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.
*.sas7bdat −It represents SAS Data File which contains a SAS data set including variable names, labels, and the results of calculations.
Comments in SAS code are specified in two ways. Below are these two formats.
A comment in the form of *message; can not 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;

 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 */


    50 Lectures 
     5.5 hours 

 Code And Create

    124 Lectures 
     30 hours 

 Juan Galvan

    162 Lectures 
     31.5 hours 

 Yossef Ayman Zedan

    35 Lectures 
     2.5 hours 

 Ermin Dedic

    167 Lectures 
     45.5 hours 

 Muslim Helalee
 Print
 Add Notes
 Bookmark this page