SAS Base Programming study materials
SAS Base Programming study resources

SAS Base Programming practice questions

One question at a time. Build your SAS Base Programming understanding.

First 50 of 199 questions. Each one lists the choices, the correct answer, and an explanation. The checker at the top of this page saves progress in this browser when it is available.

Continue in the practice tool above to load more questions, 50 at a time.

Question 1Access and create data structures

Which statement associates a libref with a SAS library so that its data sets can be referenced with a two-level name?

  1. LIBNAME Correct answer.
  2. PATH
  3. LIBREF
  4. FILENAME

Explanation

The LIBNAME statement assigns a libref to a SAS library, which is a collection of data sets stored in a directory or other location. After the assignment, the libref can be used as the first level of a two-level data set name such as work.sales. The FILENAME statement works with external files, not with SAS libraries.

Question 2Access and create data structures

Which statement explicitly sets the storage length of a character variable that is created in a DATA step?

  1. FORMAT
  2. RENAME
  3. LENGTH Correct answer.
  4. LABEL

Explanation

The LENGTH statement declares how many bytes a character variable occupies in the program data vector and in the output data set. Without it, a new character variable receives a default length the first time it is assigned. FORMAT and LABEL affect presentation and description, not storage size.

Question 3Access and create data structures

Which DATA step statement writes text to the SAS log by default when no file reference is specified?

  1. WRITE
  2. OUTPUT
  3. PUT Correct answer.
  4. ECHO

Explanation

A PUT statement without a preceding FILE statement writes its values to the SAS log, which makes it useful for debugging. OUTPUT writes the current observation to the output data set rather than to a log or file. WRITE and ECHO are not DATA step statements.

Question 4Access and create data structures

Which data set option changes the names of variables as the data set is read or written?

  1. LABEL=
  2. KEEP=
  3. RENAME= Correct answer.
  4. WHERE=

Explanation

The RENAME= data set option supplies a list of old=new pairs that rename variables for the duration of that use of the data set. It can be applied on either an input data set or an output data set. KEEP= selects variables, and WHERE= selects observations.

Question 5Access and create data structures

A DATA step contains the single statement: set work.q1 work.q2; What is the result in the output data set?

  1. Only the observations of the last data set listed are written.
  2. The observations of q1 are followed by the observations of q2 in one data set. Correct answer.
  3. The variables of q1 are merged side by side with the variables of q2.
  4. Only the observations common to q1 and q2 are kept.

Explanation

Listing multiple data sets on one SET statement concatenates them: all observations of the first data set are processed, then all observations of the second. The variables are the union of the variables in all listed data sets. Matching requires MERGE with a BY statement.

Question 6Access and create data structures

A character variable is created in a DATA step but never assigned a value in a particular observation. What value does it hold in that observation?

  1. The digit 0
  2. A single blank character Correct answer.
  3. The literal text MISSING
  4. A numeric missing value

Explanation

Character variables in the program data vector are initialized to blanks, so an unassigned character variable is a string of blanks for its declared length. Numeric variables, by contrast, are initialized to a missing value represented by a period. The two missing-value conventions are not interchangeable across variable types.

Question 7Access and create data structures

Which of these is a valid SAS comment that can appear inside a DATA step without being executed?

  1. // compute totals
  2. -- compute totals
  3. # compute totals
  4. /* compute totals */ Correct answer.

Explanation

SAS supports two block-style and line-style comment forms, and the slash-asterisk pair is the block comment used in both DATA step and procedure code. An asterisk followed by a semicolon also creates a statement comment but must be a standalone statement. The other markers shown are not SAS comment syntax.

Question 8Access and create data structures

Which procedure displays the descriptor portion of a data set, including variable names, types, and lengths?

  1. PROC CONTENTS Correct answer.
  2. PROC PRINT
  3. PROC MEANS
  4. PROC SQL

Explanation

PROC CONTENTS reports metadata about a data set, such as the number of observations, variable names, types, lengths, formats, and labels. PROC PRINT displays the data values themselves rather than the descriptor information. PROC MEANS summarizes numeric variables and does not list variable attributes.

Question 9Access and create data structures

