home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / bit / listserv / sasl / 3743 < prev    next >
Encoding:
Text File  |  1992-08-15  |  5.4 KB  |  198 lines

  1. Comments: Gated by NETNEWS@AUVM.AMERICAN.EDU
  2. Path: sparky!uunet!paladin.american.edu!auvm!LOBBY.TI.COM!RSNYDER
  3. X-Mailer: ELM [version 2.2 PL16]
  4. Message-ID: <9208151547.AA19788@ti.com>
  5. Newsgroups: bit.listserv.sas-l
  6. Date:         Sat, 15 Aug 1992 10:47:08 CDT
  7. Reply-To:     "R. Snyder" <rsnyder@LOBBY.TI.COM>
  8. Sender:       "SAS(r) Discussion" <SAS-L@UGA.BITNET>
  9. From:         "R. Snyder" <rsnyder@LOBBY.TI.COM>
  10. Subject:      Re: Including macros into SAS program
  11. Comments: To: WHITMIR1%NIEHS.BITNET@uga.cc.uga.edu
  12. Comments: cc: sas-l@uga.cc.uga.edu
  13. In-Reply-To:  <9208141743.AA07547@ti.com>; from
  14.               "WHITMIR1%NIEHS.BITNET@uga.cc.uga.edu" at Aug 14, 92 1:38 pm
  15. Lines: 181
  16.  
  17. WHITMIR1@NIEHS.BITNET@uga.cc.uga.edu writes:
  18. >
  19. > Hello ,
  20. >
  21. > I have large nested IF THEN ELSE in my program.  I wanted to know if there is
  22.  a
  23. > way to write section of the code in macro to make the program more efficient.
  24. > I know a little about macro but not enough to write one on my own!
  25. > I looked at the section "Including macros into sas program" of SAS guide to
  26. > macro processing.  The problem is I don't know how to write a macro.
  27. > Could anybody help me on that. I included part of the sample program below.
  28. > Thanks in advance.
  29. >
  30. > DATA ALL;
  31. >  MERGE ONE(IN=IN1) TWO(IN=IN2);
  32. >   BY ID;
  33. > IF IN1 AND IN2;
  34. >
  35. > TSTREFMO = .;   TSTREFYR = .;
  36. > IF (RC33 ^= 997 AND RC33 ^= 998) AND (RC46 ^= 997 AND RC46 ^= 998) THEN
  37. >  DO;
  38. >   IF RC33 = 1 THEN
  39. >    DO;
  40. >     TSTREFMO = TESTMO;
  41. >     TSTREFYR = TESTYR;
  42. >    END;
  43. >   IF RC33 = 2  THEN
  44. >    DO;
  45. >     IF TESTMO IN (2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12) THEN
  46. >      DO;
  47. >       TSTREFMO = TESTMO - 1;
  48. >       TSTREFYR = TESTYR;
  49. >      END;
  50. >     ELSE IF TESTMO = 1 THEN
  51. >      DO;
  52. >       TSTREFMO = 12;
  53. >       TSTREFYR = TESTYR - 1;
  54. >      END;
  55. >    END;
  56. > END;
  57. > IF (TSTRF_DT NE . AND RC33_DT NE .) THEN
  58. >  DO;
  59. >   IF (RC33_DT NE TSTRF_DT) THEN
  60. >    DO;
  61. >     VAR1 = 'RC33 = ' || PUT(RC33,Z3.);
  62. >     VAR2 = 'RC33_DT = ' || PUT(RC33MO,Z2.) || PUT(RC33YR,Z2.);
  63. >     VAR3 = 'TSTRF_DT = ' || PUT(TSTREFMO,Z2.) || PUT(TSTREFYR,Z2.);
  64. >     VAR4 = 'RC32_DT = ' || PUT(RC32MO,Z2.) || PUT(RC32YR,Z2.);
  65. >     VAR5 = 'RC32A_DT = ' || PUT(RC32AMO,Z2.) || PUT(RC32AYR,Z2.);
  66. >     OUTPUT;
  67. >    END;
  68. >  END;
  69. >
  70.  
  71. I have included two examples of ways to replace in-lime code with a 'called'
  72. routine. The first is the macro method that you requested. I suggest looking
  73. at the %macro statement definition and the preceding "Macro Invocations"
  74. section of the "SAS Guide to Macro Processing" to get started with writing
  75. simple macros such as the one I've included. The second approach I've
  76. illustrated involves using a suroutime and the 'link' statement. If you
  77. do not need macro variables then this is also a practical approach. You may
  78. also want to look at the '%include' statement in the "SAS Language
  79. Reference".
  80.  
  81. This is option 1. Note that the macro definition must be input prior to
  82. attempting to invoke it.
  83.  
  84. ============================================================================
  85.  
  86. %macro complex;  /* Begin macro definition */
  87.  
  88. IF (RC33 ^= 997 AND RC33 ^= 998) AND (RC46 ^= 997 AND RC46 ^= 998) THEN
  89.  DO;
  90.   IF RC33 = 1 THEN
  91.    DO;
  92.     TSTREFMO = TESTMO;
  93.     TSTREFYR = TESTYR;
  94.    END;
  95.   IF RC33 = 2  THEN
  96.    DO;
  97.     IF TESTMO IN (2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12) THEN
  98.      DO;
  99.       TSTREFMO = TESTMO - 1;
  100.       TSTREFYR = TESTYR;
  101.      END;
  102.     ELSE IF TESTMO = 1 THEN
  103.      DO;
  104.       TSTREFMO = 12;
  105.       TSTREFYR = TESTYR - 1;
  106.      END;
  107.    END;
  108. END;
  109.  
  110. %mend complex;   /* End macro definition */
  111.  
  112. /* Begin SAS Program */
  113.  
  114. DATA ALL;
  115.  MERGE ONE(IN=IN1) TWO(IN=IN2);
  116.   BY ID;
  117. IF IN1 AND IN2;
  118.  
  119. TSTREFMO = .;   TSTREFYR = .;
  120.  
  121. %complex;                     /* Invoke macro */
  122.  
  123. IF (TSTRF_DT NE . AND RC33_DT NE .) THEN
  124.  DO;
  125.   IF (RC33_DT NE TSTRF_DT) THEN
  126.    DO;
  127.     VAR1 = 'RC33 = ' || PUT(RC33,Z3.);
  128.     VAR2 = 'RC33_DT = ' || PUT(RC33MO,Z2.) || PUT(RC33YR,Z2.);
  129.     VAR3 = 'TSTRF_DT = ' || PUT(TSTREFMO,Z2.) || PUT(TSTREFYR,Z2.);
  130.     VAR4 = 'RC32_DT = ' || PUT(RC32MO,Z2.) || PUT(RC32YR,Z2.);
  131.     VAR5 = 'RC32A_DT = ' || PUT(RC32AMO,Z2.) || PUT(RC32AYR,Z2.);
  132.     OUTPUT;
  133.    END;
  134.  END;
  135.  
  136. ============================================================================
  137.  
  138. This is option 2. Note the addition of a return command after the main
  139. body of the program.
  140.  
  141. /* Begin SAS Program */
  142.  
  143. DATA ALL;
  144.  MERGE ONE(IN=IN1) TWO(IN=IN2);
  145.   BY ID;
  146. IF IN1 AND IN2;
  147.  
  148. TSTREFMO = .;   TSTREFYR = .;
  149.  
  150. link subr;                    /* Call subroutine */
  151.  
  152. IF (TSTRF_DT NE . AND RC33_DT NE .) THEN
  153.  DO;
  154.   IF (RC33_DT NE TSTRF_DT) THEN
  155.    DO;
  156.     VAR1 = 'RC33 = ' || PUT(RC33,Z3.);
  157.     VAR2 = 'RC33_DT = ' || PUT(RC33MO,Z2.) || PUT(RC33YR,Z2.);
  158.     VAR3 = 'TSTRF_DT = ' || PUT(TSTREFMO,Z2.) || PUT(TSTREFYR,Z2.);
  159.     VAR4 = 'RC32_DT = ' || PUT(RC32MO,Z2.) || PUT(RC32YR,Z2.);
  160.     VAR5 = 'RC32A_DT = ' || PUT(RC32AMO,Z2.) || PUT(RC32AYR,Z2.);
  161.     OUTPUT;
  162.    END;
  163.  END;
  164.  
  165.  return;                                       /* NOTE */
  166.  
  167.  subr:
  168.  
  169.  IF (RC33 ^= 997 AND RC33 ^= 998) AND (RC46 ^= 997 AND RC46 ^= 998) THEN
  170.  DO;
  171.   IF RC33 = 1 THEN
  172.    DO;
  173.     TSTREFMO = TESTMO;
  174.     TSTREFYR = TESTYR;
  175.    END;
  176.   IF RC33 = 2  THEN
  177.    DO;
  178.     IF TESTMO IN (2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12) THEN
  179.      DO;
  180.       TSTREFMO = TESTMO - 1;
  181.       TSTREFYR = TESTYR;
  182.      END;
  183.     ELSE IF TESTMO = 1 THEN
  184.      DO;
  185.       TSTREFMO = 12;
  186.       TSTREFYR = TESTYR - 1;
  187.      END;
  188.    END;
  189. END;
  190.  
  191. return;
  192.  
  193. ============================================================================
  194.  
  195. Regards,
  196.  
  197. Bob Snyder, Texas Instruments, Sherman, TX
  198.