DATA Step, Macro, Functions and more

Largest 7 day price movement

Reply
Super Contributor
Posts: 259

Largest 7 day price movement

I have a dataset covering a year of daily data (365 rows) with two field: date and price i.e.

Date Price
1/1/15. 100
2/1/15. 99
3/1/15. 101
4/1/15. 102
...
...
31/12/15 115

I want to calculate the highest %increase over any consecutive 7 days over the 365 days. What code could I use to calculate this?
Super Contributor
Posts: 252

Re: Largest 7 day price movement

Is this all you want?

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

data want;
set have;
retain max_increase 0;
max_increase = max(max_increase, price / lag6(price));
format max_increase 6.2;
run;
PROC Star
Posts: 7,474

Re: Largest 7 day price movement

Not sure if I correctly understand what you are trying to get, but I think that the following comes close:

 

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

data need (keep=date pric max_increase);
  set have;
  retain stack0-stack6;
  array stack(0:6) stack0-stack6;
  stack(mod(_n_,7))=price;
  x=mod(_n_,7);
  max_increase=0;
  do i=2 to 6, 0;
    if i eq 0 then j=6;
    else j=i-1;
    max_increase=max(stack(i)/stack(j),max_increase);
  end;
run;

proc sql;
  select max(max_increase)
    from need
  ;
quit;

Art, CEO, AnalystFinder.com

Ask a Question
Discussion stats
  • 2 replies
  • 135 views
  • 0 likes
  • 3 in conversation