BookmarkSubscribeRSS Feed
stataq
Quartz | Level 8

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)

 

2 REPLIES 2
Quentin
Super User

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.

The Boston Area SAS Users Group is hosting free webinars!
Next up: Rick Wicklin presents Ten Tips for Effective Statistical Graphics (with SAS code) on Wednesday March 26.
Register now at https://www.basug.org/events.
yabwon
Onyx | Level 15

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

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



sas-innovate-white.png

Register Today!

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.

Register now!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 769 views
  • 5 likes
  • 3 in conversation