That is one of the few frustrating things about SAS.
They should have provided a "FUNCTION" statement many many years ago to go along with the IF...THEN...ELSE, DO...END, etc. structures. It would have saved a lot of headaches over the decades.
MACRO's are not functions.
But, you can use them for what you want in this case, just not the way that you think.
MACRO's are not functions, they are code generators.
SAS is not system control oriented, but data centric.
SAS is not an interpreted language, but is one of the first, if not the first, Just-In-Time compiled languages.
SAS has probably already/implemented what you need.
Think about these things and what they mean.
SAS reads in a block of text, compiles it to very efficient machine code, executes it, reads in another block of text, compiles, executes, etc. until it reaches the end.
Text block boundaries are marked by DATA, PROC, RUN, and a few other statements. OPTIONS is more of a directive than executable code.
Macro's on the otherhand are somewhat different.
%MACRO name ... %MEND; delineate a block of text that is compiled, but retained in memory and then "executed" by SAS when it runs across the %name macro statement. At that point, the macro is executed, and if it generates code, then that code is injected/inserted in the text block at that point, before the text block is compiled. Then the text block is compiled and executed, but the %name macro has already been executed, and is not again; its work has already been done.
How did I learn all this? By reading the SAS manuals, SAS documentation, which used to be excellent in its completeness. This cannot be said for the MetaData Server and SAS's use of MetaData. But the stuff from before 2000 was all very very good. In fact, I keep a copy of the SAS Documentation up on my workstation all day because I am constantly referring to it, even for this response. I have programmed and do program in so much different stuff on so many different platforms that at my age, I can't keep it all straight all the time, so I constantly refer back to the documentation to make sure I'm not confusing C, ksh, awk, Java, VBScript, Perl, SAS, etc. with each other. I also don't remember as well, partly because I am also continually researching how some system works. Details matter, and my memory is more conceptual than rote. SAS is so big, rich and deep, it would be a very rare individual who could keep the whole of it in their head and remember everything about it.
Anyway, to accomplish what you want.
[pre]
%macro standard_calc(var1,var2,var3);
&var3 = &var1 + &var2 ;
%mend;
DATA dummy;
set indata;
%standard_calc(field1,field2,sum1);
%standard_calc(field3,field4,sum2);
%standard_calc(field5,field6,sum3);
%standard_calc(field7,field8,sum4);
run;
[/pre]
Now, with cut/copy and paste readily available, think about what would be the least amount of work to accomplish what you want. Does the macro save typing and potentially simplify coding and maintenance? Or will it actually add more work and take more time to implement?
Look at my example again. For that specific situation, the right thing to do would have been to go to the SAS documentation for 9.1 (Help in PC SAS), SAS Products, Base SAS, SAS Language Dictionary, Dictionary of Language Elements, Functions and CALL Routines and found that SAS already has a "SUM" function. So, all we would really need to do is type "sum1 = sum(field1,field2);" and then copy and past it few times, and then update the numeric parts of the variable names.
SAS is so unique, that you have to play with it a little to get to know it, but playing with SAS is usually simple, especially within EG and in PC SAS. Running SAS batch processes on Unix or a Mainframe is a little more work, and Batch SAS on Windows can be even more tricky.
I would strongly recommend that you read the real SAS documentation. It is readily available online (off the web) now. Also, search for threads by Joshua. He is new to SAS this year and has posted an excellent list of books that have helped him.
Message was edited by: Chuck