<?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: Subtract first from second rows and put it in a new column in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Subtract-first-from-second-rows-and-put-it-in-a-new-column/m-p/653032#M22496</link>
    <description>&lt;P&gt;A loop would be useful if you had to do the same process to a set of variables.&amp;nbsp; But you have only one variable (totalsales), and you want to compare successive observations within the data set:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data All;
  Input Department$ Totalsales Date DDMMYY8.;
  format Date yymmdd10.;
cards;
D 45 01/03/19
D 47 02/03/19
D 51 03/03/19
D 55 04/03/19
B 35 01/03/19
B 37 02/03/19
B 43 03/03/19
B 42 04/03/19
E 59 03/03/19
E 64 04/03/19
M 45 01/03/19
M 50 02/03/19
M 55 03/03/19
M 56 04/03/19
M 59 05/03/19
M 59 07/03/19
M 59 06/03/19
run;
data want;
  set all;
  by department notsorted;
  newsales=dif(totalsales);
  if first.department then newsales=.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The DIF function is defined as &amp;nbsp; dif(x)=x-lag(x).&amp;nbsp; The "if first.department …" statement tests for the start of a new department, and set newsales to missing, thereby avoiding contamination of the start of one department with a lagged value from the prior department.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The "notsorted" option is because you seem to have your data observations grouped by department, but not in ascending order.&lt;/P&gt;</description>
    <pubDate>Tue, 09 Jun 2020 03:34:50 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2020-06-09T03:34:50Z</dc:date>
    <item>
      <title>Subtract first from second rows and put it in a new column</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Subtract-first-from-second-rows-and-put-it-in-a-new-column/m-p/653031#M22495</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I have a data that has three variables namely Department, Totalsales and Date. I have many departments. Each day, new sales adds to the the Totalsales column and shows the total until that specific day. I want to know how many new sells there are each day by subtracting one day's totalsales from the next day's totalsales and put it in a new column. Therefore at last I will have four columns (Department, Totalsales, Date, and newsells). I sorted the data by Department and Date. I tried to work using do loop but lost on the way. Can anybody assist me with this. I am posting a hypothetical data similar to my data here.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Data All;&lt;BR /&gt;Input Department$ Totalsales Date DDMMYY8.;&lt;BR /&gt;format Date yymmdd10.;&lt;BR /&gt;cards;&lt;BR /&gt;D 45 01/03/19&lt;BR /&gt;D 47 02/03/19&lt;BR /&gt;D 51 03/03/19&lt;BR /&gt;D 55 04/03/19&lt;BR /&gt;B 35 01/03/19&lt;BR /&gt;B 37 02/03/19&lt;BR /&gt;B 43 03/03/19&lt;BR /&gt;B 42 04/03/19&lt;BR /&gt;E 59 03/03/19&lt;BR /&gt;E 64 04/03/19&lt;BR /&gt;M 45 01/03/19&lt;BR /&gt;M 50 02/03/19&lt;BR /&gt;M 55 03/03/19&lt;BR /&gt;M 56 04/03/19&lt;BR /&gt;M 59 05/03/19&lt;BR /&gt;M 59 07/03/19&lt;BR /&gt;M 59 06/03/19;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;I want new sales for each department in the first column.&lt;/P&gt;</description>
      <pubDate>Thu, 04 Jun 2020 00:10:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Subtract-first-from-second-rows-and-put-it-in-a-new-column/m-p/653031#M22495</guid>
      <dc:creator>million</dc:creator>
      <dc:date>2020-06-04T00:10:11Z</dc:date>
    </item>
    <item>
      <title>Re: Subtract first from second rows and put it in a new column</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Subtract-first-from-second-rows-and-put-it-in-a-new-column/m-p/653032#M22496</link>
      <description>&lt;P&gt;A loop would be useful if you had to do the same process to a set of variables.&amp;nbsp; But you have only one variable (totalsales), and you want to compare successive observations within the data set:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data All;
  Input Department$ Totalsales Date DDMMYY8.;
  format Date yymmdd10.;
cards;
D 45 01/03/19
D 47 02/03/19
D 51 03/03/19
D 55 04/03/19
B 35 01/03/19
B 37 02/03/19
B 43 03/03/19
B 42 04/03/19
E 59 03/03/19
E 64 04/03/19
M 45 01/03/19
M 50 02/03/19
M 55 03/03/19
M 56 04/03/19
M 59 05/03/19
M 59 07/03/19
M 59 06/03/19
run;
data want;
  set all;
  by department notsorted;
  newsales=dif(totalsales);
  if first.department then newsales=.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The DIF function is defined as &amp;nbsp; dif(x)=x-lag(x).&amp;nbsp; The "if first.department …" statement tests for the start of a new department, and set newsales to missing, thereby avoiding contamination of the start of one department with a lagged value from the prior department.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The "notsorted" option is because you seem to have your data observations grouped by department, but not in ascending order.&lt;/P&gt;</description>
      <pubDate>Tue, 09 Jun 2020 03:34:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Subtract-first-from-second-rows-and-put-it-in-a-new-column/m-p/653032#M22496</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-06-09T03:34:50Z</dc:date>
    </item>
    <item>
      <title>Re: Subtract first from second rows and put it in a new column</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Subtract-first-from-second-rows-and-put-it-in-a-new-column/m-p/653038#M22497</link>
      <description>Thank you so much. This works!&lt;BR /&gt;&lt;BR /&gt;Just a followup question. What if we want the the start of a new department column to be the same as the totalsales value in that column? Not a missing value.</description>
      <pubDate>Thu, 04 Jun 2020 01:35:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Subtract-first-from-second-rows-and-put-it-in-a-new-column/m-p/653038#M22497</guid>
      <dc:creator>million</dc:creator>
      <dc:date>2020-06-04T01:35:12Z</dc:date>
    </item>
    <item>
      <title>Re: Subtract first from second rows and put it in a new column</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Subtract-first-from-second-rows-and-put-it-in-a-new-column/m-p/654844#M22560</link>
      <description>Sorry for the delayed response - been offline.  I leave the answer to your question as a "homework" assignment.</description>
      <pubDate>Tue, 09 Jun 2020 03:36:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Subtract-first-from-second-rows-and-put-it-in-a-new-column/m-p/654844#M22560</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-06-09T03:36:16Z</dc:date>
    </item>
  </channel>
</rss>

