BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
AnnMarie
Calcite | Level 5

Please help!

How do I use a macro to read in 15 datasets (with the same variables) - location G:\xyz\output (filenames follow the same format - example ab_city.sas7bdat, xx_city.sas7bdat, yz_city.sas7bdat.

- and export an excel output of a subset of the variables in separate excel files to one location (G:\xyz\output\city).  Each file will be saved as G\xyz\output\city\ab_city.xls.
I appreciate your help!

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Hi,

No probs.  Let me explain the call execute first, this may clarify things.  Call execute takes a string and sends that string directly to the compiler rather than to the macro pre-processor.  The macro pre-processor is a bit like a find replace function where it expands all you code to full code before sending to the compiler, so it resolves macro variables, expands %if or %do loops etc.  What I am doing is creating the code I want to execute - and this can be any code - and utilizing the datastep which operates as a loop over each row to generate the code which gets sent to the compiler.  Here is an update following your request:

data _null_;

  set sashelp.vtable (where=(libname="WORK" and index(upcase(memname),"TEST")>0));

  call execute('data work.exportit (keep=var1 var2 var5 var10); set work.'||strip(memname)||'; run;

                       proc export data=work.exportit outfile="s:\temp\rob\'||strip(memname)||'.xls"; run;');

run;

What this actually does is first creates a temporary dataset with all records from vtable where libname=WORK and TEST is in the dataset name.  Then for each of these rows creates a string - say we have two: x1_test and y1_test then there will be two strings sent to the compiler:

"data work.exportit (keep=var1 var2 var5 var10); set work.x1_test; run;

proc export data=work.exportit outfile="s:\temp\rob\x1_test.xls"; run;"

"data work.exportit (keep=var1 var2 var5 var10); set work.y1_test; run;

proc export data=work.exportit outfile="s:\temp\rob\y1_test.xls"; run;"

If you check the log when you run something like the above, you will see what is generated under the 'NOTE: CALL EXECUTE generate line'

View solution in original post

6 REPLIES 6
Reeza
Super User

Here's a sketch, you'll have to specify your export exactly.

libname out 'G:\xyz\output';

proc sql;

create table want as

select distinct memname into :file1-:file15 from dictionary.table

where libname="OUT" and upcase(memname) like "%_CITY";

quit;

%put &file1;

%put &file10;

%put &file15.;

%macro export_files;

%do i=1 %to 15;

proc export data=&&file&i .....rest of export statements;

%end;

%mend;

%export_files

Reeza
Super User

Adding in comments to help you understand it... and cleaning it up Smiley Happy

SAS has a table called sashelp.vtable or Dictionary.table. Navigate to sashelp.vtable and take a look at it.

This will query that table and pull the table names into 15 macro variables file1 to file15. The where clause is used to filter the table, so lib name is the library as assigned and memname is the dataset name, where it starts anything but ends with _CITY. 

Play around with different options to see what it returns.

proc sql noprint;

select distinct memname into :file1-:file15 from dictionary.table

where libname="OUT" and upcase(memname) like "%_CITY";

quit;

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Hi,

Just to add, in your methodology you could just write:

data _null_;

  set sashelp.vtable (where=(libname="WORK" and index(upcase(memname),"TEST")>0));

  call execute('proc export data=work.'||strip(memname)||' outfile="s:\temp\rob\'||strip(memname)||'.xls"; run;');

run;

Note to replace WORK and TEST with your libname and filenames.  Saves all that macro processing.

AnnMarie
Calcite | Level 5

Thank you!

I modified yours a bit to see if without using the macro, I can get it one step at a time.
can I modify it to this?  (I'm new at this and would like to know the basic and then work thru the complex)


*read a few variables from the original dataset

-  data exportit

      set output.filename;              **(this calls all the variables in the original dataset)

     keep var1, var2, var5, var10  **(this keeps the few variables I need to export to xls)

run;

proc export data = exportit;

outfile = 'G:\xyz\output\file1\var1.xls'

dbms=xls replace;

run;

I appreciate your help!

*(your original code);

data _null_;

  set sashelp.vtable (where=(libname="WORK" and index(upcase(memname),"TEST")>0));

  call execute('proc export data=work.'||strip(memname)||' outfile="s:\temp\rob\'||strip(memname)||'.xls"; run;');

run

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Hi,

No probs.  Let me explain the call execute first, this may clarify things.  Call execute takes a string and sends that string directly to the compiler rather than to the macro pre-processor.  The macro pre-processor is a bit like a find replace function where it expands all you code to full code before sending to the compiler, so it resolves macro variables, expands %if or %do loops etc.  What I am doing is creating the code I want to execute - and this can be any code - and utilizing the datastep which operates as a loop over each row to generate the code which gets sent to the compiler.  Here is an update following your request:

data _null_;

  set sashelp.vtable (where=(libname="WORK" and index(upcase(memname),"TEST")>0));

  call execute('data work.exportit (keep=var1 var2 var5 var10); set work.'||strip(memname)||'; run;

                       proc export data=work.exportit outfile="s:\temp\rob\'||strip(memname)||'.xls"; run;');

run;

What this actually does is first creates a temporary dataset with all records from vtable where libname=WORK and TEST is in the dataset name.  Then for each of these rows creates a string - say we have two: x1_test and y1_test then there will be two strings sent to the compiler:

"data work.exportit (keep=var1 var2 var5 var10); set work.x1_test; run;

proc export data=work.exportit outfile="s:\temp\rob\x1_test.xls"; run;"

"data work.exportit (keep=var1 var2 var5 var10); set work.y1_test; run;

proc export data=work.exportit outfile="s:\temp\rob\y1_test.xls"; run;"

If you check the log when you run something like the above, you will see what is generated under the 'NOTE: CALL EXECUTE generate line'

AnnMarie
Calcite | Level 5

Thank you so much For the details!!

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 1343 views
  • 3 likes
  • 3 in conversation