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
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Hi,

 

You may be best off posting some test data (in the form of a datastep), and what the output should look like to illustrate your problem.  It seems you have already found a formula:

EMA =( (close price)-Previous day EMA)*smoothing constant)+Previous day EMA

So what is the problem with it, you can retain the EMA variable to keep for the next record, and you could assign a record count to the observations to know when record 22 is encountered.  So not sure what the question is.

lakshmiG
Obsidian | Level 7
Hi, Thanks for your quick response.
I have updated my question with the excel sheet attached.
I was retaining the lag value till now, so the result value was blank for all records except 22nd obs. Now tried retaining result value. i will check whether it is populating correctly. Thanks much!!!
Rick_SAS
SAS Super FREQ

You can find a discussion and formulas for exponentially weighted moving average in the article "Rolling statistics in SAS/IML".  If you do not have access to SAS/IML software, the formula for the EWMA translates readily to a DATA step program that uses a LAG function.

 

You can also find references to DATA step programs at the botton of the article "Compute a moving average in SAS"

lakshmiG
Obsidian | Level 7
Thanks a lot!!! I will go through the article. Thanks for your Quick response!!!

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 3411 views
  • 2 likes
  • 3 in conversation