Skip Navigation

UNC Carolina Population Center

 

Processing a Limited Number of Observations


First and Last Observations

  • Use FIRSTOBS=n to cause processing to begin at the nth observation.
  • Use OBS=n to cause processing to stop at the nth observation.
  • Can assign first and last observations via:
    • Data set options in the SET statement

      set test(firstobs=300);
      set test(obs=500);
      set test(firstobs=25 obs=325);

    • Data set options in Procedures

      proc print data=test(firstobs=5);
      proc print data=test(obs=50);
      proc print data=test(firstobs=5 obs=100);

    • System options

      options firstobs=10;
      options obs=200;
      options firstobs=15 obs=75;


The _N_ Variable

  • Automatic variable that is initially set to 1 at the start of the data step.
  • Increments by 1 after each loop of the data step.
  • Can use _n_ to specify number of observations to process in your data step.

    data new;
    set in.test;

    *** Select the first 500 observations using the _N_ Variable ***;

    if _n_ <= 500;

    run;

Using the WHERE statement with the OBS= option

  • Using the WHERE statement in conjunction with the OBS= option first selects the observations with the WHERE condition and then only prints up to the first 40 observations of when that condition is true.

    proc print data = new(obs=40);
    where gender = 2;
    run;

Random Sample

  • Look at a random sample of the data using the RANUNI function.
  • The following is an example of an approximate-sized random sample without replacement:

    proc print data=ag (obs=30);
    id VILL94 LEKTI94;
    var TRACTOR LANDOWNED PLOTS RICE CASSAVA SUGAR;
    where ranuni(333) <= .10;
    run;

  • The seed or arbitrary starting point (333 above) should be nonnegative.
  • You can reproduce a random sample by using the same seed.
  • If you choose a seed of zero, the computer clock time is used at execution.

Another topic?
Questions or comments?  If you are affiliated with the Carolina Population Center, send them to Phil Bardsley.