BookmarkSubscribeRSS Feed
brophymj
Quartz | Level 8

 

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;

 

 

 

10 REPLIES 10
ballardw
Super User

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?

brophymj
Quartz | Level 8
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
brophymj
Quartz | Level 8
I don't have sas/ets
brophymj
Quartz | Level 8
Any suggestions on an approach?
rogerjdeangelis
Barite | Level 11
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
ballardw
Super User

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?

brophymj
Quartz | Level 8

Hi Ballardw

 

Thanks for your feedback. 

 

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

 

 

brophymj
Quartz | Level 8
I think the solution is probably through the use of arrays but they're not my strongpoint
ballardw
Super User

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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 10 replies
  • 1532 views
  • 0 likes
  • 3 in conversation