BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
robertrao
Quartz | Level 8

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)

1 ACCEPTED SOLUTION

Accepted Solutions
twocanbazza
Quartz | Level 8


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

4 REPLIES 4
Linlin
Lapis Lazuli | Level 10

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.

twocanbazza
Quartz | Level 8


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

robertrao
Quartz | Level 8

Hi,

Thanks a lot to both of you.

Regards

art297
Opal | Level 21

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

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
  • 4 replies
  • 1974 views
  • 5 likes
  • 4 in conversation