Skip to content
Tutorials Center
My learning

Topics

Lesson 6 of 37 ยท Environment and Data Steps

DATA Step Basics: SET, Assignments, KEEP and DROP

Learn to write your first real DATA steps: reading a data set with SET, creating variables, setting LENGTH and LABEL, choosing columns with KEEP and DROP.

The DATA step is where most SAS work happens: it reads data, changes it and writes a new data set. You already know it processes one observation at a time. Now you will write one: read an existing table with SET, calculate new variables, control which columns survive and add labels.

The shape of a DATA step

SAS
data output_name;
   set input_name;
   /* statements that run once per observation */
run;

The data statement names the data set to create; the set statement names the data set to read. SAS reads the first observation of the input into memory, runs the statements below, writes the result as a row of the output, then moves on to the second observation, and so on until the input is used up.

The output data set is created from scratch each run: if it already exists it is replaced. Never use the same name for input and output unless you really mean to overwrite your source.

Creating variables with assignment

An assignment statement has the form variable = expression;. If the variable does not exist yet, SAS creates it, and its type comes from the expression.

SAS
data class_bmi;
   set sashelp.class;
   height_m = height * 0.0254;        /* inches to metres */
   weight_kg = weight * 0.45359237;   /* pounds to kg     */
   bmi = weight_kg / height_m**2;
run;

Arithmetic operators are + - * / ** (the last is exponent). Parentheses control order, just as in algebra. Every variable in sashelp.class is carried into the new data set too, so class_bmi has the original five variables plus height_m, weight_kg and bmi.

Character assignments use quoted text. Single and double quotes both work in a plain DATA step:

SAS
data labelled;
   set sashelp.class;
   group = "student";
   greeting = "Hello, " || strip(name);   /* || joins text */
run;

If either value in an arithmetic expression is missing, the result is missing too, and SAS writes a note to the log. age + . is .. Use the SUM function when you want missing values ignored: sum(a, b).

Choosing columns: KEEP and DROP

You rarely need every variable in the output. Two statements decide:

SAS
data small;
   set sashelp.class;
   keep name age;           /* only these two end up in SMALL */
run;

data nosex;
   set sashelp.class;
   drop sex;                /* everything except SEX */
run;

KEEP and DROP as statements apply to the output data set, and the variables are still available inside the step. You can get the same effect with data set options: set sashelp.class (keep=name age); removes variables before the step ever sees them, which is the faster choice for big data. Use one style consistently, and prefer the option on SET when the variables are not needed in calculations.

LENGTH, LABEL, FORMAT

  • LENGTH sets storage length. For character variables it must come before the first use, otherwise SAS takes the length from the first value it meets.
  • LABEL gives a descriptive text that procedures show in output.
  • FORMAT attaches a display format without changing the stored value.
SAS
data tidy;
   length status $ 10;
   set sashelp.class;
   label height = "Height (inches)"
         weight = "Weight (pounds)";
   format bmi 5.1;
   weight_kg = weight * 0.45359237;
   height_m  = height * 0.0254;
   bmi = weight_kg / height_m**2;
   status = "enrolled";
run;

Declaring length status $ 10; first matters: without it, status would get the length of the first value assigned, which is 8 for "enrolled", and later longer values would be cut off. The $ marks a character variable.

Order of variables in the output

By default variables appear in the order SAS first meets them. length statements before set therefore put those variables first. To control the order explicitly, name every variable in a retain statement (or use length) before set. A rarely needed trick, but handy for report layouts.

Reading from several steps

Use several short steps rather than one huge one. For example, step one converts units, step two filters, step three prints. Each step has its own log notes, so a mistake is easier to find. The trade-off is that intermediate data sets take disk space, which is a good reason to write them to WORK.

The row loop in Python

Here is the BMI step as a Python stand-in (not SAS). Notice that the arithmetic happens in the loop body, just like the DATA step body, and that the original fields are copied to the output.

Python Python stand-in: a DATA step with SET, assignments and KEEP

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 three rows are invented for the demo; they are not the real sashelp.class values.

Common mistakes

  • Overwriting your input data set by using the same name in data and set.
  • Forgetting that keep removes a variable you still need later in the same step: use the data set option on the output if you only want to drop at the end.
  • Assigning text longer than the first value and getting truncation; set length first.
  • Using = for comparison in a plain assignment context: x = y; assigns, while comparisons live inside if and where.

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In 'data a; set b; run;', which data set is created?

    The data statement names the data set that is created; set names the one that is read.
  2. 2. What is the result of 5 + . (missing) in a SAS expression?

    Arithmetic with a missing value gives a missing value. The SUM function ignores missing values.
  3. 3. Where must a LENGTH statement for a new character variable appear?

    The length is fixed the first time SAS sees the variable, so declare it first.
  4. 4. Which statement keeps only name and age in the output?

    KEEP lists the variables to write to the output data set.

Summary

  • data names the output, set names the input; the body runs once per observation.
  • Assignment statements create variables; missing values propagate through arithmetic.
  • keep and drop choose columns; as data set options they are faster and happen on read.
  • length, label and format control storage, descriptions and display.

Next: The Program Data Vector and How a DATA Step Executes