BookmarkSubscribeRSS Feed
brophymj
Quartz | Level 8
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?
2 REPLIES 2
LaurieF
Barite | Level 11

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;
art297
Opal | Level 21

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

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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