Before combining two data sets with a MERGE statement that includes a BY statement, what must be true of the input data sets?

  1. The BY variables must exist in both data sets with identical lengths.
  2. Both must be sorted by the BY variables, or have an appropriate index. Correct answer.
  3. Both must contain exactly the same number of observations.
  4. Both must have at least one variable not shared with the other.

Explanation

A BY-grouped merge processes observations group by group, so the input data sets must be in ascending order of the BY variables, which is normally achieved with PROC SORT. If they are not sorted, the merge produces incorrect results and the log may report that the data sets are not properly sorted. Equal observation counts and identical variable lengths are not required.

Question 10Access and create data structures

Which statement causes a variable to keep its value from one iteration of the DATA step to the next instead of being reset to missing?

  1. SUM
  2. KEEP
  3. RETAIN Correct answer.
  4. LAG

Explanation

At the start of each iteration, the program data vector resets most non-automatic variables to missing, but a RETAIN statement prevents that reset for the listed variables. RETAIN is commonly used for accumulating totals and for carrying values forward across observations. The LAG function returns a previous value but is not a retention statement.

Question 11Access and create data structures

Under what condition are the FIRST.variable and LAST.variable temporary variables available in a DATA step?

  1. Only when the SET statement uses the POINT= option.
  2. Only when a RETAIN statement appears in the DATA step.
  3. Only when an index is defined on the BY variables.
  4. When the input data set is sorted by the BY variables and a BY statement is used in the DATA step. Correct answer.

Explanation

FIRST. and LAST. indicators are created automatically for each BY variable when a DATA step contains a BY statement operating on sorted data. They are set to 1 on the first and last observation of each BY group and to 0 otherwise. Sorting, or an index that provides the required order, is a prerequisite.

Question 12Access and create data structures

Which function converts a character value into a numeric value using a specified informat?

  1. CHAR
  2. INPUT Correct answer.
  3. PUT
  4. CONVERT

Explanation

The INPUT function reads a character source value using an informat and returns a numeric result, which is how character strings are converted to numbers. The PUT function does the reverse: it applies a format to a value and returns a character string. CONVERT and CHAR do not perform this informat-driven conversion.

Question 13Access and create data structures

Which statement executes three assignments only when the value of X is greater than 0?

  1. if x>0 do; a=1; b=2; c=3; end;
  2. if x>0 then a=1; b=2; c=3;
  3. if x>0 then do a=1; b=2; c=3; end;
  4. if x>0 then do; a=1; b=2; c=3; end; Correct answer.

Explanation

A DO group starts with DO followed by a semicolon and ends with END followed by a semicolon, and it lets a THEN clause control several statements at once. Without the DO and END, only the single statement immediately after THEN is conditional, and the remaining assignments execute for every observation. Missing the semicolon after DO or after THEN produces a syntax error.

Question 14Access and create data structures

Consider this step: data three; do i=1 to 3; output; end; run; How many observations does the data set three contain?

  1. One
  2. Three Correct answer.
  3. Zero, because no input data set is named
  4. It fails to compile because the variable i is undefined

Explanation

An explicit OUTPUT statement writes the current program data vector as an observation each time it executes, and the DO loop executes it three times. A DATA step does not need an input data set when it generates data with assignment statements and OUTPUT. The automatic variable i is created automatically by the iterative DO statement.

Question 15Access and create data structures

Given the statement array q[3] a b c; what does the expression dim(q) return?

  1. The name of the first array element
  2. The sum of the current values of the array elements
  3. The number of elements in the array, which is 3 Correct answer.
  4. The number of nonmissing elements of the array

Explanation

The DIM function returns the number of elements defined for an array, which in this case is 3. It reports the declared dimension, independent of the data values stored in the elements. To obtain element values you reference the element names or an index such as q[1].

Question 16Access and create data structures

What is the effect of the KEEP= data set option on an input data set named in a SET statement?

  1. It has no effect because KEEP= is valid only on output data sets.
  2. It renames the listed variables in the output data set.
  3. It deletes the listed variables from the input data set permanently.
  4. It reads only the listed variables from the input data set into the program data vector. Correct answer.

