## Proc Expand Exponential Moving Average question

Solved
Super Contributor
Posts: 506

# Proc Expand Exponential Moving Average question

Hi everyone,

I am trying to use Proc expand to calculate the EMA and I compare to Excel calculation, I have 2 questions.

First, in proc expand, how can I put transformout=( ewma 1/3 ) instead of transformout=( ewma 0.3333 );

(I would like to put it 1/3 to avoid rounding error plus, it will help me to convert to Macro later)

Second, I only want SAS to start calculate average once N>=number of average. Right now, SAS calculate average for all record.

HHC

data have ;
input id var1 expected_EMA ;
cards;
1 2 0
2 2 0
3 10 0
4 2 0
5 6 4.4
6 9 5.933333333
7 8 6.622222222
8 1 4.748148148
9 6 5.165432099
10 5 5.110288066
11 8 6.073525377
12 9 7.049016918
13 6 6.699344612
14 2 5.132896408
15 1 3.755264272
16 74 27.17017618
;run;

proc expand data=have out=want;
convert var1=EMA/ transformout=( ewma 0.3333 );
run;

Accepted Solutions
Solution
‎09-10-2014 11:18 PM
Posts: 5,543

## Re: Proc Expand Exponential Moving Average question

Not very elegant, but close to what you want:

data have ;

input id var1 expected_EMA ;

cards;

1 2 0

2 2 0

3 10 0

4 2 0

5 6 4.4

6 9 5.933333333

7 8 6.622222222

8 1 4.748148148

9 6 5.165432099

10 5 5.110288066

11 8 6.073525377

12 9 7.049016918

13 6 6.699344612

14 2 5.132896408

15 1 3.755264272

16 74 27.17017618

;

%let ewmaOrder=3;

%let ewmaFraction=%sysevalf(1/&ewmaOrder);

proc expand data=have out=want;

convert var1=smoothedVar1 / transformout=( ewma &ewmaFraction );

run;

quit;

data want;

set want;

if _n_ <= (&ewmaOrder+1) then smoothedVar1=0;

run;

PG

PG

All Replies
Solution
‎09-10-2014 11:18 PM
Posts: 5,543

## Re: Proc Expand Exponential Moving Average question

Not very elegant, but close to what you want:

data have ;

input id var1 expected_EMA ;

cards;

1 2 0

2 2 0

3 10 0

4 2 0

5 6 4.4

6 9 5.933333333

7 8 6.622222222

8 1 4.748148148

9 6 5.165432099

10 5 5.110288066

11 8 6.073525377

12 9 7.049016918

13 6 6.699344612

14 2 5.132896408

15 1 3.755264272

16 74 27.17017618

;

%let ewmaOrder=3;

%let ewmaFraction=%sysevalf(1/&ewmaOrder);

proc expand data=have out=want;

convert var1=smoothedVar1 / transformout=( ewma &ewmaFraction );

run;

quit;

data want;

set want;

if _n_ <= (&ewmaOrder+1) then smoothedVar1=0;

run;

PG

PG
🔒 This topic is solved and locked.