Study guide
3.4 Controlling Data Output & Variable Selection (KEEP, DROP, RENAME, OBS=)
SAS Certified Specialist: Base Programming Using SAS 9.4 A00-231
Track your progress as you read
Finished sections get a check mark.
1 Chapter 1: SAS Base Programming Exam Overview
1.1 A00-231 Exam Format, Scoring & Test-Day Logistics 1.2 Official Content Areas, Weights & Study Plan
2 Chapter 2: Accessing & Creating SAS Data Structures
2.1 SAS Environment, Syntax Rules, & Data Step Basics 2.2 SAS Data Libraries & the LIBNAME Statement 2.3 Investigating Libraries with PROC CONTENTS & PROC DATASETS 2.4 Importing Delimited & Excel Data with PROC IMPORT 2.5 Raw Data Ingestion with INFILE and INPUT Statements
3 Chapter 3: Managing & Combining SAS Data Sets
3.1 Reading SAS Data Sets with the SET Statement 3.2 Combining Data Sets: Concatenating & Interleaving 3.3 Match-Merging with BY Statements & FIRST./LAST. Processing 3.4 Controlling Data Output & Variable Selection (KEEP, DROP, RENAME, OBS=)
4 Chapter 4: Data Transformation & Conditional Logic
4.1 Creating & Modifying Variables with Assignment Statements 4.2 Modifying Variable Attributes with LENGTH, LABEL, FORMAT & RENAME= 4.3 Conditional Processing: IF-THEN/ELSE Statements & WHERE Subsetting 4.4 Accumulating Totals & Subtotals (RETAIN & Sum Statement) 4.5 Processing Data Iteratively with DO Loops
5 Chapter 5: SAS Functions & Data Type Conversions
5.1 Character Functions for String Manipulation 5.2 Numeric & Mathematical Functions 5.3 Working with SAS Dates, Times, & Intervals 5.4 Reading & Displaying Dates with Informats and Formats 5.5 Explicit & Implicit Data Type Conversions (INPUT & PUT)
6 Chapter 6: Restructuring Data & Program Maintenance
6.1 SAS Array Processing for Multi-Variable Transformations 6.2 Sorting Data Sets with PROC SORT 6.3 Restructuring Data Sets with PROC TRANSPOSE 6.4 Creating & Applying Custom Formats with PROC FORMAT 6.5 Simplifying Programs with Macro Variables (%LET)
7 Chapter 7: Error Handling & Debugging SAS Programs
7.1 SAS Log Analysis & Diagnostic Messages 7.2 Syntax vs. Logic Errors & Phasing 7.3 Debugging Techniques with _ERROR_, _N_, & PUT Statements 7.4 Identifying Logic Errors with the PUTLOG Statement 7.5 Handling Missing Values & Preventing Calculation Propagations
8 Chapter 8: Summary & Detail Data Reporting
8.1 Detail Reporting with PROC PRINT 8.2 Descriptive Statistics with PROC MEANS & PROC SUMMARY 8.3 Frequency & Categorical Analysis with PROC FREQ 8.4 Screening Extreme & Missing Values with PROC UNIVARIATE 8.5 Custom Tabular Reports with PROC REPORT & PROC TABULATE
9 Chapter 9: ODS Output Delivery & Data Export
9.1 Output Delivery System (ODS) Architecture Basics 9.2 Generating PDF, HTML, & RTF Output Destinations 9.3 Exporting Reports to Excel with ODS EXCEL & XLSX 9.4 Generating CSV, PowerPoint & Other ODS Destinations 9.5 Customizing ODS Titles, Footnotes, & Global Options 9.6 Exporting SAS Data Sets with PROC EXPORT & the XLSX Engine
3.4 Controlling Data Output & Variable Selection (KEEP, DROP, RENAME, OBS=)
Key Takeaways Explain This Topic AI
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 Method When it Executes PDV Presence Performance Impact
Input Dataset Option SET ds(KEEP=vars) Before data enters PDV Only specified variables enter PDV Highest Efficiency : Reduces I/O and memory
Output Dataset Option DATA ds(KEEP=vars) When writing PDV to disk All variables enter PDV and can be used in code Moderate : Variables available for calculations
DATA Step Statement KEEP vars; When writing PDV to disk All variables enter PDV and can be used in code Moderate : 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
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 .
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
- Feature Syntax Scope / Effect Key Pitfall to Avoid
- KEEP= (Input) set ds(keep=A B); A, B enter PDV; others excluded Cannot use unselected variables in logic
- KEEP (Statement) keep A B; All enter PDV; only A, B output Applies to all datasets in DATA statement
- RENAME= (Input) set ds(rename=(A=X)); Renames upon entering PDV Must reference X in subsequent logic
- Explicit OUTPUT output dsname; Overrides implicit output Omitting output in branches loses records
FIRSTOBS= / OBS= set ds(firstobs=5 obs=10); Processes rows 5 through 10 OBS= is the row limit, not count of rows