I receive Excel files from clients, and these will have varying numbers of variables (columns) each time. After importing to SAS, I wish to automatically determine the number of variables in a dataset so I can set up the index for array processing. I know the SET command has NOBS, but is there nothing similar for number of variables?
Thanks!
In addition to the dictionary tables, you can open the attributes of the data set in a DATA Step.
If you are going to work with arrays, you may want to separate the character from the numeric variables.
Here is sample code:
%let indsn=sashelp.class;
data _NULL_;
length varnme $32 varClst varNlst $32767;
dsid = open("&indsn"); /* open data set attributes */
vars = attrn(dsid,"NVARS"); /* get number of variables */
do i = 1 to vars; /* go through variable names */
varnme = varname (dsid, i); /* name of variable */
vartype = vartype(dsid,i); /* Character or numeric? */
if vartype = "C" then
varClst = catx(' ',varClst,varnme);
else
varNlst = catx(' ',varNlst,varnme);
end;
cntC = count(strip(varClst),' ') + 1;
cntN = count(strip(varNlst),' ') + 1;
call symputx('varClst',varClst); /* put variable list in macro variable */
call symputx('varNlst',varNlst); /* put variable list in macro variable */
call symputx('varCcnt',cntC); /* put variable count in macro variable */
call symputx('varNcnt',cntN); /* put variable count in macro variable */
rc = close(dsid); /* close data set attributes */
run;
%put _user_; /* verify existence of macro variables */
You can request that from the dictionary tables. For example to find out how many variables are in sashelp.class
proc sql;
select nvar
from dictionary.tables
where libname='SASHELP' and memname='CLASS';
QUIT;
UPPER case is important for the library name and the dataset name (memname variable) as these are stored as upper case in the dictionary tables.
In addition to the dictionary tables, you can open the attributes of the data set in a DATA Step.
If you are going to work with arrays, you may want to separate the character from the numeric variables.
Here is sample code:
%let indsn=sashelp.class;
data _NULL_;
length varnme $32 varClst varNlst $32767;
dsid = open("&indsn"); /* open data set attributes */
vars = attrn(dsid,"NVARS"); /* get number of variables */
do i = 1 to vars; /* go through variable names */
varnme = varname (dsid, i); /* name of variable */
vartype = vartype(dsid,i); /* Character or numeric? */
if vartype = "C" then
varClst = catx(' ',varClst,varnme);
else
varNlst = catx(' ',varNlst,varnme);
end;
cntC = count(strip(varClst),' ') + 1;
cntN = count(strip(varNlst),' ') + 1;
call symputx('varClst',varClst); /* put variable list in macro variable */
call symputx('varNlst',varNlst); /* put variable list in macro variable */
call symputx('varCcnt',cntC); /* put variable count in macro variable */
call symputx('varNcnt',cntN); /* put variable count in macro variable */
rc = close(dsid); /* close data set attributes */
run;
%put _user_; /* verify existence of macro variables */
Your data step compiler already has the infirmation for you so you shouldn't need and preparatory steps at all;-)
use _character_ and _numeric_ as variable lists to populate arrays and you'll have all you need.
data action;
Set your.dataset ;
Array nums(*) _numeric_ ;
array chrs(*) _character_ ;
number_of_numerics = dim( nums );
number_of_charvars = dim( chrs );
Hi Dmorrell,
I think use SASHELP to the maximum instead of using these heavy codes.
Once you import Excel file into sas then use SASHELP tables namely Dictionary.tables or sashelp.VTable to obtain every information about every element in the datasets present or user created.
Thanks
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.