Wednesday, March 21, 2007

Find the number of observations in the dataset

I work with huge daatsets - more than 100,000 observations followed up for 20 years. In the process of merging datasets read in through macros, I have lost track of the number of observations.

Following code gives the number of observations in the dataset - dataname:
%let dsid=%sysfunc(open(dataname));
%let num=%sysfunc(attrn(&dsid,nobs));
%let rc=%sysfunc(close(&dsid));
%put There are &num observations in dataset dataname.;


This is from the SAS samples.

6 comments:

  1. Good post. I use that macro code quite often and find it very useful. As you probably know, you can use those functions within a data step if you didn't want to ruin your pretty code with %&&%& :)

    data _null_;
    dsid = open('work.myDataSet');
    nobs = attrn(dsid,'nobs');
    rc = close(dsid);
    put nobs=;
    * you can put it into a macro var with;
    call symput( 'myMacVar' , trim( put( nobs,$8. ) );
    run;

    ReplyDelete
  2. Just wanted to say thanks for the code, it was very helpful!

    ReplyDelete
  3. Hello, this was a nice post, but it is not complete without mentioning that "NOBS" doesn't always work...
    For example, a dataset that has been edited thru the "Edit" button in the SAS Base environment may not return the correct number of observations.

    One good article on this and how to avoid being "tricked" can be found at
    http://www2.sas.com/proceedings/sugi26/p095-26.pdf

    There's also even simpler way of using NOBS:


    data _null_;
    put nobs=;
    stop;
    set MYSASLIB.MYSASDATASET nobs=nobs;
    Run;

    Cheers

    ReplyDelete
  4. Hi, Merely returned here to remind you regarding Mobile Monopoly. This is a great technique and WILL make you money, especially seeing as you own a web site. Take a quick look at the video, the strategy is about using Mobile Advertising which is completely new and an unexposed market where you can make thousands by doing hardly any work. I assure you that after you watch their short video it will change you and it will start making you think. http://mobile-mastermind.com

    ReplyDelete
  5. I'vе thoroughly treasured looking throuǥh yolur Ƅlog post annd will
    be suggesting it to relatives and buddiеs.

    Here is my pagе - 100 Vg E Liquid Malaysia

    ReplyDelete