Hi Everyone,
I thought it should be simple to use proc expand to get exponential moving average (EMA), however, look like I am missing something here. In the example of calculating EMA in the link below, I can't add the value of lookback day, say 50 day EMA.
Can you please help to get the 50 day EMA?
Thank you,
HHC
https://blogs.sas.com/content/iml/2016/01/27/moving-average-in-sas.html
title "Monthly IBM Stock Price";
proc sort data=sashelp.stocks(where=(STOCK='IBM') rename=(Date=t Close=y))
out=Series;
by t;
run;
proc expand data=Series out=out method=none;
id t;
convert y = EWMA / transout=(ewma 0.3);
run;
So, the alpha in EWMA formula = 2/(N observation +1)
if 20 EMA or 20 EWMA, alpha = 2/(20+1)
convert y = EWMA / transout=(ewma ALPHA);
HHC
@hhchenfx wrote:
I thought it should be simple to use proc expand to get exponential moving average (EMA), however, look like I am missing something here. In the example of calculating EMA in the link below, I can't add the value of lookback day, say 50 day EMA.
I don't know what this means: "I can't add the value of lookback day, say 50 day EMA". Please explain further.
50 day? EMA? (By the way, there is EWMA, WMA, MA but no EMA). If you mean EWMA, there is no such thing as a 50 day EWMA, the EWMA uses ALL history, weighted exponentially.
Hi PaigeMiller,
I got the definition of EMA in the link below. Should it be match with the concept of EWMA.
If so, is the parameter 0.3 in the below formula is [2: (N observation+1)] as in the investopedia link?
convert y = EWMA / transout=(ewma 0.3);
Next, you must calculate the multiplier for smoothing (weighting) the EMA, which typically follows the formula: [2 ÷ (number of observations + 1)]. For a 20-day moving average, the multiplier would be [2/(20+1)]= 0.0952.
Finally, the following formula is used to calculate the current EMA:
- EMA = Closing price x multiplier + EMA (previous day) x (1-multiplier)
So, the alpha in EWMA formula = 2/(N observation +1)
if 20 EMA or 20 EWMA, alpha = 2/(20+1)
convert y = EWMA / transout=(ewma ALPHA);
HHC
SAS does not have an EMA (Exponential Moving Average). Whether or not it is the same as the EWMA (Exponentially Weighted Moving Average) in SAS, I leave that to you and others.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.