DATA Step, Macro, Functions and more

Determining number of variables in a dataset

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 9
Accepted Solution

Determining number of variables in a dataset

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!


Accepted Solutions
Solution
‎05-24-2013 11:59 AM
SAS Employee
Posts: 26

Re: Determining number of variables in a dataset

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 */

View solution in original post


All Replies
Super User
Posts: 11,343

Re: Determining number of variables in a dataset

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.

Solution
‎05-24-2013 11:59 AM
SAS Employee
Posts: 26

Re: Determining number of variables in a dataset

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 */

Valued Guide
Posts: 2,177

Re: Determining number of variables in a dataset

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

Occasional Contributor
Posts: 12

Re: Determining number of variables in a dataset

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

🔒 This topic is solved and locked.

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

Discussion stats
  • 4 replies
  • 11893 views
  • 1 like
  • 5 in conversation