SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
larkjr18
Fluorite | Level 6

Hey guys,

 

I'm relatively new to SAS and am trying to write a piece of code in SAS enterprise that finds the number of rows and columns for each data set in a library of 20 or so datasets. I know I need some sort of macro and possibly a nobs= statement but overall I'm generally lost. Any tips or suggestions to point me in the right direction would be much appreciated.

1 ACCEPTED SOLUTION

Accepted Solutions
kiranv_
Rhodochrosite | Level 12

All the info you need is sashelp.vtable and you can get by below query. Two things to keep in mind . SASHELP.vtable is dictionary view. Any queries run against dictionary views will be slow or very slow. So always have where clause and also put the libname you want in upper case as shown in the code.

proc sql ;
select libname, memname, nobs as totalrows, nvar as totalcolumns from sashelp.vtable
where libname in ('LIB1','LIB2');
quit;

View solution in original post

6 REPLIES 6
RW9
Diamond | Level 26 RW9
Diamond | Level 26

And where do you get the idea you need macro?

 

data want_obs (keep=memname nobs);
  set sashelp.vtables (where=(libname="<YOURLIB>"));
run;

This will create a dataset with all the obs for each of the datasets in the given library - note replace that text with your libname in upper case.

 

Same for variables:

data want_cols (keep=memname cnt);
  set sashelp.vcolumn (where=(libname="<YOUR_LIB"));
  by memname;
  retain cnt;
  if first.memname then cnt=1;
  else cnt=cnt+1;
  if last.memname then output;
run;

This keeps a running count of columns per dataset and only outputs 1 row with count for each dataset.

PaigeMiller
Diamond | Level 26

@RW9 wrote:

And where do you get the idea you need macro?

 

data want_obs (keep=memname nobs);
  set sashelp.vtables (where=(libname="<YOURLIB>"));
run;

This will create a dataset with all the obs for each of the datasets in the given library - note replace that text with your libname in upper case.

 

Same for variables:

data want_cols (keep=memname cnt);
  set sashelp.vcolumn (where=(libname="<YOUR_LIB"));
  by memname;
  retain cnt;
  if first.memname then cnt=1;
  else cnt=cnt+1;
  if last.memname then output;
run;

This keeps a running count of columns per dataset and only outputs 1 row with count for each dataset.


While this will work (except that it's sashelp.vtable and not vtables), the number of variables is in sashelp.vtable as well, no need to add up things in sashelp.vcolumn.

--
Paige Miller
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Oh yes, NVAR, good spot:

Updated:

data want_obs (keep=memname nobs nvar);
  set sashelp.vtable (where=(libname="<YOURLIB>"));
run;
PaigeMiller
Diamond | Level 26

You don't need a macro.

 

You can simply look in data set (well, actually it's a data view) sashelp.vtable

--
Paige Miller
andreas_lds
Jade | Level 19

Have a look at sashelp.vtable:

 

proc sql;
   select MemName, NVar, nobs
      from sashelp.vtable
         where Libname = 'YOUR_LIB'
   ;
quit;

 

To late 😉

kiranv_
Rhodochrosite | Level 12

All the info you need is sashelp.vtable and you can get by below query. Two things to keep in mind . SASHELP.vtable is dictionary view. Any queries run against dictionary views will be slow or very slow. So always have where clause and also put the libname you want in upper case as shown in the code.

proc sql ;
select libname, memname, nobs as totalrows, nvar as totalcolumns from sashelp.vtable
where libname in ('LIB1','LIB2');
quit;

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 40984 views
  • 4 likes
  • 5 in conversation