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!!!
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;
Can you show the output you are expecting?
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;
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.
Find more tutorials on the SAS Users YouTube channel.