<?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: Subtracting Variables between two Rows based on other similar variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Subtracting-Variables-between-two-Rows-based-on-other-similar/m-p/360442#M274656</link>
    <description>&lt;P&gt;First, sort by customer, product_id and order_date.&lt;/P&gt;
&lt;P&gt;Then, in a data step, use&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
by customer product_id;
n_order = quantity;
n_order_prev = lag(quantity);
difference = n_order_prev - n_order;
if not first.product_id then output;
drop quantity;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The lag() function retrieves the previous value of a certain variable, it works as a FIFO chain.&lt;/P&gt;</description>
    <pubDate>Mon, 22 May 2017 14:06:12 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2017-05-22T14:06:12Z</dc:date>
    <item>
      <title>Subtracting Variables between two Rows based on other similar variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Subtracting-Variables-between-two-Rows-based-on-other-similar/m-p/360436#M274654</link>
      <description>&lt;P&gt;Hello. &amp;nbsp;I am working with customer data that looks like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Order_id&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;Customer_Id&lt;/TD&gt;&lt;TD&gt;order_date&lt;/TD&gt;&lt;TD&gt;Product_id&lt;/TD&gt;&lt;TD&gt;Quantity&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;c1&lt;/TD&gt;&lt;TD&gt;1/23/2014&lt;/TD&gt;&lt;TD&gt;P1&lt;/TD&gt;&lt;TD&gt;10&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;c1&lt;/TD&gt;&lt;TD&gt;1/23/2016&lt;/TD&gt;&lt;TD&gt;P1&lt;/TD&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;c2&lt;/TD&gt;&lt;TD&gt;2/5/2015&lt;/TD&gt;&lt;TD&gt;P2&lt;/TD&gt;&lt;TD&gt;6&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;c2&lt;/TD&gt;&lt;TD&gt;6/6/2015&lt;/TD&gt;&lt;TD&gt;P2&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to find customers whose quantity is decreasing over time based on product_ID and Customer_ID. &amp;nbsp;My final table would like this:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Customer&lt;/TD&gt;&lt;TD&gt;Product_ID&lt;/TD&gt;&lt;TD&gt;N order&lt;/TD&gt;&lt;TD&gt;N-1 Order&lt;/TD&gt;&lt;TD&gt;Difference&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;C1&lt;/TD&gt;&lt;TD&gt;P1&lt;/TD&gt;&lt;TD&gt;10&lt;/TD&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;6&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;C2&lt;/TD&gt;&lt;TD&gt;P2&lt;/TD&gt;&lt;TD&gt;6&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My original idea is to use Proc SQL but I am having trouble figuring out how to the combine the rows based on customer and procuct id. &amp;nbsp; Can anyone provide some help?&lt;/P&gt;</description>
      <pubDate>Mon, 22 May 2017 13:55:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Subtracting-Variables-between-two-Rows-based-on-other-similar/m-p/360436#M274654</guid>
      <dc:creator>help2891</dc:creator>
      <dc:date>2017-05-22T13:55:27Z</dc:date>
    </item>
    <item>
      <title>Re: Subtracting Variables between two Rows based on other similar variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Subtracting-Variables-between-two-Rows-based-on-other-similar/m-p/360441#M274655</link>
      <description>&lt;P&gt;A data step solution could be&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Order_id$ Customer_Id$ order_date:mmddyy10. Product_id$ Quantity;
format order_date mmddyy10.;
datalines; 
1 c1 1/23/2014 P1 10 
2 c1 1/23/2016 P1 4 
3 c2 2/5/2015 P2 6 
4 c2 6/6/2015 P2 2 
;

proc sort data = have;
	by Customer_Id Product_id;
run;

data want;
	format Customer_Id Product_id lag_N_Order N_Order Difference;
	set have;
	by Customer_Id Product_id;

	lag_N_Order = lag1(Quantity);
	N_Order = Quantity;
	Difference = lag_N_Order - N_Order;

	keep Customer_Id Product_id N_Order lag_N_Order Difference;
	if last.Customer_Id &amp;amp; last.Product_id &amp;amp; Difference&amp;gt;0;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 22 May 2017 14:05:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Subtracting-Variables-between-two-Rows-based-on-other-similar/m-p/360441#M274655</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2017-05-22T14:05:11Z</dc:date>
    </item>
    <item>
      <title>Re: Subtracting Variables between two Rows based on other similar variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Subtracting-Variables-between-two-Rows-based-on-other-similar/m-p/360442#M274656</link>
      <description>&lt;P&gt;First, sort by customer, product_id and order_date.&lt;/P&gt;
&lt;P&gt;Then, in a data step, use&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
by customer product_id;
n_order = quantity;
n_order_prev = lag(quantity);
difference = n_order_prev - n_order;
if not first.product_id then output;
drop quantity;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The lag() function retrieves the previous value of a certain variable, it works as a FIFO chain.&lt;/P&gt;</description>
      <pubDate>Mon, 22 May 2017 14:06:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Subtracting-Variables-between-two-Rows-based-on-other-similar/m-p/360442#M274656</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-05-22T14:06:12Z</dc:date>
    </item>
    <item>
      <title>Re: Subtracting Variables between two Rows based on other similar variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Subtracting-Variables-between-two-Rows-based-on-other-similar/m-p/360447#M274657</link>
      <description>&lt;P&gt;Same approach (i.e., data step), but I don't think you need to create extra variables:&lt;/P&gt;
&lt;PRE&gt;data decreases;
  set have;
  by customer_id Product_id;
  if quantity lt lag(quantity) and first.Product_id eq 0 then output;
run;
&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;</description>
      <pubDate>Mon, 22 May 2017 14:26:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Subtracting-Variables-between-two-Rows-based-on-other-similar/m-p/360447#M274657</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-05-22T14:26:54Z</dc:date>
    </item>
  </channel>
</rss>

