SAS : Run SAS Procedure on Multiple Datasets

Deepanshu Bhalla 7 Comments

This tutorial explains how to run SAS procedure on multiple datasets.

Dictionary.Columns vs. Dictionary.Tables

See the comparison between Dictionary.Columns and Dictionary.Tables in SAS.

Dictionary.Columns
It returns information about the columns in one or more data sets. It is similar to the results of the CONTENTS procedure.
Dictionary.Tables
It returns information about names of SAS files and type, date created and last modified, number of observations, observation length, number of variables etc.
Count Number of Datasets in a library

The following code counts the number of tables in the specified library (sashelp) using the dictionary.tables view and store the count in the macro variable "n". The value of n is then printed to the SAS log using the %put statement.

%let lib = sashelp;
proc sql noprint;
select count(*) into :n
from dictionary.tables
where libname=%upcase("&lib");
quit;
%put &n;
List name of all datasets in a SAS library

The following code returns a list of table names from a specified library (sashelp) and then store each table name in separate macro variables using a range of macro variable names. The number of macro variables created would be equal to the number of tables in the library.

proc sql noprint;
select memname into :data1 - :data%LEFT(&n)
from dictionary.tables
where libname=%upcase("&lib");
quit;
Export all SAS data sets of a library in CSV format

The following SAS macro named "temp" exports data from multiple tables in a specified library to CSV files.

%macro temp;
%do i=1 %to &n.;
proc export data = &lib..&&data&i
outfile = "C:\Users\Deepanshu\Documents\KeyBank\&&data&i...csv"
DBMS = CSV;
run;
%end;
%mend;
%temp;
Related Posts
Spread the Word!
Share
About Author:
Deepanshu Bhalla

Deepanshu founded ListenData with a simple objective - Make analytics easy to understand and follow. He has over 10 years of experience in data science. During his tenure, he worked with global clients in various domains like Banking, Insurance, Private Equity, Telecom and HR.

Post Comment 7 Responses to "SAS : Run SAS Procedure on Multiple Datasets"
  1. Awesome concepts!!! Thanks

    ReplyDelete
  2. Hi Deepanshu
    i want to print all table names. Written below code
    %let lib = sashelp;
    proc sql noprint;
    select memname into :data1 - :data10
    from dictionary.tables
    where libname=%upcase("&lib");
    quit;
    %macro mymacro;
    %do i=1 %to 10;
    %put &data&i;
    %end;
    %mend;
    %mymacro;
    getting error:
    WARNING: Apparent symbolic reference DATA not resolved.
    &data1
    WARNING: Apparent symbolic reference DATA not resolved.
    &data2
    WARNING: Apparent symbolic reference DATA not resolved.... so on

    Please suggest

    ReplyDelete
    Replies
    1. Please use "&&data&i" inside the %do loop
      Then 10 dataset names will be shown in the log

      Delete
  3. related to previous above post....
    got resolved by &&data&i

    still have query that how n value is identified in below code
    %do i=1 %to &n.;

    ReplyDelete
  4. hello,
    why we have used "..." in "&&data&i...CSV" statement?

    ReplyDelete
  5. shouldn't line 8 be %put &n.;

    ReplyDelete
  6. Hi, how can we loop over all sas datasets in our work library and add the table name as a column in all datasets? All the file names are different.

    ReplyDelete
Next → ← Prev