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;

About Author:

Deepanshu founded ListenData with a simple objective - Make analytics easy to understand and follow. He has close to 7 years of experience in data science and predictive modeling. During his tenure, he has worked with global clients in various domains like retail and commercial banking, Telecom, HR and Automotive.


While I love having friends who agree, I only learn from those who don't.

Let's Get Connected: Email | LinkedIn

Get Free Email Updates :
*Please confirm your email address by clicking on the link sent to your Email*

Related Posts:

6 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
  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

Next → ← Prev