DATA Step, Macro, Functions and more

Suggestion for new automatic macro variable SYSNVAR

Reply
New Contributor
Posts: 2

Suggestion for new automatic macro variable SYSNVAR

I am aware of the automatic macro variables SYSNOBS and SQLOBS, which tell me how many observations in a new table, but we do not seem to have one for number of variables. DICTIONARY.TABLES gives me NVAR, which tells me what I want, but I am surprised that we do not have a corresponding macro variable such as SYSNVAR.

Super User
Super User
Posts: 7,392

Re: Suggestion for new automatic macro variable SYSNVAR

Well, wasn't aware of these ones, I have always queried the metadata tables.  I suppose its because you should know the structure of your data, as knowing how many variables doesn't help much, I mean you can't really use that info.  However I like the idea, and it is part of the wider discussion on how to access the metadata of the system.  Personally I don't like the macro variable way of doing things.  In my way of thinking metadata should be accessible via a function, which can then be used directly in code explicitly on any dataset not just the last one, maybe something like:

metadata(library=work,member=,property=[,where=]);

 

So you could use it:

number_of_observations=metadata(library=sashelp,member=cars,property=nobs);

number_of_variables=metadata(library=sashelp,member=cars,property=nvar);

Maybe you want to create an array for all col variables:

array list_col{*} metadata(library=sashelp,member=cars,property=vars,where=%str(substr(varname,1,3)="COL"));

 

So a function that can return any of the metadata, not just obs, vars, but any property from table or columns.

 

Super User
Posts: 17,776

Re: Suggestion for new automatic macro variable SYSNVAR

SAS has a semiformal method for making suggestions, you can post a new idea in the SAS ballotware section so others can vote on the idea.

https://communities.sas.com/t5/SASware-Ballot-Ideas/idb-p/sas_ideas
Contributor
Posts: 55

Re: Suggestion for new automatic macro variable SYSNVAR

[ Edited ]

Something you are probably aware of, but maybe others are not.

 

I've always used sysfunc/qsysfunc with attrn or attrc functions to get to these metadata entries if I want to use functions or store things into macro variables. i.e., for the task at hand for number of variables.

 

%let rc = %sysfunc(open(WORK.HAVE));
%let nvars = %sysfunc(attrn(&rc.,nvars));
%let rc = %sysfunc(close(WORK.HAVE));

%put &nvars.;

 

 The nice thing about these is they can be used in open code or in macros (in my experience).

 

 

Other available attributes are viewable here

Super User
Super User
Posts: 7,392

Re: Suggestion for new automatic macro variable SYSNVAR

Yes, I quite agree.  The only thing I would say is that the need to do an close(open(function)) syntax is not ideal.  We should be able to pull metadata items using one simple function, which doesn't require opening/closing files, but just returns a query of the metdata tables.

Contributor
Posts: 55

Re: Suggestion for new automatic macro variable SYSNVAR

Definitely true. Speaking of, I should probably add that to the end in case someone uses it and doesn't know to close.
Ask a Question
Discussion stats
  • 5 replies
  • 257 views
  • 2 likes
  • 4 in conversation