Sample 24671: Dynamically determine the number of observations and variables in a SAS data set
Using the SAS data set functions OPEN, ATTRN, and CLOSE, retrieve
the number of observations and variables in a data set. Note this
sample is done in macro code, not a DATA step.
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
/* Create a test data set */
data test;
input a b c $ d $;
datalines;
1 2 A B
3 4 C D
;
%macro obsnvars(ds);
%global dset nvars nobs;
%let dset=&ds;
/* Open data set passed as the macro parameter */
%let dsid = %sysfunc(open(&dset));
/* If the data set exists, then grab the number of observations and variables */
/* then close the data set */
%if &dsid %then
%do;
%let nobs =%sysfunc(attrn(&dsid,nobs));
%let nvars=%sysfunc(attrn(&dsid,nvars));
%let rc = %sysfunc(close(&dsid));
%end;
/* Otherwise, write a message that the data set could not be opened */
%else %put open for data set &dset failed - %sysfunc(sysmsg());
%mend obsnvars;
/* Execute the macro -- pass TEST as the data set parameter */
%obsnvars(test)
%put &dset has &nvars variable(s) and &nobs observation(s).;
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
OUTPUT to SAS log
test has 4 variable(s) and 2 observation(s).
Using the SAS data set functions OPEN, ATTRN, and CLOSE, retrieve
the number of observations and variables in a data set. Note that
all of the following is done in macro code, not a DATA Step.
| Type: | Sample |
| Topic: | SAS Reference ==> DATA Step Common Programming Tasks ==> Utilities SAS Reference ==> Macro
|
| Date Modified: | 2007-05-04 03:03:05 |
| Date Created: | 2004-09-30 14:09:02 |
Operating System and Release Information
| SAS System | Base SAS | All | 6.12 | n/a |