Reads a specified observation from a SAS data set into the Data Set Data Vector (DDV).

Category: SAS File I/O


Required Arguments


is a numeric variable that specifies the data set identifier that is returned by the OPEN function.


is a numeric constant, variable, or expression that specifies the number of the observation to read. FETCHOBS treats the observation value as a relative observation number unless you specify the ABS option. The relative observation number might not coincide with the physical observation number on disk, because the function skips observations marked for deletion. When a WHERE clause is active, the function counts only observations that meet the WHERE condition.

Default FETCHOBS skips deleted observations.

Optional Argument


is a character constant, variable, or expression that names one or more options, separated by blanks:

ABS specifies that the value of obs-number is absolute. That is, deleted observations are counted.
NOSET prevents the automatic passing of SAS data set variable values to DATA step or macro variables even if the SET routine has been called.


FETCHOBS returns 0 if the operation was successful, ≠0 if it was not successful, and −1 if the end of the data set is reached. To retrieve the error message that is associated with a nonzero return code, use the SYSMSG function. If the SET routine has been called previously, the values for any data set variables are automatically passed from the DDV to the corresponding DATA step or macro variables. To override this behavior temporarily, use the NOSET option.
If obs-number is less than 1, the function returns an error condition. If obs-number is greater than the number of observations in the SAS data set, the function returns an end-of-file condition.


This example fetches the tenth observation from the SAS data set MYDATA. If an error occurs, the SYSMSG function retrieves the error message and writes it to the SAS log. Note that in a macro statement that you do not enclose character strings in quotation marks.
%let rc = %sysfunc(fetchobs(&mydataid,10));
%if &rc = −1 %then
   %put End of data set has been reached.;
%if &rc > 0 %then %put %sysfunc(sysmsg());