Explanation

Using KEEP= on the input side of a SET statement limits which variables are brought into the program data vector, which reduces processing and memory use. The input data set itself is never modified by a data set option. The KEEP statement, by contrast, applies to the variables written to the output data set.

Question 17Access and create data structures

Which function returns the first nonmissing character value among its arguments?

  1. COALESCE
  2. CMISS
  3. COALESCEC Correct answer.
  4. NOTMISS

Explanation

COALESCEC scans its arguments left to right and returns the first one that is not a character missing value, which makes it useful for combining inconsistent text sources. COALESCE performs the same scan but is intended for numeric arguments. CMISS and NOTMISS only test for missingness and return counts or indicators.

Question 18Access and create data structures

In PROC IMPORT, which option names the SAS data set that the procedure creates?

  1. DATA=
  2. INTO=
  3. OUT= Correct answer.
  4. FILE=

Explanation

PROC IMPORT requires OUT= to specify the output SAS data set that receives the imported data. The FILE= option identifies the external file being read, and DBMS= states its type. DATA= and INTO= are not the options used here to name the created data set.

Question 19Access and create data structures

A DATA step contains an explicit OUTPUT statement. What happens at the end of each iteration of that DATA step?

  1. No additional observation is written unless another OUTPUT statement executes. Correct answer.
  2. The DATA step fails to compile.
  3. An additional observation is also written automatically.
  4. The explicit OUTPUT statement is ignored when a SET statement is present.

Explanation

When a DATA step contains an explicit OUTPUT statement, SAS turns off the automatic output that normally occurs at the end of each iteration. Each observation is therefore written only where an OUTPUT statement executes. This makes OUTPUT the tool for creating multiple or selective observations per iteration.

Question 20Access and create data structures

In a DATA step, which statement subsets observations from the input data set before they are loaded into the program data vector?

  1. WHERE Correct answer.
  2. IF
  3. SELECT
  4. SUBSET

Explanation

A WHERE statement in the DATA step applies to the input data set, so observations that fail the condition are never read into the program data vector and variables are not created from them. An IF statement executes after the observation is already in the program data vector. The WHERE approach is generally more efficient for excluding input rows.

Question 21Access and create data structures

Two data sets are concatenated with SET, and the variable Name has length 10 in the first data set and length 25 in the second. What length does Name have in the output data set?

  1. 25, because SAS always uses the largest length encountered.
  2. 17, because SAS uses the average of the two lengths.
  3. 35, because SAS adds the lengths of both versions.
  4. 10, because the length is taken from the first data set in which the variable appears. Correct answer.

Explanation

During compilation the program data vector is built as variables are first encountered, so a variable's length is fixed by the first data set that defines it. Values read later from a longer version are truncated to that length. To avoid truncation, place a LENGTH statement before the SET statement.

Question 22Access and create data structures

Data set A has three observations with ID=1 and data set B has one observation with ID=1. Both are sorted by ID and combined with a MERGE statement using BY ID. What happens to the values contributed by B on the second and third observations of the ID=1 group?

  1. They are retained and repeat on all three observations of the group. Correct answer.
  2. The second observation of B is read again to supply new values.
  3. The DATA step stops processing at the second observation of the group.
  4. They are reset to missing on the second and third observations.

Explanation

In a one-to-many merge, the variables from the data set with a single observation per BY value are not re-read or reset within the group, so their values are carried to the remaining observations. This is often used to attach a single lookup record to many transaction records. Only the data set with multiple observations advances within the group.

Question 23Access and create data structures

Which description correctly characterizes the variable created by the END= option on a SET statement?

  1. It is a character variable that holds the name of the last variable in the input data set.
  2. It is a temporary variable that is set to 1 when the last observation is read and to 0 otherwise, and it is not written to the output data set. Correct answer.
  3. It holds the observation number of the last observation in the input data set.
  4. It is stored in the output data set as a numeric variable.

Explanation

The END= option creates a temporary variable that acts as an end-of-file flag, taking the value 1 on the iteration that reads the final observation and 0 on all others. Because it is temporary, it is not added to the output data set unless you explicitly assign its value to another variable. It is commonly used to trigger end-of-file processing.

