<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Get maximum value in the interval of 7 days in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Get-maximum-value-in-the-interval-of-7-days/m-p/934048#M367338</link>
    <description>&lt;P&gt;I am trying to calculate the maximum daily dose in the last 7 days [t-7, t]. Each row has a dose and the respective day and I need a new column with the maximum value in the last seven days (that day inclusive). I can have situations where some days aren't available.&lt;/P&gt;&lt;P&gt;Please see below the type of data I have and what I'm aiming for:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data have;&lt;BR /&gt;input id $ dose day;&lt;BR /&gt;cards;&lt;BR /&gt;A 1 1&lt;BR /&gt;A 1 2&lt;BR /&gt;A 1 3&lt;BR /&gt;A 1 4&lt;BR /&gt;A 1 5&lt;BR /&gt;A 2 6&lt;BR /&gt;A 2 7&lt;BR /&gt;A 3 8&lt;BR /&gt;A 1 9&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;data want;&lt;BR /&gt;input id $ dose day max7days;&lt;BR /&gt;cards;&lt;BR /&gt;A 1 1 1&lt;BR /&gt;A 1 2 1&lt;BR /&gt;A 1 3 1&lt;BR /&gt;A 1 4 1&lt;BR /&gt;A 1 5 1&lt;BR /&gt;A 2 6 2&lt;BR /&gt;A 1 7 2&lt;BR /&gt;A 3 8 3&lt;BR /&gt;A 1 9 3&lt;BR /&gt;;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;I'm fairly new SAS and would appreciate suggestions on the best method to perform this calculation. Please let me know if I should provide mor details so you can help me. Thanks in advance!&lt;/P&gt;</description>
    <pubDate>Fri, 28 Jun 2024 13:00:12 GMT</pubDate>
    <dc:creator>sgsilva</dc:creator>
    <dc:date>2024-06-28T13:00:12Z</dc:date>
    <item>
      <title>Get maximum value in the interval of 7 days</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Get-maximum-value-in-the-interval-of-7-days/m-p/934048#M367338</link>
      <description>&lt;P&gt;I am trying to calculate the maximum daily dose in the last 7 days [t-7, t]. Each row has a dose and the respective day and I need a new column with the maximum value in the last seven days (that day inclusive). I can have situations where some days aren't available.&lt;/P&gt;&lt;P&gt;Please see below the type of data I have and what I'm aiming for:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data have;&lt;BR /&gt;input id $ dose day;&lt;BR /&gt;cards;&lt;BR /&gt;A 1 1&lt;BR /&gt;A 1 2&lt;BR /&gt;A 1 3&lt;BR /&gt;A 1 4&lt;BR /&gt;A 1 5&lt;BR /&gt;A 2 6&lt;BR /&gt;A 2 7&lt;BR /&gt;A 3 8&lt;BR /&gt;A 1 9&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;data want;&lt;BR /&gt;input id $ dose day max7days;&lt;BR /&gt;cards;&lt;BR /&gt;A 1 1 1&lt;BR /&gt;A 1 2 1&lt;BR /&gt;A 1 3 1&lt;BR /&gt;A 1 4 1&lt;BR /&gt;A 1 5 1&lt;BR /&gt;A 2 6 2&lt;BR /&gt;A 1 7 2&lt;BR /&gt;A 3 8 3&lt;BR /&gt;A 1 9 3&lt;BR /&gt;;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;I'm fairly new SAS and would appreciate suggestions on the best method to perform this calculation. Please let me know if I should provide mor details so you can help me. Thanks in advance!&lt;/P&gt;</description>
      <pubDate>Fri, 28 Jun 2024 13:00:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Get-maximum-value-in-the-interval-of-7-days/m-p/934048#M367338</guid>
      <dc:creator>sgsilva</dc:creator>
      <dc:date>2024-06-28T13:00:12Z</dc:date>
    </item>
    <item>
      <title>Re: Get maximum value in the interval of 7 days</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Get-maximum-value-in-the-interval-of-7-days/m-p/934053#M367340</link>
      <description>&lt;P&gt;If you have SAS/ETS there are procs for doing things like that.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You could just let PROC SQL join the dataset with itself and use the MAX() aggregate function.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as 
select a.id, a.day, a.dose
     , max(b.dose) as max7days
from have a
inner join have b
  on a.id = b.id
  and b.day between a.day-6 and a.day 
group by a.id, a.day, a.dose
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But it might be a lot faster to do it in a data step.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If all DAY values are present it is pretty simple:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want2;
  set have;
  by id day ;
  array history[7] _temporary_;
  if first.id then call missing(of history[*]);
  history[ceil(day/7)] = dose;
  max7dose = max(of history[*]);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But if there are gaps then you will need to work a little harder.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want2;
  set have;
  by id day ;
  array history[7] _temporary_;
  lag_day = lag(day);
  if first.id then call missing(of history[*]);
  else do lag_day=lag_day+1 to day-1;
    history[ceil(lag_day/7)]=.;
  end;
  history[ceil(day/7)] = dose;
  max7dose = max(of history[*]);
  drop lag_day;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 28 Jun 2024 13:25:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Get-maximum-value-in-the-interval-of-7-days/m-p/934053#M367340</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-06-28T13:25:02Z</dc:date>
    </item>
    <item>
      <title>Re: Get maximum value in the interval of 7 days</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Get-maximum-value-in-the-interval-of-7-days/m-p/934065#M367343</link>
      <description>Thank you so much &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;! This solved my issue.</description>
      <pubDate>Fri, 28 Jun 2024 14:04:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Get-maximum-value-in-the-interval-of-7-days/m-p/934065#M367343</guid>
      <dc:creator>sgsilva</dc:creator>
      <dc:date>2024-06-28T14:04:54Z</dc:date>
    </item>
  </channel>
</rss>

