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.

8 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!

witecat 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...

[url=http://www.blingforfun.com]hip hop jewelry[/url],[url=http://www.blingforfun.com/pendants/cat_9.html]hip hop pendants[/url],hip hop watches,[url=http://blingforfun.com/belts/cat_18.html]bling bling[/url] ,hip hop,[url=http://blingforfun.com/chains/cat_7.html]hip hop chains[/url],hip hop bling,[url=http://blingforfun.com/chains/cat_7.html]iced out chains[/url],[url=http://www.blingforfun.com/chains/cat_7.html]wholesale chains[/url]
hip hop jewelry
wholesale hip hop watches
hip hop rings

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

Anonymous said...

Hey! Ɗߋ yoս knoԝ іf tҺey makee ɑny plugins tߋ pprotect against hackers?
I'm kinda paranoid аbout losing еverything I've
workеd hard on. Anyy recommendations?

Loօk into my web-site - TOP slot machines to play

Jian Zhuo 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