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.
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.
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
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.
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 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: