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

  1. Comments: Gated by NETNEWS@AUVM.AMERICAN.EDU
  2. Path: sparky!uunet!paladin.american.edu!auvm!NIHCU.BITNET!HIS
  3. Message-ID: <SAS-L%92082010254173@UGA.CC.UGA.EDU>
  4. Newsgroups: bit.listserv.sas-l
  5. Date:         Thu, 20 Aug 1992 10:24:18 EDT
  6. Reply-To:     Howard Schreier <HIS@NIHCU.BITNET>
  7. Sender:       "SAS(r) Discussion" <SAS-L@UGA.BITNET>
  8. From:         Howard Schreier <HIS@NIHCU.BITNET>
  9. Subject: Re: ResetLagQueues function?
  10. Lines: 105
  11.  
  12. CONTENT:  Response/Comment
  13. SUMMARY:  Can code a complex expression to behave as desired
  14.  
  15. > How can I reinitialize the LAG queue? On a BY variable change I want
  16. > the queue to look like the beginning--filled with missing values.
  17. >
  18. > GROUP  X     Y=lag(X)    Desired Y
  19. > 1      3       .            .
  20. > 1      2       3            3
  21. > 1      7       2            2
  22. > 2      8       7            .
  23. > 2      .       8            8
  24. > 2      4       .            .
  25.  
  26. A very good question, I think.  I have  long  believed  that
  27. SAS  should  have  a family of "LAGBY" functions which would
  28. behave this way.
  29.  
  30. > The app this will sit in has a hundred BY variable changes, 100000
  31. > observations, and multiple LAG queues, some of which go up to LAG20.
  32. >
  33. > I would prefer not to program my own LAG function with retains.
  34. >
  35. > I would prefer not to break up the original data set into a hundred
  36. > small ones and use macro looping to process each separately and then
  37. > recombine.
  38. >
  39. > Here is a sort of clumsy approach:
  40. >
  41. > data t;                  *sample data set generation;
  42. >   do g=1 to 3;
  43. >     do y=1 to 8;
  44. >       output;
  45. >     end;
  46. >   end;
  47.  
  48. [code and results deleted]
  49.  
  50. > William Kahn
  51. > W. L. Gore and Associates
  52. > (410) 398-6400
  53.  
  54. The best I can do is another candidate for the kludge of the
  55. year  award :-).  You could add a term to each LAG reference
  56. which would contain parallel LAG references to each  of  the
  57. BY  variables and would evaluate to zero if they all matched
  58. and to missing otherwise.  For  your  test  case,  something
  59. like this:
  60.  
  61.    data t;
  62.      set t;
  63.      by g;
  64.      desiredy=lag6(y) + input(substr('.0',((lag6(g)=g)/*&...*/)+1,1),1.);
  65.      *                       i      s     =                   =    s   i;
  66.  
  67. The comment line below the assignment statement just  labels
  68. some  of  the  parentheses.   The third set from the outside
  69. ("=") surrounds the equality condition.  If there were  more
  70. BY  variables,  each would have to be tested and the results
  71. ANDed together.  Note:  this can give incorrect  results  if
  72. there  are  multiple  BY  groups  with  the same BY variable
  73. values (possible if NOTSORTED is coded).  The  SUBSTR  takes
  74. the  result  and uses it to extract either the period or the
  75. zero from the hard-coded string, and the INPUT  converts  it
  76. to   numeric.    If  the  LAG  reference  crosses  BY  group
  77. boundaries,  missing  value  propagation  causes  the  whole
  78. expression to evaluate to missing, as desired.  The results:
  79.  
  80.    OBS    G    Y    DESIREDY
  81.  
  82.      1    1    1        .
  83.      2    1    2        .
  84.      3    1    3        .
  85.      4    1    4        .
  86.      5    1    5        .
  87.      6    1    6        .
  88.      7    1    7        1
  89.      8    1    8        2
  90.      9    2    1        .
  91.     10    2    2        .
  92.     11    2    3        .
  93.     12    2    4        .
  94.     13    2    5        .
  95.     14    2    6        .
  96.     15    2    7        1
  97.     16    2    8        2
  98.     17    3    1        .
  99.     18    3    2        .
  100.     19    3    3        .
  101.     20    3    4        .
  102.     21    3    5        .
  103.     22    3    6        .
  104.     23    3    7        1
  105.     24    3    8        2
  106.  
  107. I really hope somebody else comes up with something  better.
  108. This  solution  is  clean  in  that  it  doesn't  create any
  109. additional variables, but it's still pretty ugly.
  110.  
  111. /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
  112. \   Howard Schreier, U.S. Dept. of Commerce, Washington    /
  113. /                     MVS 5.18 & 6.07                      \
  114. \   Voice: (202) 377-4180        BITNET: HIS@NIHCU         /
  115. /   Fax:   (202) 377-4614      INTERNET: HIS@CU.NIH.GOV    \
  116. \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
  117.