Skip to content
Tutorials Center
My learning

Topics

Lesson 3 of 37 ยท Getting Started

SAS Program Structure: Steps, Statements and Comments

Learn the anatomy of a SAS program: statements and semicolons, DATA and PROC steps, step boundaries, comments, naming rules and good layout habits.

Every SAS program, from three lines to three thousand, follows the same grammar. Once you can see where statements end and steps begin, other people's programs become readable. This lesson covers that grammar: statements, steps, step boundaries, comments and names.

Statements end with a semicolon

A SAS statement is a keyword (or an expression) followed by options and finished with a semicolon:

SAS
title "Class report";
data work.copy;
   set sashelp.class;
run;

SAS is free format: you can put several statements on one line or split one across many lines, and indent however you like. It is also case-insensitive for keywords and variable names (DATA, Data and data are the same), but text inside quotes keeps its case, and character values are compared case-sensitively.

The most common beginner error is a missing semicolon, which makes SAS read two statements as one. You will meet it in The Log and Common Errors.

Two kinds of steps

A SAS program is a sequence of steps:

Step Begins with Purpose
DATA step data name; Creates or modifies a data set
PROC step proc name ...; Runs a procedure on a data set
SAS
data young;                    /* DATA step */
   set sashelp.class;
   where age < 13;
run;

proc print data=young;         /* PROC step */
run;

Besides steps, a program can contain global statements that sit outside any step and take effect immediately, such as libname, options, title, footnote, filename and ods. They are not part of a DATA or PROC step.

Where a step ends

SAS executes a step when it finds a step boundary:

  • run; ends a DATA step or most PROC steps.
  • quit; ends procedures that keep running interactively, such as proc sql and a few others.
  • The next data or proc statement also ends the previous step.
SAS
data a; x = 1; run;
data b; y = 2; run;
proc print data=a; run;

Always write run; or quit; explicitly. Without it the last step in a submitted selection may not execute, because SAS is still waiting to see whether more statements belong to it.

Comments

SAS has two comment styles:

SAS
/* A block comment can
   span several lines. */

* A statement comment ends at the semicolon;

data demo;
   x = 1;   /* a comment at the end of a line */
run;

Use /* ... */ for most comments. The * ... ; style is a statement, so it cannot contain a semicolon inside and, in macro code, it behaves differently. Comments are your documentation: say why a step exists, not what each line does.

An unclosed /* swallows everything after it, up to the next */ or the end of the program. If a program suddenly does nothing, look for a comment that never ends.

Naming rules

Data set names and variable names follow the same rules:

  • 1 to 32 characters long.
  • The first character is a letter or an underscore.
  • The other characters are letters, digits or underscores.
  • No spaces or special characters (unless the special VALIDVARNAME=ANY option and name literals are used, which you should avoid).

Valid: total_sales, _temp, q1_2024. Invalid: 2024_sales, net sales, price$. Names that SAS reserves for itself, such as _N_, _ERROR_ and _NULL_, should not be reused for your own variables.

Reading a program top to bottom

Take this program. See if you can predict what it does before reading on:

SAS
libname demo "C:\training\data";

data demo.adults;
   set sashelp.class;
   if age >= 14;
run;

proc means data=demo.adults;
   var height weight;
run;

The global libname statement connects the nickname demo to a folder. The DATA step reads sashelp.class, keeps only observations where age is 14 or more, and writes them to a permanent data set demo.adults. The PROC step then computes summary statistics for height and weight. The folder path is just an example: use a path that exists on your machine or server.

A tiny step counter

Programmers often check their own code with scripts. This Python stand-in (not SAS) splits a program into steps with the rules above, which is a good way to test that you understand where steps end.

Python Python stand-in: split a SAS program into steps

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The splitter is deliberately simple: it would break on a semicolon inside a quoted string, which real SAS handles correctly.

Layout habits that pay off

  • One statement per line, with the body of a step indented by three spaces.
  • A blank line between steps.
  • Lowercase keywords and descriptive names such as monthly_sales.
  • A header comment with purpose, author, date and inputs.
  • Always run; or quit;.

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What ends most DATA and PROC steps explicitly?

    run; marks the end of most steps; quit; ends interactive procedures such as proc sql.
  2. 2. Which is a valid SAS variable name?

    Names start with a letter or underscore and contain only letters, digits and underscores.
  3. 3. Is title "Report"; part of a DATA or PROC step?

    Global statements such as title, libname and options sit outside steps and apply immediately.
  4. 4. What happens if you leave a /* comment unclosed?

    The comment keeps going until a closing */ is found, which can hide most of your program.

Summary

  • Statements end with a semicolon; SAS is free format and case-insensitive.
  • A program is a series of DATA and PROC steps plus global statements such as libname, options and title.
  • Steps end at run;, quit; or the next data/proc statement; always write them.
  • Comments use /* ... */ (or * ... ;); names are 1 to 32 characters of letters, digits and underscores.

Next: The Log and Common Errors