MET MA 603: SAS Programming and Applications
MET MA 603:
SAS Programming and Applications
Statements, Comments, and Programs
1
1
Statements
A statement gives information or instruction to SAS. It’s the basic building block of SAS coding.
Example of a statement:
dm ‘post “This is a message” ’ ;
Statements must end with a semicolon. This is how SAS recognizes the end of the statement. Inadvertently omitting a semicolon can cause unexpected results and is a common error at all levels.
Statements can start anywhere, and can continue to the next line.
Case does not change how code is executed in SAS.
2
2
Code is written and executed in the Editor Window. Multiple Editor Windows can be open within a single SAS session. Click “File >> New Program” to open a new Editor Window.
There are several ways to execute code:
Using the mouse, click “Run >> Submit”
Using the mouse, click the “running person” icon
Using the Command bar, enter “Submit”
Using the keyboard, press “F3”
If any part of the code is highlighted, only that part will be executed. Otherwise, the entire code will be executed.
dm ‘post “This is a message” ’ ;
dm ‘post “This is another message” ’ ;
Executing Statements
3
3
Comments
Comments are sections of code that are not executed when your program is run. By default, they are displayed as green text.
Comments can be used to describe the purpose of a statement or program, prevent certain codes from being executed (debugging), document changes, or identify the author and/or version of a program.
Comments make your code easier to understand, for yourself and for others.
In SAS, there are two ways to make comments: the “statement” method and the “block” method.
4
4
“Statement” Comments
“Statement” comments are a special case of statements, which begin with the star (*) character.
Example of a “statement” comment:
* this is a comment ;
The same properties of statements also apply to “statement” comments.
“Statement” comments cannot contain internal semicolons, since the semicolon signifies the end of the comment.
* the semicolon key (;) indicates the end of a statement comment ;
5
5
“Block” Comments
“Block” comments begin with /* and end with */
SAS ignores everything that is inside a block comment.
Example of a “block” comment:
/*
this code displays a message in a pop-up box
dm ‘post “This is a message” ‘ ; */
“Block” comments can contain almost any character, but they cannot be nested.
“Block” comments can be inserted in the middle of a statement or can be used to “comment out” multiple statements.
Lines can be “block” commented or uncommented using the respective shortcuts Ctrl + ? and Ctrl + Shift + ?
6
6
Programs
A program is a series of statements.
Example of a program:
* this code displays messages in pop-up boxes;
dm ‘post “This is a message” ’ ; /* first message */
dm ‘post “This is another message” ’ ;/* second message */
/* this is the end of the program */
Programs can contain statements as well as comments. Comments can be used to explain aspects of the code.
Statements in a program are executed in sequence from top to bottom.
Two special types of programs in SAS are Data Steps and Procedures.
7
7
“Run-away” Comments
“Run-away” comments are a type of error that happens when a comment inadvertently does not get closed, causing subsequent code to be unintentionally commented.
Both Statement and Block comments can become run-away comments if not properly closed.
When present in a piece of code, run-away comments can be very difficult to find, because they aren’t usually identified as an error by the compiler.
Example of a “run-away” comment:
* Vacation check-list
dm ‘post “Leave food for the cat” ’ ;
dm ‘post “Water Plants” ’ ;
dm ‘post “Have a great vacation!” ’ ;
8
8
Practice
Without copying into SAS, determine which of the statements below would get executed.
/* statement1;
/**/ statement2; * statement3; */ statement4; /* */
statement5; /* statement6; /* */ statement7; /*
statement8
;
*/
9
9
Readings
Textbook sections 1.1, 1.6, 1.7
SAS Programming Documentation on Comments
10
10
/docProps/thumbnail.jpeg