Question 24Access and create data structures

In a MERGE statement, what does the data set option IN= create?

  1. A temporary variable that indicates whether the tagged data set contributed to the current observation. Correct answer.
  2. A value indicating whether the tagged data set is sorted by the BY variables.
  3. The value of the BY variable as it appears in the tagged data set.
  4. The number of observations read from the tagged data set.

Explanation

The IN= option names a temporary numeric variable that is 1 when the current observation includes a contribution from that data set and 0 when it does not. Comparing these flags is the standard way to detect matches, non-matches, and observations present in only one input. The variable is not written to the output data set.

Question 25Access and create data structures

To increase the storage length of a character variable that is read from an existing data set, where must the LENGTH statement be placed?

  1. In a separate DATA step that runs after the data is read.
  2. Before the SET statement that reads the data set. Correct answer.
  3. In the PROC step that runs before the DATA step.
  4. Immediately after the SET statement that reads the data set.

Explanation

The program data vector attributes are established during compilation as statements are processed, so a LENGTH statement must be encountered before the statement that defines the variable. Placing LENGTH before SET lets the new length govern the variable rather than the length stored in the input data set. A LENGTH statement placed after SET has no effect on that variable.

Question 26Access and create data structures

Which statement assigns the libref salesdata to the directory /home/u123/sales?

  1. libname '/home/u123/sales' salesdata;
  2. libname salesdata '/home/u123/sales'; Correct answer.
  3. set salesdata '/home/u123/sales';
  4. assign salesdata='/home/u123/sales';

Explanation

The LIBNAME statement takes the form libname libref 'path'; with the libref first and the directory path in quotation marks second. Once assigned, the libref can be used as the first level of a two-level data set name.

Question 27Access and create data structures

What happens when a DATA step begins with the statement data _null_; ?

  1. The step executes but no output data set is created. Correct answer.
  2. A data set named _NULL_ is created in the Work library.
  3. All observations are written to the SAS log as a report.
  4. The step is compiled but never executed.

Explanation

DATA _NULL_ is a special data set name that tells SAS to run the step without creating an output data set. The statements execute, so PUT statements, assignments, and file writes still take effect.

Question 28Access and create data structures

A DATA step begins with a one-level name, such as data final; . Where is the resulting data set stored?

  1. In the Work library. Correct answer.
  2. In the library that was most recently assigned.
  3. In the Sasuser library.
  4. In the directory named by the SASROOT system option.

Explanation

A one-level data set name is automatically prefixed with the libref Work, so the data set is temporary and is deleted when the SAS session ends. To store it permanently you must use a two-level name.

Question 29Access and create data structures

Which of these is a valid SAS variable name?

  1. total score
  2. _score1 Correct answer.
  3. score%
  4. 1st_score

Explanation

A SAS variable name must begin with a letter or an underscore and may contain only letters, digits, and underscores. It can be up to 32 characters long.

Question 30Access and create data structures

Which data set option limits the number of observations that are read from an input data set?

  1. OBS= Correct answer.
  2. NOBS=
  3. FIRSTOBS=
  4. MAXOBS=

Explanation

The OBS= data set option specifies the last observation to be processed, so with the default starting point it caps how many observations are read. FIRSTOBS= sets the starting observation instead.

Question 31Access and create data structures

When PROC SORT is run without an OUT= option, what happens to the input data set?

  1. It is deleted after the sort completes.
  2. It is left unchanged and a new sorted data set is created.
  3. It is replaced by a sorted version of itself. Correct answer.
  4. It is copied to the Work library before being sorted.

Explanation

By default PROC SORT rewrites the input data set in place with the observations in sorted order. Specifying OUT= sends the sorted result to a different data set and leaves the input untouched.

Question 32Access and create data structures

Which automatic variable holds the number of the current iteration of the DATA step?

  1. _I_
  2. _CHARACTER_
  3. _N_ Correct answer.
  4. _ERROR_

Explanation

_N_ is incremented each time the DATA step begins a new iteration, so it counts loop passes rather than observations written. _ERROR_ is a separate automatic variable that flags data errors.

