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, );
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,
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.
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.
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 end=done;
if done then do;