Best SAS Syntax Tutorial – Grasp the SAS Statements & Comments
We are going to study SAS syntax and concepts related to the syntax of SAS programming language. Whenever we write a program, it is important to keep in mind the syntax and the set of rules that the program must follow. Like all languages, SAS programming language also follows a set of rules while creating a program, called SAS Syntax. We will study various conventions of SAS syntax in detail.
Let’s quickly start the SAS syntax tutorial.
SAS Syntax Statements
Here are the conditions for executing SAS syntax statements:
- Statements in SAS can begin anywhere and can end anywhere.
- Every statement should end with a semicolon.
- SAS allows statements to be on the same or multiple lines, with each statement ending with a semicolon.
- The components in the statement of a SAS program can be separated using spaces.
- SAS keywords are not case sensitive but it is nice if uppercase letters are used so that the program looks more neat, organized and easy to understand.
- Every SAS program must end with a RUN statement.
- SAS statements can either be in uppercase or in lowercase.
- Statements can start in any column.
Have you completed? – SAS Operators Guide
Fundamental SAS Syntax-Rules:
SAS statements have these characteristics:
- Usually, begin with an identifying keyword.
- Always end with a semicolon
data staff; input LastName $ FirstName $ JobTitle $ Salary; datalines; ...insert text here... run; proc print data=staff; run;
1. Quotation Marks
SAS Programming has the ability to recognize text when in quotation marks (“xyz”) or apostrophes (‘xyz’). Either of the two can be used, but be sure that the text block should start and end with the same one. When SAS changes the color of the words into a purplish-pink color, you will know that you have followed the rules correctly. It is important to note that if your text contains an apostrophe, then you must enclose it with quotation marks. Check an illustration of the same:
“Here is a sentence” -> Correct use of two quotation marks.
‘Here is a sentence’ -> Correct use of two apostrophes.
‘Here’s a sentence’ -> Incorrect use of two apostrophes because of an apostrophe in a text.
“Here’s a sentence” -> Correct use of two quotation marks when the text contains an apostrophe.
2. Formatting in SAS
SAS programming language is not strict as compared to other coding languages in terms of using capitalization, indentation, and line breaks.
- Any indentations or spacing before a statement is ignored.
- Any extra lines occurring between statements are ignored.
SAS Variable Names
Now, in SAS Syntax tutorial, we are going to explore variable name in SAS Programming language.
SAS variable names are names given to a column of a SAS data set. The following rules must be followed while naming variables in SAS:
- It can be of a maximum length of 32 characters.
- No blanks should be included.
- It must start with an underscore (_) or any letters from A to Z (it does not matter if letters are in upper or lowercase).
- Numbers can be included but the first character should not be a number.
- Variable names are not case sensitive.
The last one is an important point in SAS Syntax for a variable name. For example- The data set name length-breadth is the same as LENGTHBREADTH or LengthBreadth.
# SOME Valid Variable Names
# Invalid variable Names
DATE OF JOINING #contains Space.
PERCENTAGE% # contains a special character other than the underscore.
82_ID # Starts with a number.
Understand the concept of SAS variables in detail
SAS Data Set
In this portion of SAS Syntax, we are going to learn DataSet. The DATA statement indicates that a new data set is created in SAS. The rules for DATA set creation are as below:
- When a DATA statement follows by a single word, it means that a temporary data set is created and will be erased once the session gets over.
- For a data set to be permanent, we must prefix it with a library name so that it becomes permanent. Once permanent, the data set remains even after the session gets over.
- If a SAS data set name gets omitted, then SAS creates a temporary data set with a name that SAS generates like – DATA1, DATA2, etc.
Learn the whole concept behind SAS Data Set
# Temporary data sets.
# Permanent data sets.
Many a time, it happens that our data remains incomplete and can’t understand by SAS. In such cases, if a character data is missing, it represents by a blank, and a single period (.) represents missing numeric data.
SAS File Extensions
Windows saves SAS programs, data files with different extensions, some are:
- *.log – It represents the SAS Log File. It contains all information on errors, warnings, and data set when the program was run and is similar to a browsing history on our computers.
- *.sas – This represents the SAS code file which can edit using the SAS Editor or any text editor.
- *.mht / *.html – It stores all results of a SAS program.
- *.sas7bdat – It represents the entire SAS data set.
Explore the Important Structure of SAS Program
SAS comment is any line or a block of code which the SAS ignores during the execution of a program. Comments improve the readability of a program by denoting what the steps are doing, what to perform or how something is working. A program that well comments will help the user understand the thought process while making the program.
There are two ways in which we can comment in a SAS program:
- Add an asterisk at the beginning of the line, and add a semicolon at the end of the text that we want to appear in the form of a comment. All text between the asterisk and the semicolon will be ignored by SAS during execution.
- Add a forward slash and an asterisk at the beginning of the comment, and place an asterisk and a forward slash at the end of the line.
Do you know? – Applications of SAS Programming
An example SAS program containing comments might look like this:
* Check the variables in the most recently used dataset using the CONTENTS procedure;
/* Print the contents of the most recently used dataset using the PRINT procedure.*/
* This is comment;
Following is a multiline comment example:
* This is first line of the comment
* This is second line of the comment;
/* This is comment */
Following is a multiline comment example:
/* This is first line of the comment
* This is second line of the comment */
So, this was all about SAS Syntax. Hope you like our explanation.
We studied all the fundamental SAS Syntax Rules, as well as the SAS syntax for different kinds of statements. You must follow all the conditions while writing a program in SAS with SAS Syntax. The most important part is to remember that all statements in SAS end with a semicolon and that SAS is not case sensitive language.
If you have any kind of query, feel free to ask in the comment section.