Question 33Access and create data structures

Which statement removes a variable from the output data set while still allowing that variable to be used inside the DATA step?

  1. The DROP statement Correct answer.
  2. The DROP= data set option on the SET statement
  3. The RENAME statement
  4. The KEEP statement

Explanation

The DROP statement is executed at the end of the step, so the variable is available for computations but is not written to the output data set. DROP= as an input data set option instead prevents the variable from being read at all.

Question 34Access and create data structures

Consider the step: data new; set old(rename=(score=grade)); run; What is the result?

  1. The values of Score are stored under the name Grade in New. Correct answer.
  2. The option fails because Score does not exist in New.
  3. Score is renamed inside the data set Old before the values are read.
  4. Both Score and Grade appear as variables in the new data set.

Explanation

RENAME= is a data set option applied to the input data set, so the variable is read into the program data vector under its new name. Only Grade appears in the output data set, and the input data set is not modified.

Question 35Access and create data structures

What is the effect of the WHERE= data set option in the statement set sales(where=(region='East')); ?

  1. Only observations that satisfy the condition are read into the DATA step. Correct answer.
  2. It creates a variable named Region that equals East for every observation.
  3. Only matching observations are written to the output data set, but all are read.
  4. It flags the matching observations and passes all observations to the DATA step.

Explanation

WHERE= is applied when the data set is read, so nonmatching observations never enter the program data vector. This is different from a subsetting IF, which tests observations after they have been loaded.

Question 36Access and create data structures

What does the statement Total + Amount; do inside a DATA step?

  1. It creates Total, retains its value across iterations, and treats a missing Amount as zero. Correct answer.
  2. It adds Total to Amount and stores the result back in Amount.
  3. It creates Total only when Amount is not missing and otherwise assigns a missing value to Total.
  4. It sets Total equal to Amount for each observation and resets it on every iteration.

Explanation

This is the sum statement. The target variable is automatically retained, is initialized to 0 before the first iteration, and ignores missing values of the expression instead of becoming missing.

Question 37Access and create data structures

What is the effect of the statement format salary dollar12.; placed inside a DATA step?

  1. It converts Salary to a character variable in the output data set.
  2. It rounds Salary to whole dollars before storing the value.
  3. It stores the DOLLAR12. format with Salary in the output data set. Correct answer.
  4. It applies the format to Salary only for the duration of the DATA step.

Explanation

A FORMAT statement in a DATA step permanently associates the format with the variable in the output data set, so the values display with that format in later procedures and reports. The underlying stored values are unchanged.

Question 38Access and create data structures

Which informat reads the value 15JAN2023 as a SAS date value?

  1. COMMA10.
  2. $CHAR10.
  3. MMDDYY10. Correct answer.
  4. DATETIME20.

Explanation

MMDDYY10. reads a date written with the day first, followed by a three-letter month abbreviation and a four-digit year. It converts the text into the number of days since January 1, 1960.

Question 39Access and create data structures

Which procedure adds the observations of one data set to the end of another existing data set without creating a new data set?

  1. PROC COPY
  2. PROC TRANSPOSE
  3. PROC DATASETS
  4. PROC APPEND Correct answer.

Explanation

PROC APPEND reads observations from a source data set and writes them directly to the end of an existing base data set. Because no new data set is built, it is more efficient than a DATA step for simple concatenation.

Question 40Access and create data structures

Consider the step: proc sort data=work.visits nodupkey; by patient; run; What is the result?

  1. An error occurs because NODUPKEY requires at least two BY variables.
  2. Duplicate Patient values are replaced by a single missing value.
  3. Duplicate Patient values are removed; the first of each group is kept. Correct answer.
  4. Observations whose values are duplicated across all variables are removed.

Explanation

NODUPKEY compares the values of the BY variables and, when it finds a repeated combination, keeps only the first observation in the sorted order and deletes the rest. NODUP compares entire observations instead.

Question 41Access and create data structures

Which PROC SQL statement creates a SAS data set from a query result?

  1. CREATE TABLE ... AS SELECT ... Correct answer.
  2. UPDATE ... SET ...
  3. INSERT INTO ... SELECT ...
  4. SELECT ... INTO :macrovar ...

