Obsidian | Level 7

## How to code A(t-250)*A(t-249)* ... A(t-1) efficently

I am trying to create a reference price measure that is an average of past 250 days stock prices, weighted by the probabilities of a stock not being traded since the date of the corresponding price:
reference_pricet=(pricet-250*turnover_ratiot-250*(1-turnover_ratiot-249)*(1-turnover_ratiot-248)*...*(1-turnover_ratiot-1)+pricet-249*turnover_ratiot-249*(1-turnover_ratiot-248)*(1-turnover_ratiot-247)*...*(1-turnover_ratiot-1)+...+pricet-1*turnover_ratiot-1)/k, where k is a constant that ensure price weights add up to 1 (see the formula below).

The following link is for STATA:

https://www.statalist.org/forums/forum/general-stata-discussion/general/1329146-generating-a-referen...

For each stock, how to code the reference price in SAS? Price, Turnover ratio each day are given.

Thanks very much!

6 REPLIES 6
Obsidian | Level 7

## Re: How to code A(t-250)*A(t-249)* ... A(t-1) efficently

The format of the dataset is as follow:

ID             Date             Price             Turnover

AAA       1990/08/25         8                    0.05

AAA        1990/08/26         8.05               0.06

.....

BBB

....

CCC

and so on

Tourmaline | Level 20

## Re: How to code A(t-250)*A(t-249)* ... A(t-1) efficently

Do you have SAS/ETS?

Obsidian | Level 7

## Re: How to code A(t-250)*A(t-249)* ... A(t-1) efficently

I am not sure if my sas has it. How to check it?

If I have it, how to code it?
Super User

## Re: How to code A(t-250)*A(t-249)* ... A(t-1) efficently

@ZZB wrote:
I am not sure if my sas has it. How to check it?

If I have it, how to code it?

I think that you may actually want IML, SAS's matrix language more than ETS. ETS does time series analysis but I haven't had a license for 20 years so no practice...

```Proc setinit;
run;
```

If you see SAS/ETS in the output of the log you have a license for ETS.

If you have the license run:

```Proc product_status;
run;```

to see if it is installed. If licensed and not installed then update the installation.

Obsidian | Level 7

## Re: How to code A(t-250)*A(t-249)* ... A(t-1) efficently

Any help? Thanks!

Obsidian | Level 7

## Re: How to code A(t-250)*A(t-249)* ... A(t-1) efficently

I use the simple data steps to calculate the above formula, in this case I just replace "t-250" with "t-4" for simplicity.

If I want to back to t-250, how to code efficiently?

/****************************************/

*proc printto log=junk;
proc expand data=MSF out=TO method=none;
by permno;
id date;
run;
*proc printto;

data TO1;
set TO;
run;

*proc printto log=junk;
proc expand data=TO1 (keep=permno date prc TO Lead_TO_1) out=TO2 method=none;
by permno;
id date;
convert Lead_TO_1 = CUM_Lead_TO3 / transformout=(MOVPROD 3 trimleft 2); *** 1990/5 vs. 1990/1, 3=Date interval - 1;
convert Lead_TO_1 = CUM_Lead_TO2 / transformout=(MOVPROD 2 trimleft 1); *** 1990/5 vs. 1990/2;
run;
*proc printto;

*proc printto log=junk;
proc expand data=TO2 out=TO3 method=none;
by permno;
id date;
run;
*proc printto;

data TO4;
set TO3;
PRC1=PRC*TO;
run;

data TO4;
set TO4;
K1=TO;
run;

*proc printto log=junk;
proc expand data=TO4 out=TO5 method=none;
by permno;
id date;
convert PRC4 = LAG_TO4 / transformout=(lag 4);
convert PRC3 = LAG_TO3 / transformout=(lag 3);
convert PRC2 = LAG_TO2 / transformout=(lag 2);
convert PRC1 = LAG_TO1 / transformout=(lag 1);

convert K4 = LAG_K4 / transformout=(lag 4);
convert K3 = LAG_K3 / transformout=(lag 3);
convert K2 = LAG_K2 / transformout=(lag 2);
convert K1 = LAG_K1 / transformout=(lag 1);
run;
*proc printto;

data TO6;
set TO5;
RP=(LAG_TO4+LAG_TO3+LAG_TO2+LAG_TO1)/(LAG_K4+LAG_K3+LAG_K2+LAG_K1);
run;

Discussion stats
• 6 replies
• 1028 views
• 0 likes
• 3 in conversation