<?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: Cutting off datastep calculations in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Cutting-off-datastep-calculations/m-p/619342#M181811</link>
    <description>&lt;P&gt;That makes sense thank you. I ended up doing the following to accomodate for the other entries as well (Brand &amp;amp; State) which in my example works, but needs testing on the live data now.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data diff;
	set have;
	by brand state key descending month;
	retain diff maxweeknum;
	if first.month then do; 
		diff = ar-lag(ar);
		if first.brand or first.State or first.Key then diff =.;
		maxweeknum=weeknum;
	end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 22 Jan 2020 22:12:26 GMT</pubDate>
    <dc:creator>Krueger</dc:creator>
    <dc:date>2020-01-22T22:12:26Z</dc:date>
    <item>
      <title>Cutting off datastep calculations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cutting-off-datastep-calculations/m-p/619336#M181806</link>
      <description>&lt;P&gt;Hi all, what I'm tryin to do is calculate the AR difference between months and offset based on the weeknum.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Below is my code which is working, however I need it to cut off and not calculate unless the Brand, State and Key all match from the previous record. Incorrect ARCalcs are output lines: 9-12, 17-20. The values for diff and arcalc should be '.' (the same as output lines 1-4).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hopefully this all makes sense. If not please ask away.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	input Brand $ State $ Key AR commax8. drevmo:mmddyy10. week:mmddyy10. weeknum month;
	format drevmo mmddyy10. week mmddyy10.;
	datalines;
ac ca 2 10000.00 01/01/2020 01/05/2020 1 1
ac ca 2 10000.00 01/01/2020 01/12/2020 2 1
ac ca 2 10000.00 01/01/2020 01/19/2020 3 1
ac ca 2 10000.00 01/01/2020 01/26/2020 4 1
ac ca 2 20000.00 02/01/2020 02/02/2020 1 2
ac ca 2 20000.00 02/01/2020 02/09/2020 2 2
ac ca 2 20000.00 02/01/2020 02/16/2020 3 2
ac ca 2 20000.00 02/01/2020 02/23/2020 4 2
ac ca 3 1000	 01/01/2020 01/05/2020 1 1
ac ca 3 1000 	 01/01/2020 01/12/2020 2 1
ac ca 3 1000 	 01/01/2020 01/19/2020 3 1
ac ca 3 1000 	 01/01/2020 01/26/2020 4 1
ac ca 3 2000 	 02/01/2020 02/02/2020 1 2
ac ca 3 2000 	 02/01/2020 02/09/2020 2 2
ac ca 3 2000 	 02/01/2020 02/16/2020 3 2
ac ca 3 2000 	 02/01/2020 02/23/2020 4 2
bb ca 2 100		 01/01/2020 01/05/2020 1 1
bb ca 2 100		 01/01/2020 01/12/2020 2 1
bb ca 2 100		 01/01/2020 01/19/2020 3 1
bb ca 2 100		 01/01/2020 01/26/2020 4 1
bb ca 2 200		 02/01/2020 02/02/2020 1 2
bb ca 2 200		 02/01/2020 02/09/2020 2 2
bb ca 2 200		 02/01/2020 02/16/2020 3 2
bb ca 2 200		 02/01/2020 02/23/2020 4 2
;
run;

proc sort data=have; by brand state key descending week ; run;

data diff;
	set have;
	by brand state key descending month;
	retain diff maxweeknum;
	if first.month then do; 
		diff = ar-lag(ar);
		maxweeknum=weeknum;
	end;
run;

data want;
	set diff;
	arcalc = ar - ((diff/maxweeknum)*weeknum);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;in the data diff section if I try filtering down further e.g.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data diff;
	set have;
	by brand state key descending month;
	retain diff maxweeknum;
	if first.month then do; 
                if brand=lag(brand) then do;
		diff = ar-lag(ar);
		maxweeknum=weeknum;
               end;
	end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It completely breaks the calculation and doesn't work as I am expecting it to.&lt;/P&gt;</description>
      <pubDate>Wed, 22 Jan 2020 22:02:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cutting-off-datastep-calculations/m-p/619336#M181806</guid>
      <dc:creator>Krueger</dc:creator>
      <dc:date>2020-01-22T22:02:10Z</dc:date>
    </item>
    <item>
      <title>Re: Cutting off datastep calculations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cutting-off-datastep-calculations/m-p/619339#M181809</link>
      <description>&lt;P&gt;You are conditionally running the LAG() function call. Since you are calling it inside that IF first.month test it is calculating returning the first value of the previous month. Not the value of the previous record, which would be the last value of the previous month.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For now let's assume that is what you actually want to do.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It sounds like you just need to add more code to "forget' that lagged value when you are starting an new group.&lt;/P&gt;
&lt;P&gt;So add a line to set DIFF missing when you are starting a new group.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data diff;
   set have;
  by brand state key descending month;
  retain diff maxweeknum;
  if first.month then do; 
    diff = ar-lag(ar);
    if first.key then diff=.;
    maxweeknum=weeknum;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 22 Jan 2020 22:06:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cutting-off-datastep-calculations/m-p/619339#M181809</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-01-22T22:06:58Z</dc:date>
    </item>
    <item>
      <title>Re: Cutting off datastep calculations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cutting-off-datastep-calculations/m-p/619342#M181811</link>
      <description>&lt;P&gt;That makes sense thank you. I ended up doing the following to accomodate for the other entries as well (Brand &amp;amp; State) which in my example works, but needs testing on the live data now.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data diff;
	set have;
	by brand state key descending month;
	retain diff maxweeknum;
	if first.month then do; 
		diff = ar-lag(ar);
		if first.brand or first.State or first.Key then diff =.;
		maxweeknum=weeknum;
	end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 22 Jan 2020 22:12:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cutting-off-datastep-calculations/m-p/619342#M181811</guid>
      <dc:creator>Krueger</dc:creator>
      <dc:date>2020-01-22T22:12:26Z</dc:date>
    </item>
  </channel>
</rss>