Explanation

CREATE TABLE followed by AS SELECT builds a new table, and when a one- or two-level SAS name is used the table is a SAS data set. The selected columns and rows become the structure and content of the new data set.

Question 42Access and create data structures

Which INFILE option treats consecutive delimiters as a single delimiter and removes quotation marks from values?

  1. DLM=
  2. DSD Correct answer.
  3. TRUNCOVER
  4. MISSOVER

Explanation

The DSD option changes list input so that repeated delimiters are read as one, quoted values can contain delimiters, and the quotation marks are stripped from the stored value. It is common with comma-separated files.

Question 43Access and create data structures

Consider the step: data sub; set big(firstobs=11 obs=20); run; Assuming Big contains at least 20 observations, how many observations does Sub contain?

  1. 10 Correct answer.
  2. 9
  3. 11
  4. 20

Explanation

FIRSTOBS= specifies the first observation to read and OBS= specifies the last, and both endpoints are included. Observations 11 through 20 inclusive give ten observations.

Question 44Access and create data structures

Which statement defines an array of four numeric elements that exist only for the duration of the DATA step and are not written to the output data set?

  1. array x[4] _all_;
  2. array x[4] _automatic_;
  3. array x[4] _temporary_; Correct answer.
  4. array x[4] _numeric_;

Explanation

Elements listed with the _TEMPORARY_ keyword are held only in the program data vector and are never written to the output data set. They also are not initialized to missing at the start of each iteration.

Question 45Access and create data structures

Which SET statement option allows a DATA step to read a specific observation directly by its observation number?

  1. END=
  2. POINT= Correct answer.
  3. OBS=
  4. NOBS=

Explanation

POINT= names a variable whose value, set before the SET statement executes, identifies which observation to read. It enables direct access to an observation without processing all preceding ones.

Question 46Access and create data structures

Which option in a PROC SORT BY statement sorts the values of a variable from highest to lowest?

  1. NODUPKEY
  2. ORDER=DESC
  3. REVERSE
  4. DESCENDING Correct answer.

Explanation

The DESCENDING keyword precedes a BY variable to reverse the sort order for that variable. It can be applied to individual variables so that a single BY statement mixes ascending and descending order.

Question 47Access and create data structures

Which procedure copies data sets from one SAS library to another?

  1. PROC LIBRARY
  2. PROC COPY Correct answer.
  3. PROC MOVE
  4. PROC TRANSFER

Explanation

PROC COPY transfers selected members from an input library to an output library, and it can copy an entire library in one step. It works on stored members without reading and rewriting the data observation by observation.

Question 48Manage data

What does PROC SORT do when the step contains no BY statement?

  1. It sorts by the first numeric variable in the data set.
  2. It sorts by all variables in the data set in ascending order. Correct answer.
  3. It stops with an error because a BY statement is required.
  4. It sorts by all variables in the data set in descending order.

Explanation

When no BY statement is supplied, PROC SORT orders the observations using every variable in the data set, ascending, with the first variable as the primary key. Adding a BY statement restricts the sort to the listed variables.

Question 49Manage data

A DATA step contains the statement: set sales.jan sales.feb; What is the result?

  1. The output holds the observations of JAN followed by those of FEB. Correct answer.
  2. JAN and FEB are merged one-to-one by observation number.
  3. Only the variables common to JAN and FEB are kept.
  4. The step fails because two data sets cannot be listed.

Explanation

A SET statement that lists several data sets concatenates them, so all observations of the first data set are read, then all observations of the second. A MERGE statement, not SET, is what combines data sets side by side.

Question 50Manage data

Which statement assigns the libref MYLIB to the directory /home/user/data?

  1. libname /home/user/data mylib;
  2. libname mylib '/home/user/data'; Correct answer.
  3. filename mylib '/home/user/data';
  4. set mylib '/home/user/data';

Explanation

The LIBNAME statement takes the form libname libref 'path'; so the libref comes first and the physical path second, in quotes. The libref can then be used as the first-level name of SAS data sets stored in that directory.