Site icon DataFlair

Best SAS Syntax Tutorial – Grasp the SAS Statements & Comments

SAS Syntax

SAS Basic Syntax

FREE Online Courses: Dive into Knowledge for Free. Learn More!

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:

Fundamental SAS Syntax-Rules:

SAS statements have these characteristics:

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

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

SAS programming language is not strict as compared to other coding languages in terms of using capitalization, indentation, and line breaks.

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:

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.

Example:

# SOME Valid Variable Names
EMPLOYEE_NAME
EMPLOYEE
_ADDRESS
# Invalid variable Names
DATE OF JOINING       #contains Space.
PERCENTAGE%          # contains a special character other than the underscore.
82_ID                         # Starts with a number.

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:

Example:

# Temporary data sets.
DATA Temp_Data;
DATA abc;
# Permanent data sets.
DATA LIBRARY1.
DATA1DATA MYLIB.newdat;

Missing data

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:

SAS Comments

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:

  1. 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.
  2. 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.

An example SAS program containing comments might look like this:

* Check the variables in the most recently used dataset using the CONTENTS procedure;
PROC CONTENTS;
RUN; 
/* Print the contents of the most recently used dataset using the PRINT procedure.*/
PROC PRINT;
RUN;
* 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.

Summary

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.

Exit mobile version