<?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: Summing rows with identical values using the LAG function and keeping one row in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Summing-rows-with-identical-values-using-the-LAG-function-and/m-p/899822#M355633</link>
    <description>&lt;P&gt;Not sure about your overall logic, but you should definitely fix the miss use of LAG().&amp;nbsp; If you run LAG() only on some of the observations, then it can only return some of the values (the ones you passed it by running LAG()).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data new;
  set old;
  by ID atc p_date;
  lag_duration = lag(duration);
  if p_date=lag(p_date) then do;
 /* Code can now use LAG_DURATION variable to get value from previous observation */
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 24 Oct 2023 17:03:49 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2023-10-24T17:03:49Z</dc:date>
    <item>
      <title>Summing rows with identical values using the LAG function and keeping one row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Summing-rows-with-identical-values-using-the-LAG-function-and/m-p/899815#M355631</link>
      <description>&lt;P&gt;Hi everyone,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have some prescription redemption data that looks something like this:&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;TABLE border="1" width="100%"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="20%" height="30px"&gt;ID&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;ATC&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;P_DATE&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;PACKSIZE&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;DURATION(days)&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="20%" height="30px"&gt;1&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;AA10&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;03_01_12&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;100&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;30&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="20%" height="30px"&gt;1&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;AA10&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;03_01_12&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;100&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;30&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="20%" height="30px"&gt;1&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;AA10&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;03_01_12&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;100&amp;nbsp;&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;30&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="20%" height="30px"&gt;2&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;AA10&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;14_02_12&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;50&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;30&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="20%" height="30px"&gt;2&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;AA10&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;15_04_12&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;50&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;30&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="20%" height="30px"&gt;2&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;BB10&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;12_01_12&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;100&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;60&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="20%" height="30px"&gt;2&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;BB10&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;15_06_12&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;100&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;60&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="20%" height="30px"&gt;3&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;CC10&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;27_08_13&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;50&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;30&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="20%" height="30px"&gt;3&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;CC10&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;12_09_13&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;50&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;30&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here, data are sorted by ID, ATC, P_DATE (ascending).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have the duration in days of each prescription, but some individuals redeem multiple prescriptions for the same drug the same day, and I would like to calculate the total duration for those prescriptions.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example, for the first three rows, I would like to sum&amp;nbsp;the three durations for the prescriptions with the same ID, atc and P_date.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My initial idea was to add up the durations using the LAG function in a way such that for the first three rows (same ID, atc and p_date), the durations are added up in one of the three prescriptions (with a duration of 90 days) and then delete the other two using proc sort and nodupkey later.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This way the prescriptions become a type of "prescription redemption events" such that for each specific ID, date and ATC code, only one prescription with the summed duration exists.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My base code (which unfortunately only partially works - if there are more than 2 identical prescriptiond per date it creates errors and there are other issues):&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data new;
 set old;
by ID atc p_date;
if p_date=lag(p_date) then do;
duration=duration+lag(duration);
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;However, I haven't been able to do this. Does anyone have a suggestion? Feel free to suggest different approaches.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks.&lt;/P&gt;</description>
      <pubDate>Tue, 24 Oct 2023 16:11:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Summing-rows-with-identical-values-using-the-LAG-function-and/m-p/899815#M355631</guid>
      <dc:creator>ha33</dc:creator>
      <dc:date>2023-10-24T16:11:14Z</dc:date>
    </item>
    <item>
      <title>Re: Summing rows with identical values using the LAG function and keeping one row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Summing-rows-with-identical-values-using-the-LAG-function-and/m-p/899817#M355632</link>
      <description>&lt;P&gt;The data step solution would be to RETAIN a value and add to it.&lt;/P&gt;
&lt;PRE&gt;data new;
  set old;
  by ID atc p_date;
  retain cumduration;
  if first.p_date cumduration=duration;
  else cumduration=cumduration+duration;
  if last.p_date;
run;&lt;/PRE&gt;
&lt;P&gt;Retain means that the variable values are kept across the data step iteration boundary.&lt;/P&gt;
&lt;P&gt;The BY creates automatic variables that indicate whether an observation is the first or last of a by group. You access these values using First.variablename and Last.variablename. These are temporary and do not get written to the data set and have values of 1, true, or 0, false for if something is the first or last.&lt;/P&gt;
&lt;P&gt;The If Last.p_date is a subsetting if and only observations where the condition is true are kept. So no need for proc sort nodupekey. You could comment out this bit to see the data set get built with the cumulative duration and then uncomment to execute to subset the data.&lt;/P&gt;
&lt;P&gt;I add a variable for the cumulative duration so you can see if it works as expected.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 24 Oct 2023 16:23:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Summing-rows-with-identical-values-using-the-LAG-function-and/m-p/899817#M355632</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-10-24T16:23:27Z</dc:date>
    </item>
    <item>
      <title>Re: Summing rows with identical values using the LAG function and keeping one row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Summing-rows-with-identical-values-using-the-LAG-function-and/m-p/899822#M355633</link>
      <description>&lt;P&gt;Not sure about your overall logic, but you should definitely fix the miss use of LAG().&amp;nbsp; If you run LAG() only on some of the observations, then it can only return some of the values (the ones you passed it by running LAG()).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data new;
  set old;
  by ID atc p_date;
  lag_duration = lag(duration);
  if p_date=lag(p_date) then do;
 /* Code can now use LAG_DURATION variable to get value from previous observation */
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 24 Oct 2023 17:03:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Summing-rows-with-identical-values-using-the-LAG-function-and/m-p/899822#M355633</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-10-24T17:03:49Z</dc:date>
    </item>
    <item>
      <title>Re: Summing rows with identical values using the LAG function and keeping one row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Summing-rows-with-identical-values-using-the-LAG-function-and/m-p/899828#M355634</link>
      <description>&lt;P&gt;Thanks a lot - that works perfectly.&lt;/P&gt;</description>
      <pubDate>Tue, 24 Oct 2023 17:39:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Summing-rows-with-identical-values-using-the-LAG-function-and/m-p/899828#M355634</guid>
      <dc:creator>ha33</dc:creator>
      <dc:date>2023-10-24T17:39:36Z</dc:date>
    </item>
  </channel>
</rss>

