BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ilikesas
Barite | Level 11

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!!!

1 ACCEPTED SOLUTION

Accepted Solutions
stat_sas
Ammonite | Level 13

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;

View solution in original post

2 REPLIES 2
kannand
Lapis Lazuli | Level 10

Can you show the output you are expecting?

Kannan Deivasigamani
stat_sas
Ammonite | Level 13

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;

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and save with the early bird rate—just $795!

Register now

How to connect to databases in SAS Viya

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.

Discussion stats
  • 2 replies
  • 1561 views
  • 0 likes
  • 3 in conversation