home *** CD-ROM | disk | FTP | other *** search
- 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
- From: CCEWWM@NUSVM.BITNET (Wong W M)
- Newsgroups: bit.listserv.sas-l
- Subject: MOVING AVERAGE
- Message-ID: <SAS-L%92110421321554@OHSTVMA.ACS.OHIO-STATE.EDU>
- Date: 5 Nov 92 02:52:19 GMT
- Sender: "SAS(r) Discussion" <SAS-L@UGA.BITNET>
- Reply-To: Wong W M <CCEWWM@NUSVM.BITNET>
- Lines: 63
- Comments: Gated by NETNEWS@AUVM.AMERICAN.EDU
-
-
- I found the following in SAS Sample Library. You can modify
- it to suit your requirements.
-
- /****************************************************************/
- /* S A S S A M P L E L I B R A R Y */
- /* */
- /* NAME: MAVERAGE */
- /* TITLE: COMPUTATION OF MOVING AVERAGE */
- /* PRODUCT: SAS */
- /* SYSTEM: PC DOS */
- /* KEYS: TIME DATMAN */
- /* PROCS: */
- /* DATA: */
- /* */
- /* REF: */
- /* MISC: */
- /* */
- /****************************************************************/
- /* This program computes moving average of 11 observations, */
- /* but can be changed easily. */
- /****************************************************************/
- Data temp;
- Do x=1 to 4 by 0.1; sumx+x; output; end;
- Data new;
- If _n_=1 then do;
- Do n=1 to 11;
- set temp; average=sumx/n; output; end; end;
- Else do;
- Merge temp temp(firstobs=12 rename=(sumx=sumx2));
- If sumx2 ^= . ;
- average=(sumx2-sumx)/11; output; end;
- PROC PRINT; Var x sumx sumx2 average;
- Title 'Moving Average over 11 Observations';
- /****************************************************************/
- /* Computation of moving average separately within By groups. */
- /* The first data step creates a sample data set, and */
- /* computes the cumulative sum. The variable byn2 is */
- /* the observation number within each BY group. */
- /****************************************************************/
- Data temp;
- byvar=1; byn2=0;
- Do x=1 to 3 by 0.1;
- sumx+x; byn2+1; output; end;
- byvar=2; byn2=0; sumx=0;
- Do x=20 to 30 by 0.5;
- sumx+x; byn2+1; output; end;
- Data;
- If _n_=1 then do;
- Do n=1 to 11;
- Set temp; average=sumx/n;
- Byvar2 = 1;
- Output; End; End;
- Else do;
- Merge temp temp(firstobs=12 keep=sumx byvar byn2 x
- rename=(sumx=sumx2 byvar=byvar2));
- If sumx2 ^= . ;
- If byvar=byvar2 then average=(sumx2-sumx)/11;
- else average=sumx2/byn2;
- Output; End;
- PROC PRINT; Var x sumx sumx2 average; By byvar2;
- Title3 'Computed Separately Within Each BY Group';
- RUN;
-