SAS- Problem-5:NUMBER OF OBSERVATIONS IN A DATASET
Posted by Analyst in SAS, SAS-_N_, SAS-Observations, SAS-Opening/Closing Datasets
You are here: Home > SAS-Opening/Closing Datasets > SAS- Problem-5:NUMBER OF OBSERVATIONS IN A DATASET
on Sep 17, 2009Simplest way to find number of observations of a Dataset
Sometimes finding the number of observations becomes very important, like in cases when you need to run a “do loop” for all the observations and obviously you do not want to right click on the dataset and see it in properties. There are many ways of doing this, using “nobs” is one of them but the problem with “nobs” is that it fails in many cases, so to get around this I have listed below two very easy ways of accomplishing the same.
SAMPLE CODE:
DATA YEARS;
INPUT YEAR MONTH_START MONTH_END;
CARDS;
2004 5 8
2005 1 5
2006 20 30
2007 12 15
;
RUN;
/**************** Number of observations ***********************/
*1 ;
%LET DATA_SET = YEARS;
%LET OPEN_DATA=%SYSFUNC(OPEN(&DATA_SET));
%LET OBSERVATIONS=%SYSFUNC(ATTRN(&OPEN_DATA,NOBS));
%LET CLOSE_DATASET=%SYSFUNC(CLOSE(&OPEN_DATA));
%PUT &OBSERVATIONS.;
*2 ;
PROC SQL;
SELECT COUNT(*) INTO:OBSERVATIONS FROM YEARS ; QUIT;
%PUT &OBSERVATIONS.;
The first one is more or less self explanatory. In the first step I am defining the dataset, next we open the dataset, then finding the number of observations and finally closing the dataset. Closing the dataset is very important otherwise it will make the rest of the process that follows in your code slow.
The next way is using “Proc Sql” where we simply count all the rows and put it into the variable “Observations”.
In both the cases I have a “%PUT” statement at the end which prints the value of OBSERVATION variable in the log.(Marked red below)
LOG:
77 /**************** Number of observations ***********************/
78 *1 ;
79 %LET DATA_SET = YEARS;
80 %LET OPEN_DATA=%SYSFUNC(OPEN(&DATA_SET));
81 %LET OBSERVATIONS=%SYSFUNC(ATTRN(&OPEN_DATA,NOBS));
82 %LET CLOSE_DATASET=%SYSFUNC(CLOSE(&OPEN_DATA));
83
84 %PUT &OBSERVATIONS.;
4
85
86 *2 ;
87
88 PROC SQL;
89 SELECT COUNT(*) INTO:OBSERVATIONS FROM YEARS ;
NOTE: Writing HTML Body file: sashtml1.htm
89 ! QUIT;
NOTE: PROCEDURE SQL used (Total process time):
real time 0.04 seconds
cpu time 0.04 seconds
90
91 %PUT &OBSERVATIONS.;
4
PEOPLE WHO READ THIS ALSO READ : SAS,
SAS-_N_,
SAS-Observations,
SAS-Opening/Closing Datasets
This entry was posted on Sep 17, 2009 at 4:13 PM and is filed under SAS, SAS-_N_, SAS-Observations, SAS-Opening/Closing Datasets. You can follow any responses to this entry through the RSS 2.0. You can leave a response.
- No comments yet.
If you liked this post, make sure you subscribe to the RSS feed!



SAS
XCELSIUS





