Examples / BETAS.RPF |
BETAS.RPF is an example of a repetitive analysis. The actual calculation is rather simple (just a regression of a stock return on market return to get the beta), but it shows how to run that over a range of stocks input off a separate file.
This first reads a list of ticker symbols from the file ticker.txt. READ(VARYING) is used as we don't know in advance how many items are on that. Once we've read the data, we can get the number using the %SIZE function.
The data file with returns has one series named MARKET and others with the ticker names. Outside the loop, we read in MARKET; inside the loop we use DATA redirection to read data using the ticker name into a SERIES named SECURITY. LINREG(NOPRINT) is used to compute the beta, which will be the second coefficient in the %BETA vector. That's written to the COPY unit: a file named betas.lst.
Full Program
cal(m) 1986:1
allocate 1996:12
open data ticker.txt
declare vector[label] tickers
read(varying) tickers
compute ntickers=%size(tickers)
open data returns.rat
open copy betas.lst
data(format=rats) / market
*
do i=1,ntickers
clear security
data(format=rats) / security<<&tickers(i)
linreg(noprint) security
# constant market
display(unit=copy) tickers(i) @20 ##.##### %beta(2)
end do i
close copy
Output
There is no direct output from this—instead, it produces a file (betas.lst) with the estimated betas along with the identifier of the security:
r2000 1.02795
spmidcap 0.99785
Copyright © 2025 Thomas A. Doan