BookmarkSubscribeRSS Feed
BrahmanandaRao
Lapis Lazuli | Level 10
proc sql;
select catx("-",memname,nvar) into: x separated by ","
from dictionary.columns
where libname='FIRST' and memtype="DATA";
quit;

%put &x;

I want all variables for each dataset in FIRST libraray 

11 REPLIES 11
PaigeMiller
Diamond | Level 26

What is wrong with the code you show?

--
Paige Miller
PaigeMiller
Diamond | Level 26

NVAR is not a variable in DICTIONARY.COLUMNS.


What are you trying to do by using this non-existent NVAR variable?

--
Paige Miller
ballardw
Super User

@BrahmanandaRao wrote:

Anandkvn_0-1616075621562.png

 


 

NVAR is in Dictionary.Tables not Dictionary.Columns. Each entry in Dictionary.columns is a single variable, so the "number"

would always be 1.

 

I shudder to think just what you intend to do with such a macro variable.

Tom
Super User Tom
Super User

Since you are retrieving one observation per variable just use GROUP BY and COUNT(*).

proc sql noprint ;
  select catx("-",memname,count(*))
    into :x separated by ","
  from dictionary.columns
  where libname='WORK' and memtype="DATA"
  group by memname
  ;
quit;
%put &=sqlobs &=x ;
Reeza
Super User

If you do a search of this topic on here you'll find another little nifty macro like the one I linked you to yesterday that creates a code book or documents all the metadata for your tables in a nice form, with many options for customization. 

Sajid01
Meteorite | Level 14

Hello @BrahmanandaRao 

Your question is "How to count variables for each dataset in a library".
While there are many approaches to the solution, I would use the following code which I find simple and pretty straight forward. 
I hope you will find it useful.
It creates two column output, the first is name of the dataset and the second the number of columns in the table.
Here is the code.

proc sql;
select memname Label "Dataset" ,count(name) as Num_of_cols
from dictionary.columns
where memtype="DATA"
group by memname ;
quit;

Shmuel
Garnet | Level 18

It seems you wat to select from dictionary.tables and not dictionary.columns.

Run next code for example:

proc sql;
   select memname, nvar
   from dictionary.tables
   where libname = "SASHELP";
quit;

 

 

BrahmanandaRao
Lapis Lazuli | Level 10
Using macros to get required result
Shmuel
Garnet | Level 18

@BrahmanandaRao , your subject is "How to count variables for each dataset in a library".

You can covert the code I sent to a macro in order to get the information for any desired dataset in a given library:

%macro count(lib=,dsname=);
  proc sql;
    select memname, nvar
   from dictionary.tables
   where libname = %upcase("&lib") and
               memname = %upcase("&dsname");
 quit;
%mend count;
%count(lib=sashelp, dsname=class); /* this is an example of using the macro */

 

 

Shmuel
Garnet | Level 18

What kind of result you expect?
If you prefer a macro-variable then:
%macro count(lib=,dsname=);
proc sql noprint;

%global nvar;
select nvar into :nvar
from dictionary.tables
where libname = %upcase("&lib") and
memname = %upcase("&dsname");
quit;
%mend count;
%count(lib=sashelp, dsname=class);
%put NVAR=&nvar;

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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
  • 11 replies
  • 2126 views
  • 2 likes
  • 7 in conversation