SAS Macro : Run SAS Procedure on Multiple Datasets

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

Dictionary.Columns vs. Dictionary.Tables
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.
Task : Export all SAS data sets of a library in CSV format

*Count Number of Datasets in a library;
%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 library;
proc sql noprint;
select memname into :data1 - :data%LEFT(&n)
from dictionary.tables
where libname=%upcase("&lib");
quit; 

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

ListenData Logo
Spread the Word!
Share
Related Posts
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 has worked with global clients in various domains like Banking, Insurance, Private Equity, Telecom and Human Resource.

7 Responses to "SAS Macro : 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. Use &&data&i in above code then it should work

      Delete
    2. 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

Next → ← Prev
Looks like you are using an ad blocker!

To continue reading you need to turnoff adblocker and refresh the page. We rely on advertising to help fund our site. Please whitelist us if you enjoy our content.