home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / bit / listserv / sasl / 5557 < prev    next >
Encoding:
Text File  |  1993-01-07  |  2.5 KB  |  72 lines

  1. Comments: Gated by NETNEWS@AUVM.AMERICAN.EDU
  2. Path: sparky!uunet!paladin.american.edu!auvm!COMPUSERVE.COM!76350.1604
  3. Message-ID: <930107201406_76350.1604_EHJ78-1@CompuServe.COM>
  4. Newsgroups: bit.listserv.sas-l
  5. Date:         Thu, 7 Jan 1993 15:14:06 EST
  6. Reply-To:     Andy Norton <76350.1604@COMPUSERVE.COM>
  7. Sender:       "SAS(r) Discussion" <SAS-L@UGA.BITNET>
  8. From:         Andy Norton <76350.1604@COMPUSERVE.COM>
  9. Subject:      user-defined macro functions?
  10. Comments: To: SAS-L <SAS-L@AWIIMC12.IMC.UNIVIE.AC.AT>
  11. Lines: 59
  12.  
  13. ----------------------------------------------------------------------
  14. CONTENT:   Response
  15. SUMMARY:   user-defined Macro functions
  16. REL/PLTF:  6.07.0304/CMS, 6.04/PC-DOS, beta 6.08/OS2
  17. E-ADDR:    76350.1604@compuserve.com
  18. NAME:      Andy Norton
  19. ADDRESS:   Trilogy Consulting, 5148 Lovers Lane, Kalamazoo MI 49002 USA
  20. PHONE:     (616) 344-4100
  21. ----------------------------------------------------------------------
  22.  
  23. John Mark williams asks if it is possible to write a user-defined
  24. function in SAS, such as
  25.  
  26. > %let x = %amacro;
  27.  
  28. Others have answered, but let me throw in my two cents worth.
  29.  
  30. A macro can return a value which can be used in a macro statement.
  31.     %macro INCRMENT (VAR);
  32.        %eval(&VAR + 1)
  33.     %mend INCRMENT;
  34.     %let VARPLUS = %INCRMENT(2);
  35. VARPLUS will have the value 3.
  36.  
  37. Macro-generated text need not be open-code (e.g. DATA step) text.
  38. -----------------------
  39. JMW also asks
  40. > Alternatively what about
  41. >    %amacro(x)
  42. > where the value of x is altered within the macro and is available
  43. > outside it.
  44.  
  45. Yes, this would work.
  46.     %macro INCRMENT (VARNAME);
  47.        %let &VARNAME = %eval(&&&VARNAME + 1);
  48.     %mend INCRMENT;
  49.     %INCRMENT (X)
  50. As long as you don't try
  51.     %INCRMENT (VARNAME)
  52. because the macro variable VARNAME is local to macro INCRMENT.
  53.  
  54. The three ampersands tell SAS to resolve &VARNAME, and then to put an
  55. ampersand in front and resolve it again.
  56.  
  57. You can access macro variable X from within macro INCRMENT because
  58. macros can access variables from the calling environment (unless a
  59. local variable has a conflicting name).
  60.  
  61. Note that typically when you want to pass in a value rather than a
  62. macro variable reference, you resolve the value in the calling
  63. environment:
  64.     %M(&X)
  65. This guarantees that &X refers to the macro variable X in the calling
  66. environment rather than any macro variable X that may be locally
  67. defined within macro M.
  68.  
  69. It can all get rather complicated.  It's not as controlled a situation
  70. as user-defined functions in other languages.  The best place to start
  71. learning is the book SAS Guide to Macro Processing.
  72.