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 (BASUG) is hosting our in person SAS Blowout on Oct 18!
This full-day event in Cambridge, Mass features four presenters from SAS, presenting on a range of SAS 9 programming topics. Pre-registration by Oct 15 is required.
Full details and registration info 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 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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