## Max price change within interval i.e. 30 days or 365 days

Super Contributor
Posts: 262

# Max price change within interval i.e. 30 days or 365 days

Using teh dataset below, is there a straightforward way of calculating:

(1) the largest price increase in any consecutive 30/360/720 day period. So the algorithm would need to check the price change of from every day to every other day within 30/360/720 days and output a maximum

(2) the largest price increase in a particular month

(3) the largest price increase in a particular year

(4) the correlation of price movements between January 2015 and January 2016 i.e. are price movements in Jan 2015 correlated with price movements in Jan 2016?

data have;
attrib date length=4 format=date9.;
do date = '1jan2015'd to '31dec2016'd;
price = int(ranuni(225465114) * 30) + 95;
output;
end;
run;

Super User
Posts: 13,950

## Re: Max price change within interval i.e. 30 days or 365 days

I think you may need to expand on your definitions of 2 and 3.

2) the largest price increase in a particular month: Do you mean single day to day increase within calendar month, largest end of month compared with first of month, increase from previous month end, or something else.

Some of this may be easier if you have SAS/ETS available. Do you?

Super Contributor
Posts: 262

## Re: Max price change within interval i.e. 30 days or 365 days

Hi ballard, for part 2) I mean largest price movement within a month .i.e. Could be between the 14th and 15th or the 1st and the 30th or any combination

3) same as above but it's a calendar year instead of a month
Super Contributor
Posts: 262

## Re: Max price change within interval i.e. 30 days or 365 days

I don't have sas/ets
Super Contributor
Posts: 262

## Re: Max price change within interval i.e. 30 days or 365 days

Any suggestions on an approach?
Valued Guide
Posts: 505

## Re: Max price change within interval i.e. 30 days or 365 days

``````I assumed disjoint months. not sure what you want for output.

proc sql;
create
table cart as
select
l.month
,max(abs(l.price - r.price)) as jandif
from
date as l full outer join date as r
on
l.month ne r.month and
l.date ne r.date
group
by l.month
;quit;

Up to 40 obs WORK.CART total obs=3

Obs    MONTH    JANDIF

1       1        29
2       2        28
3       3        29
``````
Super User
Posts: 13,950

## Re: Max price change within interval i.e. 30 days or 365 days

How about providing a concrete (NOT random) 45 days of data and show us what the result for the 30 day problem would look like based on that data. That should not be to large to do by hand.

You may also need to be careful about "increase". What is the result for if the data only decreases over a given time interval?

Super Contributor
Posts: 262

## Re: Max price change within interval i.e. 30 days or 365 days

[ Edited ]

Hi Ballardw

I've attached an excel with two tabs:

The first shows the calculation in excel for calculating the max price increase (not decrease) in a calendar week (Mon-Sun) over a two month period.

The second is an expansion of the first example and shows how to calculate the max price increase in any 7 day consecutive period.

Once the above has been solved, it is straightforward expanding this to months/ bigger intervals.

You will see in both examples that the agorithm needs to calculate a matrix of price change and then max over the matrix

So to calculate the max price change in a calendar week you need to perform 21 calculations and take the max the get the max price change. If we wanted to do the same for January, you would need to perform 1,860 calculations.

Does this make sense?

Thanks

Super Contributor
Posts: 262

## Re: Max price change within interval i.e. 30 days or 365 days

I think the solution is probably through the use of arrays but they're not my strongpoint
Super Contributor
Posts: 262

Super User
Posts: 13,950

## Re: Max price change within interval i.e. 30 days or 365 days

See if this does the 7 days. Since the data is random it may not match expectations.

```data have;
attrib date length=4 format=date9.;
do date = '1jan2015'd to '31dec2016'd;
price = int(ranuni(225465114) * 30) + 95;
PriceInc = dif(Price)/lag(price);
If PriceInc<0 then PriceInc=.;
Max7Day = max(priceInc,lag(priceinc),lag2(priceinc),lag3(priceinc),lag4(priceinc),lag5(priceinc),lag6(priceinc));
output;
end;
label
PriceInc = "Percent increase from previous day"
Max7Day  = "Maximum daily price increase percent 7 days"
;
format PriceInc Max7Day percent8.1;
run;```

If that at matches then some of what you requested may be possible with other report procedures and careful use of formats

```proc tabulate data=have;
class date;
format date yymon.;
var priceinc;
tables date,
priceinc*max*f=percent8.1;
run;```
Discussion stats
• 10 replies
• 259 views
• 0 likes
• 3 in conversation