moro
moro

Reputation: 13

SAS 4GL - How to compute weekly stock value differences from discontinous dataset?

I have a dataset of stock prices. Obviously it contains observations only for the days when stock exchanges operate. Additionaly the dataset starts in 2007-01-02.

What I want to obtain is to get the weekly differences in price divided by the price one week ago for year 2007.

I don't know how to do this. Most SAS 4GL procedures do not provide access to other rows then the current one. retain won't work since there are 7 previous values to be remembered and accessed in FIFO way. Moreover I cannot use lag() since I don't know the distance to the previous value as measured in read values. There are holidays and so on what makes the distance not constant.

How are such values obtained in SAS 4GL?

edit

sample data

Upvotes: 1

Views: 144

Answers (1)

Reeza
Reeza

Reputation: 21274

You can use the intnx function with the one week interval. Using SQL you can join the table to itself and use the intnx in the join condition. Assuming your dates are SAS dates this is code that is designed around the sashelp.stocks example.

proc sql;
create table want as
select a.stock, a.date, b.date as date7, 
    a.price, b.price as price7, 
    a.price-b.price as price_diff
from have as a
left join have as b
on a.stock=b.stock
and a.date=intnx('day', b.date, -7)
order by a.stock, a.date;
quit;

Upvotes: 1

Related Questions