3.4 Controlling Data Output & Variable Selection (KEEP, DROP, RENAME, OBS=)

Key Takeaways

  • KEEP and DROP statements affect all output datasets created in the DATA step, whereas KEEP= and DROP= dataset options target specific datasets.
  • Input dataset options (SET ds(KEEP=...)) filter variables before they enter the PDV, reducing memory consumption and processing overhead.
  • When RENAME= is used as an input dataset option, subsequent DATA step statements must reference the NEW variable name.
  • An explicit OUTPUT statement overrides and suppresses automatic implicit output at the end of the DATA step iteration.
  • The FIRSTOBS= and OBS= options specify observation limits, processing (OBS - FIRSTOBS + 1) total records.
Last updated: August 2026

3.4 Controlling Data Output & Variable Selection (KEEP, DROP, RENAME, OBS=)

Quick Answer: Use KEEP/DROP statements or options to select variables, RENAME to change variable names, and OBS=/FIRSTOBS= to control row processing. Statement-level options affect all output datasets, while dataset options target specific input or output streams.

Controlling variable selection, variable naming, and observation counts optimizes processing speed, reduces memory usage, and ensures correct data structure. Understanding the precise timing of when KEEP, DROP, RENAME, and OBS= operate in the Program Data Vector (PDV) is vital for the SAS Base Programming exam.


1. Variable Selection: Statements vs. Dataset Options

You can exclude or include variables using either Statements (KEEP, DROP) or Dataset Options ((KEEP=...), (DROP=...)).

/* Statement Level */
data work.subset1;
    set sashelp.class;
    keep Name Age Height;
run;

/* Dataset Option Level */
data work.subset2;
    set sashelp.class (keep=Name Age Height);
run;

Critical Difference: Input vs. Output Scope

Specification MethodWhen it ExecutesPDV PresencePerformance Impact
Input Dataset Option SET ds(KEEP=vars)Before data enters PDVOnly specified variables enter PDVHighest Efficiency: Reduces I/O and memory
Output Dataset Option DATA ds(KEEP=vars)When writing PDV to diskAll variables enter PDV and can be used in codeModerate: Variables available for calculations
DATA Step Statement KEEP vars;When writing PDV to diskAll variables enter PDV and can be used in codeModerate: Applies globally to ALL output datasets
/* Input Dataset Option Example: Variable NOT in PDV */
data work.calc_fail;
    set sashelp.class (keep=Name Age);
    
    /* ERROR / MISSING: Weight was dropped BEFORE entering PDV */
    BMI = (Weight / (Height**2)) * 703; 
run;

Exam Rule: If a variable is dropped using an input dataset option (SET ds(DROP=Weight)), that variable is never loaded into the PDV. Any subsequent DATA step logic referencing Weight will evaluate to missing or throw a compiler warning!


2. Variable Renaming with RENAME

The RENAME statement or RENAME= option changes variable names.

/* Statement level */
data work.renamed1;
    set sashelp.class;
    rename Name=StudentName Age=YearsOld;
run;

/* Input Dataset Option level */
data work.renamed2;
    set sashelp.class (rename=(Name=StudentName Age=YearsOld));
    
    /* MUST use the NEW name in subsequent programming statements! */
    if YearsOld > 13; 
run;

Exam Trap: When RENAME= is used as an input dataset option, the variable name changes immediately as it enters the PDV. All subsequent programming logic inside the DATA step MUST reference the NEW variable name (YearsOld), not the old name (Age).


3. Controlling Data Output with Explicit OUTPUT Statements

By default, SAS automatically writes the contents of the PDV to the output dataset at the end of each DATA step iteration (implicit output).

/* Implicit Output */
data work.implicit;
    set sashelp.class;
    /* Implicit OUTPUT occurs at run; */
run;

/* Explicit Output */
data work.boys work.girls;
    set sashelp.class;
    if Sex = 'M' then output work.boys;
    else if Sex = 'F' then output work.girls;
run;

Rules of Explicit OUTPUT Statements

  1. Suppression of Default Output: As soon as SAS encounters ANY explicit OUTPUT statement in a DATA step, the default implicit output at the end of the iteration is completely disabled.
  2. Multiple Outputs Per Iteration: A single DATA step iteration can execute multiple OUTPUT statements, producing multiple rows in output datasets from a single input observation (e.g., restructuring wide data to long data).
/* Writing Multiple Rows per Iteration */
data work.quarterly_long;
    set work.yearly_wide;
    
    Quarter = 1; Sales = Q1_Sales; output;
    Quarter = 2; Sales = Q2_Sales; output;
    Quarter = 3; Sales = Q3_Sales; output;
    Quarter = 4; Sales = Q4_Sales; output;
run;

4. Subsetting Observations with FIRSTOBS= and OBS=

The FIRSTOBS= and OBS= dataset options specify observation processing boundaries.

  • FIRSTOBS=n: Specifies the starting observation number (default is 1).
  • OBS=m: Specifies the ending observation number.
data work.sample_range;
    set sashelp.class (firstobs=5 obs=15);
run;
  • Total observations processed = OBS - FIRSTOBS + 1 = 15 - 5 + 1 = 11 observations.

5. Summary Comparison of Output Control Features

FeatureSyntaxScope / EffectKey Pitfall to Avoid
KEEP= (Input)set ds(keep=A B);A, B enter PDV; others excludedCannot use unselected variables in logic
KEEP (Statement)keep A B;All enter PDV; only A, B outputApplies to all datasets in DATA statement
RENAME= (Input)set ds(rename=(A=X));Renames upon entering PDVMust reference X in subsequent logic
Explicit OUTPUToutput dsname;Overrides implicit outputOmitting output in branches loses records
FIRSTOBS= / OBS=set ds(firstobs=5 obs=10);Processes rows 5 through 10OBS= is the row limit, not count of rows
Test Your Knowledge

A program contains: data work.result; set sashelp.class (rename=(Height=HT Weight=WT)); Ratio = HT / WT; run;. What happens during compilation and execution?

A
B
C
D
Test Your Knowledge

How does adding an explicit OUTPUT statement to a DATA step alter the default output behavior?

A
B
C
D
Test Your Knowledge

What is the primary advantage of specifying set work.orders (keep=OrderID CustomerID Amount); compared to using keep OrderID CustomerID Amount; as a standalone statement?

A
B
C
D
Test Your Knowledge

A SAS data set work.orders contains 500 observations. How many observations are processed when executing set work.orders (firstobs=10 obs=25);?

A
B
C
D