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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 2 replies
  • 702 views
  • 0 likes
  • 3 in conversation