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

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!

1 ACCEPTED SOLUTION

Accepted Solutions
jwsquillace
SAS Employee

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

4 REPLIES 4
ballardw
Super User

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.

jwsquillace
SAS Employee

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

Peter_C
Rhodochrosite | Level 12

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

NishunkSaxena
Calcite | Level 5

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-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
  • 39644 views
  • 3 likes
  • 5 in conversation