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:

Stephen said...

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;

Anonymous said...

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

Anonymous said...

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

Anonymous said...

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

Anonymous said...

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

Unknown said...

fitflops
louis vuitton outlet
ralph lauren outlet
tommy hilfiger
michael kors outlet online
rolex daytona
yeezy boost 350
hermes birkin
ralph lauren outlet
james shoes
20170113