BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
hhchenfx
Rhodochrosite | Level 12

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;

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
hhchenfx
Rhodochrosite | Level 12

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

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26

@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.

--
Paige Miller
hhchenfx
Rhodochrosite | Level 12

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);

 

https://www.investopedia.com/terms/e/ema.asp#:~:text=Finally%2C%20the%20following%20formula%20is,)%2... 

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)
hhchenfx
Rhodochrosite | Level 12

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

PaigeMiller
Diamond | Level 26

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.

--
Paige Miller

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1908 views
  • 1 like
  • 2 in conversation