DATA Step, Macro, Functions and more

Getting number of observations in a dataset

Reply
Frequent Contributor
Frequent Contributor
Posts: 81

Getting number of observations in a dataset

Hi Dear,
I am trying to get the number of observation in a dataset.When I wrote the below code in SAS 8.2 I got the followed error message. When wrote the same code in SAS 9.1 , I don't get any error message. Can any one explain why and let me know how to fix it.

Thanks for your help in advance.


Thanks

Inp
DATA USDATA;
INPUT X $1.;
CARDS;
RUN;

DATA _NULL_;
CALL SYMPUT('NOOBS', NOOBS);
SET USDATA NOBS = NOOBS;
STOP;
RUN;



I receive the following message for SAS 8.2
DATA _NULL_;
CALL SYMPUT('NOOBS', NOOBS);
_____
_____
_____
546
546
546
NOTE 546-185: THE VARIABLE NOOBS MAY BE UNINITIALIZED
Respected Advisor
Posts: 3,799

Re: Getting number of observations in a dataset

The NOTE not ERROR is caused by the numeric to character conversion. I would change as follows and move the STOP up. But what you have still works.

[pre]
data _null_;
call symput('noobs',trim(put(noobs,f8.-l)));
stop;
set usdata(drop=_all_) nobs = noobs;
run;
%put NOTE: noobs=&noobs;
[/pre] Added TRIM


Message was edited by: data _null_;
Frequent Contributor
Frequent Contributor
Posts: 81

Re: Getting number of observations in a dataset

Posted in reply to data_null__
Thanks so much. can you please explain what is this format f8.-l
Respected Advisor
Posts: 3,799

Re: Getting number of observations in a dataset

> Thanks so much. can you please explain what is this
> format f8.-l

F is another name for the standard W.D format. -L means left justify. Could have used left(trim(put(nobs,8.)) would be the same result. You want the value of the macro variable to be stripped of leading or trailing blank in most situations.

I often "argue" on SAS-L that putting NOBS into a macro variable is almost always unnecessary, as most examples where NOBS is then used are really just trying to determine if the data is empty.

Simplistic example:
[pre]
data _null_;
if _n_ eq 1 and eof then do;
/* processing for empty data set */
put 'NOTE: Data is empty';
end;
stop;
set usdata end=eof;
run;
[/pre]
Frequent Contributor
Frequent Contributor
Posts: 81

Re: Getting number of observations in a dataset

Posted in reply to data_null__
Hi Thanks so much. I really really appriciated for your help Mr _NULL_.

Thanks again.
N/A
Posts: 0

Re: Getting number of observations in a dataset

Another way to get the number of observations is to use SASHELP as follows:

DATA _NULL_;
SET SASHELP.VTABLE;
WHERE LIBNAME EQ ‘WORK’ AND MEMNAME EQ ‘EXAMPLE’;
CALL SYMPUTX(‘NOBS’,NOBS);
PUT “NOTE: Dataset WORK.EXAMPLE has “ NOBS “ observations.”;
RUN;

The SYMPUTX function is new in V9 and avoids having to use TRIM and LEFT functions.

I usually struggle to find documentation about these SASHELP tables, but a search just now found it under ‘Concepts: SQL Procedure’.
Super User
Posts: 5,441

Re: Getting number of observations in a dataset

Posted in reply to deleted_user
Another, a more direct way is to use the ATTRN function:

%let dsid = %sysfunc(open(mytable));
%let nobs= %sysfunc(attrn(&DSID,nobs));
%put &NOBS;

/Linus
Data never sleeps
Respected Advisor
Posts: 3,799

Re: Getting number of observations in a dataset

Did you forget to close the door?

[pre]
15 %let dsid = %sysfunc(open(mytable));
16 %let nobs= %sysfunc(attrn(&DSID,nobs));
17 %put &NOBS;
1
18 /*%let rc = %sysfunc(close(&dsid));*/
19 %put NOTE: &dsid &rc;
NOTE: 1 0
20 proc sort data=mytable;
21 by x;
22 run;

ERROR: You cannot open WORK.MYTABLE.DATA for output access with member-level control because
WORK.MYTABLE.DATA is in use by you in resource environment SORT.

[/pre]
Ask a Question
Discussion stats
  • 7 replies
  • 262 views
  • 0 likes
  • 4 in conversation