Skip to content
Tutorials Center
My learning

Topics

Lesson 19 of 37 ยท Procedures: PROC

PROC PRINT and PROC CONTENTS

Learn how PROC PRINT lists observations and PROC CONTENTS describes a data set, with VAR, WHERE, OBS, LABEL and NOOBS options, plus a runnable Python stand-in.

A SAS program is built from two kinds of steps. A DATA step creates or changes data. A PROC step (procedure) runs a ready-made tool on a data set: print it, sort it, count it, summarize it, model it. Chapter 5 tours the procedures you will use every day, and the first two are the ones you reach for to answer "what is in this data set?"

SAS itself cannot run on this site. Every sas block below is a read-only example, and I never show output I cannot guarantee. Where a TryIt appears, it is a clearly labelled Python stand-in that does the same job on a small list of records, so you can experiment with the idea. Real SAS output looks different in layout (it goes through ODS, see ODS and Output Destinations).

The shape of a PROC step

Every procedure follows the same pattern:

SAS
proc print data=work.students;
  var name score;
run;
  • proc print names the procedure.
  • data=work.students names the input data set (library work, member students). If you leave data= out, SAS uses the most recently created data set, which is a classic source of surprises. Always write it.
  • Statements such as var refine the behavior.
  • run; ends the step.

PROC PRINT

PROC PRINT shows observations (rows) in the output window or report. Without options it prints every variable and adds an Obs column with the row number.

SAS
proc print data=work.students;
run;

The most useful statements and options:

Piece What it does
var a b c; Print only these variables, in this order
where score > 80; Print only matching observations
id name; Use name as the row label instead of Obs
by class; Print one block per value of class (data must be sorted)
sum score; Add a total row for score
obs=10 (option) Stop after 10 observations
noobs (option) Suppress the Obs column
label (option) Show variable labels instead of names
n (option) Print the number of observations at the end

A typical, tidy call:

SAS
proc print data=work.students (obs=5) noobs label;
  var name class score;
  where score >= 60;
  label score = "Exam score";
run;

Notice (obs=5) is a data set option written in parentheses after the name, while noobs and label are procedure options written before the semicolon. Data set options (obs=, firstobs=, keep=, drop=, rename=) work in almost any step that reads a data set.

WHERE filters before the procedure sees the data, so obs=5 with a where gives the first five matching rows. This is usually what you want. Do not confuse it with a subsetting IF, which only works in a DATA step.

Stand-in: what PROC PRINT does

Here is a small Python function that mimics var, where, obs and noobs. It is a teaching model, not SAS.

Python Stand-in: PROC PRINT in Python

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 second call prints only name and score, only rows with a score of 60 or more, stops after three, and leaves out the row numbers, exactly the same ideas as the SAS options above.

PROC CONTENTS

PROC PRINT shows data; PROC CONTENTS shows the structure: how many observations and variables there are, each variable's name, type (numeric or character), length, format and label, and when the data set was created. It never prints the values themselves.

SAS
proc contents data=work.students;
run;

Useful options:

SAS
proc contents data=work.students varnum;   /* list variables in creation order */
run;

proc contents data=sashelp.class short;    /* just a compact list of names */
run;

proc contents data=sashelp._all_ nods;     /* every data set in a library, names only */
run;

By default PROC CONTENTS lists variables alphabetically; varnum gives the order in which they sit in the table, which is usually what you expect. sashelp is a library that ships with SAS and contains sample tables such as sashelp.class and sashelp.cars, so you can practice without importing anything.

Run PROC CONTENTS first whenever you receive an unfamiliar data set. It tells you whether date is a number (with a date format) or a text string, whether zip lost its leading zero because it is numeric, and how long each character variable is. Those facts decide how you write the rest of the program.

Stand-in: a mini CONTENTS

Python Stand-in: PROC CONTENTS in Python

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.

Real SAS stores every number in 8 bytes by default, which is why the numeric length is 8 here; a character variable's length is the number of bytes reserved for its longest value. The date in this example is Char, a warning sign: a real SAS date would be a number with a date format.

Peeking at big data sets

Printing a million rows is slow and useless. Combine a few options to look safely:

SAS
proc print data=work.big (obs=10);
run;

proc print data=work.big (firstobs=1000 obs=1010);  /* rows 1000 to 1010 */
run;

obs= is the last observation number to read, not a count of rows, so firstobs=1000 obs=1010 prints eleven rows.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which procedure shows the variable names, types and lengths of a data set without printing the values?

    PROC CONTENTS describes the structure (metadata) of a data set; PROC PRINT shows the observations.
  2. 2. What does the data set option (obs=5) do on PROC PRINT?

    obs= is the number of the last observation to read. With firstobs= it can mean fewer rows than the number.
  3. 3. Why should you always write data= in a PROC step?

    The default input is the last data set created in the session, so an omitted data= can silently run on the wrong table.
  4. 4. Which option removes the Obs column from PROC PRINT output?

    noobs is the procedure option that suppresses the observation number column.

Summary

  • A PROC step runs a built-in tool on a data set; always name it with data=.
  • PROC PRINT lists observations; var, where, id, sum, obs=, noobs and label shape the report.
  • PROC CONTENTS reports structure: counts, variable names, types, lengths, formats; use varnum for creation order.
  • Data set options (obs=, firstobs=, keep=) go in parentheses after the data set name.
  • The Python stand-ins here model the ideas only; real SAS output is produced through ODS.

Next: PROC SORT and Removing Duplicates