OBS= Data Set Option

Specifies the last row in a data source that SAS processes.

Valid in: DATA and PROC steps
Category: Observation Control
Default: MAX
Restriction: Use with input files only.
Supports: All

Syntax

OBS= n | nK | nM | nG | nT | hexX | MIN | MAX

Syntax Description

n | nK | nM | nG | nT

specifies a number to indicate when to stop processing rows. In this case, n must be an integer. Using one of the letter notations results in multiplying the integer by a specific value. That is, specifying K (kilobytes) multiplies the integer by 1,024, M (megabytes) multiplies by 1,048,576, G (gigabytes) multiplies by 1,073,741,824, or T (terabytes) multiplies by 1,099,511,627,776. For example, a value of 20 specifies 20 rows, whereas a value of 3m specifies 3,145,728 rows.

hexX

specifies a number to indicate when to stop processing as a hexadecimal value. You must specify the value beginning with a number (0–9), followed by an X. For example, the hexadecimal value F8 must be specified as 0F8x in order to specify the decimal equivalent of 248. The value 2dx specifies the decimal equivalent of 45.

MIN

sets the number to indicate when to stop processing to 0. Use OBS=0 in order to create an empty data set that has the structure, but not the rows, of another data set.

Interaction If OBS=0 and the NOREPLACE options are in effect, then SAS can still take certain actions because it actually executes each DATA and PROC step in the program, using no rows. For example, SAS executes procedures, such as CONTENTS and DATASETS, that process libraries or SAS data sets.

MAX

sets the number to indicate when to stop processing to the maximum number of rows in the data set, up to the largest 8-byte, signed integer. That value is 263-1, or approximately 9.2 quintillion. This is the default.

Details

OBS= tells SAS when to stop processing rows. To determine when to stop processing, SAS uses the value for OBS= in a formula that includes the value for OBS= and the value for FIRSTOBS=. Here is the formula:
(obs - firstobs) + 1 = results
For example, if OBS=10 and FIRSTOBS=1 (which is the default for FIRSTOBS=), the result is ten rows. That is, (10 - 1) + 1 = 10. If OBS=10 and FIRSTOBS=2, the result is nine rows. That is, (10 - 2) + 1 = 9. OBS= is valid only when an existing data set is read.
In WHERE processing, SAS first subsets the data, and then applies OBS= to the subset. The FEDSVR engine does not have the concept of observation numbering from the original data set. It sends back the number of rows requested, numbered chronologically, regardless of where they occur in the data set.

Comparisons

  • The OBS= data set option overrides the OBS= system option for the individual data set.
  • Although the OBS= data set option specifies an ending point for processing, the FIRSTOBS= data set option specifies a starting point. The two options are often used together to define a range of rows to be processed.
  • The OBS= data set option enables you to select rows from data sets. You can select rows to be read from external data files by using the OBS= option in the INFILE statement.

Examples

Example 1: Using OBS= to Specify When to Stop Processing Rows

This example illustrates the result of using OBS= to tell SAS when to stop processing rows. This example creates a SAS data set that contains 15 rows, and then executes the PRINT procedure with FIRSTOBS=2 and OBS=12. The result is 11 rows. That is, (12 - 2) + 1 = 11. The result of OBS= in this situation appears to be the row number that SAS processes last, because the output starts with row 2, and ends with row 12. However, this is only a coincidence.
libname myfiles fedsvr server="d1234.us.company.com" 
   port=2171 user=user1 pwd=pass1
   dsn=basedsn;

data myfiles.Ages;
   input Name $ Age;
   datalines;
Miguel 53
Brad 27
Willie 69
Marc 50
Sylvia 40
Arun 25
Gary 40
Becky 51
Alma 39
Tom 62
Kris 66
Paul 60
Randy 43
Barbara 52
Virginia 72
;
proc print data=myfiles.Ages (firstobs=2 obs=12);
run;
PROC PRINT Output By Using OBS=
PROC PRINT Output by Using OBS=

Example 2: Using OBS= with WHERE Processing

This example illustrates the result of using OBS= along with WHERE processing. The example uses the data set that was created in Example 1, which contains 15 rows, and assumes that the SAS session has been reset to the defaults FIRSTOBS=1 and OBS=MAX. This example returns the first 10 rows that meet the WHERE criteria.
libname myfiles fedsvr server="d1234.us.company.com" 
   port=2171 user=user1 pwd=pass1
   dsn=basedsn;

proc print data=myfiles.Ages (firstobs=1 obs=max);
  where Age LT 60;
run;
PROC PRINT Output Using a WHERE Statement and Default OBS= and FIRSTOBS=
PROC PRINT Using a WHERE Statement and Default OBS= and FIRSTOBS=
Executing the PRINT procedure with the WHERE statement and OBS=5 results in 10 rows, that is, (5 - 1) + 1 = 4. Note that with WHERE processing, SAS first subsets the data, and then applies OBS= to the subset.
proc print data=myfiles.Ages (obs=5);
  where Age LT 60;
run;
PROC PRINT Output Using a WHERE Statement and OBS=
PROC PRINT Output Using a WHERE Statement and OBS=
The result of OBS= appears to be how many rows to process, because the output consists of 5 rows, ending with the row number 5. However, the result is only a coincidence. If you apply FIRSTOBS=2 and OBS=5 to the subset, then the result is nine rows, that is, (5 - 2) + 1 = 4. OBS= in this situation is neither the row number to end with nor how many rows to process; the value is used in the formula to determine when to stop processing.
proc print data=myfiles.Ages (firstobs=2 obs=5);
  where Age LT 60;
run;
PROC PRINT Output Using WHERE Statement, OBS=, and FIRSTOBS=
PROC PRINT Output Using WHERE Statement, OBS=, and FIRSTOBS=

Example 3: Using OBS= When Rows Are Deleted

This example illustrates the result of using OBS= for a data set that has deleted rows. The example uses the data set that was created in Example 1, and deletes row 4. FIRSTOBS=1 and OBS=MAX are set in the PROC PRINT request to print all rows in the data set.
proc sql noerrorstop; delete from myfiles.Ages
   where Name="Sylvia";
quit;

proc print data=myfiles.Ages (firstobs=1 obs=max);
run;
The name “Sylvia” that was previously in row 4 is removed. The FEDSVR engine does not have the concept of observation numbering from the original data set. It sends back the number of rows requested, numbered chronologically, regardless of where they occur in the data set. There are no gaps in numbering for deleted rows.
PROC PRINT Output Showing Row 4 Deleted
PROC PRINT Output Showing Row 4 Deleted