<?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 Re: Calculate daily usage from cumulative usage in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Calculate-daily-usage-from-cumulative-usage/m-p/595108#M171143</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
    set have;
    by id;
    prev_cumulative=lag(cumulative_reading);
    if first.id then daily=cumulative_reading;
    else daily=cumulative_reading-prev_cumulative;
    drop prev_cumulative;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This assumes the data is properly sorted.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, you might want to re-think your example, there is no logical way to get a daily of 50 for the first row.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 09 Oct 2019 15:34:41 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2019-10-09T15:34:41Z</dc:date>
    <item>
      <title>Calculate daily usage from cumulative usage</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-daily-usage-from-cumulative-usage/m-p/595107#M171142</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a large data set with customer IDs and daily cumulative usage readings. I'm looking to calculate the daily usage per customer from the cumulative usage and add it as a variable in the data set. Any suggestions on how to even begin doing this? Thank you!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;HAVE
ID     date     cumulative_reading     
1     10/1/19     100
1     10/2/19     150
1     10/3/19     200
2     10/1/19     100
2     10/2/19     500
2     10/3/19     900



WANT
ID     Date     Cumulative_reading     Daily Usage
1     10/1/19      100                          50
1     10/2/19      150                          50
1     10/3/19      200                              
2     10/1/19      100                          400
2     10/2/19      500                          400
2     10/3/19      900&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 09 Oct 2019 15:30:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-daily-usage-from-cumulative-usage/m-p/595107#M171142</guid>
      <dc:creator>smcconn5</dc:creator>
      <dc:date>2019-10-09T15:30:25Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate daily usage from cumulative usage</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-daily-usage-from-cumulative-usage/m-p/595108#M171143</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
    set have;
    by id;
    prev_cumulative=lag(cumulative_reading);
    if first.id then daily=cumulative_reading;
    else daily=cumulative_reading-prev_cumulative;
    drop prev_cumulative;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This assumes the data is properly sorted.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, you might want to re-think your example, there is no logical way to get a daily of 50 for the first row.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 09 Oct 2019 15:34:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-daily-usage-from-cumulative-usage/m-p/595108#M171143</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-10-09T15:34:41Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate daily usage from cumulative usage</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-daily-usage-from-cumulative-usage/m-p/595112#M171144</link>
      <description>&lt;P&gt;Much easier if you put the value on the current record instead of the previous one.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have ;
  by id ;
  daily_usage=dif(cumulative_reading);
  if first.id then daily_usage=.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results:&lt;/P&gt;
&lt;PRE&gt;                           cumulative_    daily_
Obs    id          date      reading       usage

 1     1     2019-10-01        100            .
 2     1     2019-10-02        150           50
 3     1     2019-10-03        200           50
 4     2     2019-10-01        100            .
 5     2     2019-10-02        500          400
 6     2     2019-10-03        900          400
&lt;/PRE&gt;
&lt;P&gt;You can even decide if you want the first record's value to be missing or set it to the first value.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  if first.id then daily_usage=cumulative_reading;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results:&lt;/P&gt;
&lt;PRE&gt;                           cumulative_    daily_
Obs    id          date      reading       usage

 1     1     2019-10-01        100          100
 2     1     2019-10-02        150           50
 3     1     2019-10-03        200           50
 4     2     2019-10-01        100          100
 5     2     2019-10-02        500          400
 6     2     2019-10-03        900          400
&lt;/PRE&gt;</description>
      <pubDate>Wed, 09 Oct 2019 15:40:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-daily-usage-from-cumulative-usage/m-p/595112#M171144</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-10-09T15:40:21Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate daily usage from cumulative usage</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-daily-usage-from-cumulative-usage/m-p/595128#M171155</link>
      <description>&lt;P&gt;You need to do a "look-ahead":&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID date :mmddyy10. cumulative_reading;
format date yymmddd10.;
datalines; 
1     10/1/19     100
1     10/2/19     150
1     10/3/19     200
2     10/1/19     100
2     10/2/19     500
2     10/3/19     900
;

options mergenoby=nowarn;

data want;
merge
  have
  have (
    firstobs=2
    keep=id cumulative_reading
    rename=(id=nextid cumulative_reading=nextval)
  )
;
if id = nextid then daily_usage = nextval - cumulative_reading;
drop nextid nextval;
run;

proc print data=want noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;                    cumulative_    daily_
ID          date      reading       usage

 1    2019-10-01        100           50 
 1    2019-10-02        150           50 
 1    2019-10-03        200            . 
 2    2019-10-01        100          400 
 2    2019-10-02        500          400 
 2    2019-10-03        900            . 
&lt;/PRE&gt;</description>
      <pubDate>Wed, 09 Oct 2019 16:27:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-daily-usage-from-cumulative-usage/m-p/595128#M171155</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-10-09T16:27:05Z</dc:date>
    </item>
  </channel>
</rss>

