DATA Step, Macro, Functions and more

OPEN FUNCTION

Accepted Solution Solved
Reply
Super Contributor
Posts: 1,040
Accepted Solution

OPEN FUNCTION

Hi Team,

Could you please explain me this progeam.

*What is the use of %global for the 3 variables

*what is dsid and rc??Are these like any other variable we create using %Let???or do they have any special meaning????

Also how does this code work for several datasets???

Thanks

%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)


Accepted Solutions
Solution
‎11-01-2012 05:57 PM
Super Contributor
Posts: 356

Re: OPEN FUNCTION


Now I could be completly but seems like a homework question

%global - Refer macro documentation

DSID contains value returned by the open  function (could stand for dataset ID, could have been called anything

RC as above - return code? from the close function

%macro can be called mulitple times by passing different Dataset names in using the DS

Barry

View solution in original post


All Replies
Super Contributor
Posts: 1,636

Re: OPEN FUNCTION

Hi Karun,

%global makes the macro variables you create global. It means that you can use them outside the macro.

no special meanings for disd and rc. You can try to use different names.

Solution
‎11-01-2012 05:57 PM
Super Contributor
Posts: 356

Re: OPEN FUNCTION


Now I could be completly but seems like a homework question

%global - Refer macro documentation

DSID contains value returned by the open  function (could stand for dataset ID, could have been called anything

RC as above - return code? from the close function

%macro can be called mulitple times by passing different Dataset names in using the DS

Barry

Super Contributor
Posts: 1,040

Re: OPEN FUNCTION

Hi,

Thanks a lot to both of you.

Regards

PROC Star
Posts: 7,366

Re: OPEN FUNCTION

Karun,

Linlin and Barry have already answered your questions but, given your question and the others you have asked, I think one of us ought to make an important comment.

The code you asked about was from an example in the documentation.  Glad to see you are reading the documentation.

But don't expect all of the examples in the documentation to be totally thought out, generalizable anywhere solutions.  They often are quite far from that.

As Linlin mentioned, some of the macro variables were made global so that they could be accessed outside of the macro.  However, they weren't all initialized in the macro and the ones with return codes (like dsid and rc) are only local.

Thus, if the macro is called multiple times, then fails, the global variable values will retain the values from a previous call.

In short, question any examples that you don't fully understand and don't blindly take the documentation examples as Gospel.

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 311 views
  • 4 likes
  • 4 in conversation