SAS Enterprise Guide

Desktop productivity for business analysts and programmers
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
sun538
Obsidian | Level 7

Hi, 

 

I have a macro to create several tables. I use a Do loop to create these tables using proc sql. I need to add a proc export to the loop, such that every time it creates a table, it exports it in excel to a path:

 

proc sql ;
select distinct month,count(distinct month) into :names1-,:c
from X;
quit;

%macro t;
proc sql;
%do i =1 %to &c;
 create table &&names&i as
 select *
 from X
 where strip(month)="&&names&i";
quit;

 proc export 
  data=&&names&i
  dbms=xlsx 
  outfile="\\...\&&names&i..xlsx" 
  replace;
run;
%end;

%mend t;
options mprint;
%t

However, proc export does not work after proc sql in macro loop. Could you please help me how to use proc export here to export each table that this loop generates? Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
jarapoch
Obsidian | Level 7
Hi, sun538.Try to put proc sql; sentence inside do loop.

proc sql ;
select distinct month,count(distinct month) into :names1-,:c
from X;
quit;

%macro t;
%do i =1 %to &c;
proc sql;
 create table &&names&i as
 select *
 from X
 where strip(month)="&&names&i";
quit;

 proc export 
  data=&&names&i
  dbms=xlsx 
  outfile="\\...\&&names&i..xlsx" 
  replace;
run;
%end;

%mend t;
options mprint;
%t

View solution in original post

6 REPLIES 6
jarapoch
Obsidian | Level 7
Hi, sun538.Try to put proc sql; sentence inside do loop.

proc sql ;
select distinct month,count(distinct month) into :names1-,:c
from X;
quit;

%macro t;
%do i =1 %to &c;
proc sql;
 create table &&names&i as
 select *
 from X
 where strip(month)="&&names&i";
quit;

 proc export 
  data=&&names&i
  dbms=xlsx 
  outfile="\\...\&&names&i..xlsx" 
  replace;
run;
%end;

%mend t;
options mprint;
%t
Reeza
Super User
Good catch!
jarapoch
Obsidian | Level 7

Thank you, Reeza!!

sun538
Obsidian | Level 7
Thanks for your catch. Appreciate it!
PaigeMiller
Diamond | Level 26

In the future, for problems like this, show us the log, along with the original code.

--
Paige Miller
Tom
Super User Tom
Super User

Do you really need the individual datasets? Or just the sheets in the XLSX file?  If so there is no need to for the SQL step.

Plus then you can use the REAL values of MONTH when creating the macro variables so you don't need the STRIP() function in the WHERE clause.

%macro t;
proc sql ;
  select distinct quote(trim(month)) into :names1- from X;
%let c=&sqlobs;
quit;

%do i =1 %to &c;
proc export data=X(where=(month=&&names&i))
  dbms=xlsx replace
  outfile="\\...\&&names&i..xlsx" 
;
run;
%end;
%mend t;

options mprint;
%t

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!

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

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
  • 4426 views
  • 10 likes
  • 5 in conversation