home *** CD-ROM | disk | FTP | other *** search
- Comments: Gated by NETNEWS@AUVM.AMERICAN.EDU
- Path: sparky!uunet!paladin.american.edu!auvm!COMPUSERVE.COM!76350.1604
- Message-ID: <930107201406_76350.1604_EHJ78-1@CompuServe.COM>
- Newsgroups: bit.listserv.sas-l
- Date: Thu, 7 Jan 1993 15:14:06 EST
- Reply-To: Andy Norton <76350.1604@COMPUSERVE.COM>
- Sender: "SAS(r) Discussion" <SAS-L@UGA.BITNET>
- From: Andy Norton <76350.1604@COMPUSERVE.COM>
- Subject: user-defined macro functions?
- Comments: To: SAS-L <SAS-L@AWIIMC12.IMC.UNIVIE.AC.AT>
- Lines: 59
-
- ----------------------------------------------------------------------
- CONTENT: Response
- SUMMARY: user-defined Macro functions
- REL/PLTF: 6.07.0304/CMS, 6.04/PC-DOS, beta 6.08/OS2
- E-ADDR: 76350.1604@compuserve.com
- NAME: Andy Norton
- ADDRESS: Trilogy Consulting, 5148 Lovers Lane, Kalamazoo MI 49002 USA
- PHONE: (616) 344-4100
- ----------------------------------------------------------------------
-
- John Mark williams asks if it is possible to write a user-defined
- function in SAS, such as
-
- > %let x = %amacro;
-
- Others have answered, but let me throw in my two cents worth.
-
- A macro can return a value which can be used in a macro statement.
- %macro INCRMENT (VAR);
- %eval(&VAR + 1)
- %mend INCRMENT;
- %let VARPLUS = %INCRMENT(2);
- VARPLUS will have the value 3.
-
- Macro-generated text need not be open-code (e.g. DATA step) text.
- -----------------------
- JMW also asks
- > Alternatively what about
- > %amacro(x)
- > where the value of x is altered within the macro and is available
- > outside it.
-
- Yes, this would work.
- %macro INCRMENT (VARNAME);
- %let &VARNAME = %eval(&&&VARNAME + 1);
- %mend INCRMENT;
- %INCRMENT (X)
- As long as you don't try
- %INCRMENT (VARNAME)
- because the macro variable VARNAME is local to macro INCRMENT.
-
- The three ampersands tell SAS to resolve &VARNAME, and then to put an
- ampersand in front and resolve it again.
-
- You can access macro variable X from within macro INCRMENT because
- macros can access variables from the calling environment (unless a
- local variable has a conflicting name).
-
- Note that typically when you want to pass in a value rather than a
- macro variable reference, you resolve the value in the calling
- environment:
- %M(&X)
- This guarantees that &X refers to the macro variable X in the calling
- environment rather than any macro variable X that may be locally
- defined within macro M.
-
- It can all get rather complicated. It's not as controlled a situation
- as user-defined functions in other languages. The best place to start
- learning is the book SAS Guide to Macro Processing.
-