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

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
Barite | Level 11

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
Barite | Level 11

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
Barite | Level 11

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

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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