BookmarkSubscribeRSS Feed
lakshmiG
Obsidian | Level 7

Please guide me in calculation of SAS Exponential Moving average for N=22 days without using SAS ETS software (Proc expand Procedure)

Please let me know the formula or logic.

As of I browsed, the formula is, EMA =( (close price)-Previous day EMA)*smoothing constant)+Previous day EMA.

 

Thanks, I got some valuable answer from USers of this group.

please see my below code,

data EMA2c(keep=symbol date1 close a22 ema22 lema22 count ma22 );
set merg;
by symbol date1;
retain EMA22;
if count eq 22 then do;EMA22=ma22;end;
lema22=lag(ema22);
/* EMA Calculation */
if count gt 22 then do;EMA22=((close-lema22)*a22)+lema22;end;
run;

 

If I code as above, for 23rd and 24th observation, the 22nd EMA value is getting populated(that is lag value of 22nd observation is populated twice),  so that other values from 25 are moving down one step though lagging correctly.why this 22nd observation is lagging twice for 23rd and 24th observations? for 23rd observation it can come, for 24th observation,23rd EMA value has to be populated. So confused....

4 REPLIES 4
Ksharp
Super User

Where is your data and output ?

 

 

data have;
do stock=1 to 10;
do date=1 to 100;
price=ranuni(0)*10;output;
end;
end;
run;

%let constant=0.18;
data want;
set have;
by stock;
if first.stock then do;n=0;ema=.;end;
n+1;

retain ema;
if n=22 then ema=price;
else if n gt 22 then ema= price*&constant + (1-&constant)*ema;

run;

 

lakshmiG
Obsidian | Level 7
((closeprice-emaprevious day)*constant))+emaprevious day, Is this formula right? I will apply and try yours. Thanks for your timely help!!! I am new to this domain. Thanks for your support.
lakshmiG
Obsidian | Level 7
I am going through the link. Thanks for your valuable suggestion!!!

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is ANOVA?

ANOVA, or Analysis Of Variance, is used to compare the averages or means of two or more populations to better understand how they differ. Watch this tutorial for more.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 1956 views
  • 2 likes
  • 3 in conversation