Hello,
I am reading a sample code from help file and have a question:
why we need
%let dset=&ds;
%let dsid = %sysfunc(open(&dset));
1. can we do %let dsid = %sysfunc(open(&ds)); instead?
2. why we need open dataset in order to get info?
%macro obsnvars(ds);
%global dset nvars nobs;
%let dset=&ds;
%let dsid = %sysfunc(open(&dset));
%if &dsid %then
%do;
%let nobs =%sysfunc(attrn(&dsid,NOBS));
%let nvars=%sysfunc(attrn(&dsid,NVARS));
%let rc = %sysfunc(close(&dsid));
%put &dset has &nvars variable(s) and &nobs observation(s).;
%end;
%else
%put Open for data set &dset failed - %sysfunc(sysmsg());
%mend obsnvars;
%obsnvars(Sasuser.Houses)
Hi,
Yes, you could do:
%let dsid = %sysfunc(open(&ds)) ;
It looks like the macro was designed to create a global macro variable named DSET which is the the name of the dataset, and global macro variables NVARS and NOBS. The macro variable DS will be a local macro variable since it is a parameter to the macro. So if they wanted to create a global macro variable, this explains they need the %GLOBAL statement and the %LET statement.
As for needing to OPEN a dataset to get this variable information, this is only necessary if you want to use SAS File I/O functions. There are other ways you could get this information, for example with PROC CONTENTS.
1) the first is just to store value of DS in a global macrovariable DSET, because by macro arguments are local in their scope
2) the dataset has to be opened to reserve it for use and to access its metadata (it's like entering a room, first you need to open the door)
Bart
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.
Early bird rate extended! Save $200 when you sign up by March 31.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.