home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / bit / listserv / sasl / 4917 < prev    next >
Encoding:
Internet Message Format  |  1992-11-04  |  3.2 KB

  1. Path: sparky!uunet!noc.near.net!hri.com!spool.mu.edu!sdd.hp.com!saimiri.primate.wisc.edu!zaphod.mps.ohio-state.edu!darwin.sura.net!europa.asd.contel.com!paladin.american.edu!auvm!NUSVM.BITNET!CCEWWM
  2. From: CCEWWM@NUSVM.BITNET (Wong W M)
  3. Newsgroups: bit.listserv.sas-l
  4. Subject: MOVING AVERAGE
  5. Message-ID: <SAS-L%92110421321554@OHSTVMA.ACS.OHIO-STATE.EDU>
  6. Date: 5 Nov 92 02:52:19 GMT
  7. Sender: "SAS(r) Discussion" <SAS-L@UGA.BITNET>
  8. Reply-To: Wong W M <CCEWWM@NUSVM.BITNET>
  9. Lines: 63
  10. Comments: Gated by NETNEWS@AUVM.AMERICAN.EDU
  11.  
  12.  
  13.  I found the following in SAS Sample Library.  You can modify
  14.  it to suit your requirements.
  15.  
  16.  /****************************************************************/
  17.  /*          S A S   S A M P L E   L I B R A R Y                 */
  18.  /*                                                              */
  19.  /*    NAME: MAVERAGE                                            */
  20.  /*   TITLE: COMPUTATION OF MOVING AVERAGE                       */
  21.  /* PRODUCT: SAS                                                 */
  22.  /*  SYSTEM: PC DOS                                              */
  23.  /*    KEYS: TIME DATMAN                                         */
  24.  /*   PROCS:                                                     */
  25.  /*    DATA:                                                     */
  26.  /*                                                              */
  27.  /*     REF:                                                     */
  28.  /*    MISC:                                                     */
  29.  /*                                                              */
  30.  /****************************************************************/
  31.  /*   This program computes moving average of 11 observations,   */
  32.  /*   but can be changed easily.                                 */
  33.  /****************************************************************/
  34. Data temp;
  35.    Do x=1 to 4 by 0.1;  sumx+x;  output;  end;
  36. Data new;
  37.    If _n_=1 then do;
  38.       Do n=1 to 11;
  39.          set temp;  average=sumx/n;  output;  end;  end;
  40.    Else do;
  41.       Merge temp temp(firstobs=12 rename=(sumx=sumx2));
  42.       If sumx2 ^= . ;
  43.       average=(sumx2-sumx)/11;  output;  end;
  44. PROC PRINT;   Var x sumx sumx2 average;
  45.    Title 'Moving Average over 11 Observations';
  46.  /****************************************************************/
  47.  /*   Computation of moving average separately within By groups. */
  48.  /*   The first data step creates a sample data set, and         */
  49.  /*   computes the cumulative sum.  The variable byn2 is         */
  50.  /*   the observation number within each BY group.               */
  51.  /****************************************************************/
  52. Data temp;
  53.    byvar=1;  byn2=0;
  54.    Do x=1 to 3 by 0.1;
  55.       sumx+x;  byn2+1;  output;  end;
  56.    byvar=2;  byn2=0; sumx=0;
  57.    Do x=20 to 30 by 0.5;
  58.       sumx+x; byn2+1;  output; end;
  59. Data;
  60.    If _n_=1 then do;
  61.       Do n=1 to 11;
  62.          Set temp;  average=sumx/n;
  63.          Byvar2 = 1;
  64.          Output;  End;  End;
  65.    Else do;
  66.       Merge temp temp(firstobs=12 keep=sumx byvar byn2 x
  67.          rename=(sumx=sumx2 byvar=byvar2));
  68.       If sumx2 ^= . ;
  69.       If byvar=byvar2 then average=(sumx2-sumx)/11;
  70.          else average=sumx2/byn2;
  71.       Output;   End;
  72. PROC PRINT;  Var x sumx sumx2 average;  By byvar2;
  73.    Title3 'Computed Separately Within Each BY Group';
  74. RUN;
  75.