Help using Base SAS procedures

Use of macros to output multiple files, how?

Posts: 0

Use of macros to output multiple files, how?

I'm new at SAS programming and need help with using macros to run data statements that outputs a file. Because I need to run these statements 12 times (there is a file for each month of the year), I was told to use macros to make things easier but am unsure how this works.
File names are the month and year (e.g., Jan2010, Feb 2010). The code I need to run multiple times is as follows, with macros I was advised to use:

%macro get_time_vars(time_pd, );
data _null_;
set &time_pd._;
file "/export/&time_pd._.tx delimiter='09'x DSD;
format first_name $30.;
format email_address $60.;
put id $ @;
put desc $ @;

How do I get this to run 12 times for each month? What would you recommend? Thank you in advance,
Posts: 8,744

Re: Use of macros to output multiple files, how?

Let's assume that you have the following files:
work.jan creates /export/jan_.tx
work.feb creates /export/feb_.tx
work.mar creates /export/mar_.tx


work.mm01 creates /export/mm01_.tx
work.mm02 creates /export/mm02_.tx
work.mm03 creates /export/mm03_.tx

One simple way to run the macro 12 times, is to simply invoke the macro 12 times with the new value of &TIME_PD specified for each invocation:



There are more advanced ways to repetitively invoke your macro program, however, I would suggest that you make sure that your macro program works for all the datasets you want to read and all the files you want to create. You did not explain what your input file was or whether it was permanent or temporary. Since you read it on a SET statement, you could need a 1 level name (if the SET is in the WORK library) or you might need a 2 level name (if the SET is in a permanent SAS library). Also, you seem to plan to use the value of &TIME_PD -- unchanged -- as part of the outfile name....are you sure that name for the SET will work as the name for the output file?????

Perhaps with a bit more information folks could make more informed suggestions. I am a little uncertain about whether you have shown the ENTIRE program because you have format statements for 2 variables that are not being written with PUT statements and the form of your PUT statements are a bit unusual -- using a single trailing @ for such simple output, when a single PUT statement would work just as well.

Posts: 0

Re: Use of macros to output multiple files, how?

Cynthia -

First of all, thank you for your response. I was able to successfully run the marco using your suggestions. In response to the code I showed, it was a snippet of a much larger one, and I removed variables from the put statements to make it even shorter. Next time, I'll be a bit more cognizant with the code I show, along with my explanation, when asking for assistance. Again, thank you.

Posts: 0

Re: Use of macros to output multiple files, how?


you can try to use a data step and:
- pass all the files you want to be exported as location\input data;
- use macro variables to store the input data;
- build an external, temporary file to store the syntax;
- use %include Statement

/* files build for checking the syntax*/

data x x1;
set sashelp.class;
output x;
set sashelp.class end=done;
output x1;
if done then do;
output x1;

filename test temp;

data _null_;

input filen $35. ;

file test;

call symput (cats('z',_n_),scan(filen,1,'\','b'));

put "data _null_;";
put "set " q ";";
put 'file "' filen '" dsd;';
put "put name age;";
put "run;";

location to store\x
location to store\x1

%include test;

Posts: 0

Re: Use of macros to output multiple files, how?

Marius -

Thanks for your suggestions; between you and Cynthia's posts, I was able to make the macro variables work.

Ask a Question
Discussion stats
  • 4 replies
  • 2 in conversation