## transforming stock prices into rates of change for many stocks

Solved
Super Contributor
Posts: 459

# transforming stock prices into rates of change for many stocks

[ Edited ]

Hi,

suppose I have the following data of stock prices:

data price;
input a b;
datalines;
10 20
15 16
17 18
11 21
13 17
;
run;

what I would like to do is to transform this data into a data of changes in the prices. Right now I am doing it very inefficiently in the following way:

data price;
set price;
lag_a=lag(a);
lag_b=lag(b);
run;

data returns;
set price;
ret_a=(a-lag_a)/(lag_a);
ret_b=(b-lag_b)/(lag_b);
run;

data returns (keep=ret_a ret_b) ;
set returns;
run;

data returns ;
set returns;
a=ret_a;
b=ret_b;
run;

data returns (keep = a b);
set returns;
run;

Here are the 2 things which I would like to improve:

1) to make the rate of change calculation automatic --> not to type individually lag_a, lag_b .... ret_a, ret_b becasue if I have say 100 stocks it becomes tedious...

2) when I finally have the return data, I would like the variables to have their original names, for example ret_a reappears as a (please note its not simple relableing, I will need the stock return variables to have their original variable names).

Thank you very much!!!

Accepted Solutions
Solution
‎10-31-2015 12:47 PM
Posts: 1,270

## Re: transforming stock prices into rates of change for many stocks

May be this is what you are looking for

data want(drop=i);
set price;
array v(*) _numeric_;
do i=1 to dim(v);
v(i)=(v{i}-lag(v{i}))/lag(v{i});
end;
run;

All Replies
Regular Contributor
Posts: 161

## Re: transforming stock prices into rates of change for many stocks

Can you show the output you are expecting?

Kannan Deivasigamani
Solution
‎10-31-2015 12:47 PM
Posts: 1,270

## Re: transforming stock prices into rates of change for many stocks

May be this is what you are looking for

data want(drop=i);
set price;
array v(*) _numeric_;
do i=1 to dim(v);
v(i)=(v{i}-lag(v{i}))/lag(v{i});
end;
run;

🔒 This topic is solved and locked.