Previous Page | Next Page

Starting with SAS Data Sets

Reading Selected Observations

If you are interested in only part of a large data set, you can use data set options to create a subset of your data. Data set options specify which observations you want the new data set to include. In Creating Subsets of Observations you learn how to use the subsetting IF statement to create a subset of a large SAS data set. In this section, you learn how to use the FIRSTOBS= and OBS= data set options to create subsets of a larger data set.

For example, you might not want to read the observations at the beginning of the data set. You can use the FIRSTOBS= data set option to define which observation should be the first one that is processed. For the data set CITY, this example creates a data set that excludes observations that contain data prior to 1991 by specifying FIRSTOBS=12. As a result, SAS does not read the first 11 observations, which contain data prior to 1991. (To see the program that creates the CITY data set, see DATA Step to Create the Data Set CITY.)

The following program creates the data set CITY2, which contains the same number of variables but fewer observations than CITY.

data city2;
   set city(firstobs=12);
run;

proc print;
   title 'City Expenditures';
   title2 '1991 - 2000';
run;

The following output shows the results:

Subsetting a Data Set by Observations

                               City Expenditures                               1
                                  1991 - 2000

                                  S
                                  e
                                  r
                                  v
                                  i
                  S               c                   A
                  e               e             A     d      S
                  r       S       s             d     m      e
                  v       e       W             m     i      r
                  i       r       a      A      i     n      v      A
                  c       v       t      d      n     U      i      d
                  e       i       e      m      S     t      c      m
                  s       c       r      i      u     i      e      i
                  P       e       _      n      p     l      s      n
                  o       s       S      L      p     i      T      T       T
          Y       l       F       e      a      l     t      o      o       o
   O      e       i       i       w      b      i     i      t      t       t
   b      a       c       r       e      o      e     e      a      a       a
   s      r       e       e       r      r      s     s      l      l       l

   1    1991    2195    1002     643    256    24    55    3840    335    4175
   2    1992    2204     964     692    256    28    70    3860    354    4214
   3    1993    2175    1144     735    241    19    83    4054    343    4397
   4    1994    2556    1341     813    238    25    97    4710    360    5070
   5    1995    2026    1380     868    226    24    97    4274    347    4621
   6    1996    2526    1454     946    317    13    89    4926    419    5345
   7    1997    2027    1486    1043    226     .    82    4556      .       .
   8    1998    2037    1667    1152    244    20    88    4856    352    5208
   9    1999    2852    1834    1318    270    23    74    6004    367    6371
  10    2000    2787    1701    1317    307    26    66    5805    399    6204

You can also specify the last observation you want to include in a new data set with the OBS= data set option. For example, the next program creates a SAS data set containing only the observations for 1989 (the 10th observation) through 1994 (the 15th observation).

data city3;
   set city (firstobs=10 obs=15);
run;

Previous Page | Next Page | Top of Page