Hi, Tony:
There are some macro statements that must be enclosed entirely in a macro program -- and are not allowed to be used in open code. %DO is one of those statements.
My macro program was composed of 2 main portions or parts:
1) defining the macro program:
[pre]
%macro makecsv;
...ALL the macro statements that will generate my boilerplate code ...
... including creating a CSV file for every unique value of the SEX variable
... in SASHELP.CLASS...
%mend makecsv;
[/pre]
2) using or invoking the macro program -- this statement invokes the macro program:
[pre]
%makecsv;
[/pre]
Think of the SAS Macro facility as being a big typewriter. In the normal way of doing things, if I had wanted to generate 2 CSV files for SASHELP.CLASS AND if I wanted to control the name of the file being created, I would have had to type out 2 ODS CSV "sandwiches" with a different file name for each value of the SEX variable in SASHELP.CLASS. There'd be a lot in the 2 programs that would be the same and the only things that are different are probably the filename of the CSV file and the WHERE statement that does the subset.
So the macro program is like a "boilerplate" SAS code generator or like a form letter generator. I write the program and use macro variable references (&WANT1, &WANT2) to hold the values to come out of SASHELP.CLASS. One of the neat things that I can do in a Macro program is use a %DO Loop to generate the SAME code over and over and over. Very cool.
But the rules of using Macro Language statements like %IF and %DO are that those elements MUST be used inside a Macro Program. The %MACRO signals the beginning of the code that should execute when the macro is invoked and the name of the macro program comes after the %MACRO keyword -- in this case, the name of the MACRO program is MAKECSV.
Once SAS compiles the macro program, the program is stored in a macro catalog and in order to get the program out of the catalog and USE the macro program, I have to call it with a special name: %MAKECSV.
And then, once the macro program is called, the macro processor will be able to go through the macro program line by line to see what code it can send straight to the SAS compiler for execution and which code needs to have some more action from the macro processor (like filling in the values for the macro variables) before it is passed to the SAS compiler.
Taking classes or reading some of the books on the subject are a good idea. You can also Google for papers on SAS Macro Processing. One of the best papers on the subject of Macros, for beginners, is this one:
http://www2.sas.com/proceedings/sugi28/056-28.pdf
Good luck in your adventures with SAS and SAS Macro programs!
cynthia