<?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: How to calculate the differences in sales at different reference dates in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-the-differences-in-sales-at-different-reference/m-p/744736#M233352</link>
    <description>&lt;P&gt;&lt;SPAN&gt;thanks for your solution, it works and at the same time I understand better how arrays works&lt;/SPAN&gt;&lt;/P&gt;</description>
    <pubDate>Mon, 31 May 2021 07:47:52 GMT</pubDate>
    <dc:creator>Ccasagran737</dc:creator>
    <dc:date>2021-05-31T07:47:52Z</dc:date>
    <item>
      <title>How to calculate the differences in sales at different reference dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-the-differences-in-sales-at-different-reference/m-p/744454#M233231</link>
      <description>&lt;P&gt;Hi, I'm using SAS Studio and I need your help.&lt;BR /&gt;I have a list of variables indicating the stock of sales at the end of the month (in the example stock_202012 stock_202101 stock_202102 ...).&lt;BR /&gt;I am trying to calculate for each observation the difference between the reference stock (indicated in the example by the variable "date") and the previous month.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is my input dataset&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	infile datalines;
	input id date ddmmyy8. stock_202012 stock_202101 stock_202102 stock_202103 stock_202104;
	format date date7.;
	datalines;
1 10012021 20 23 22 27 30
2 07042021 10 10 10 10 11
3 23022021 44 44 48 48 50
4 12042021 21 24 27 30 31
5 09022021 30 40 50 43 44
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;This is the output desired&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
	infile datalines;
	input id delta_stock;
	datalines;
1 3
2 1
3 4
4 1
5 10
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;For example, for the first observation the delta_stock is obtained by making 23 (stock_202101 since the variable "date" is equal to 10/01/2021) - 20 (stock_202012) equal to 3.&lt;/P&gt;&lt;P&gt;Thanks for your help.&lt;/P&gt;&lt;P&gt;Daniele (Italy).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 28 May 2021 16:12:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-the-differences-in-sales-at-different-reference/m-p/744454#M233231</guid>
      <dc:creator>Ccasagran737</dc:creator>
      <dc:date>2021-05-28T16:12:14Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate the differences in sales at different reference dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-the-differences-in-sales-at-different-reference/m-p/744541#M233260</link>
      <description>&lt;P&gt;Use a two-dimensional array indexed on year (2020:2021) and month (1:12) based on the named variables.&amp;nbsp; Assign a dummy variable to unavailable array elements (i.e. non-existent months for 2020 and 2021)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	infile datalines;
	input id date ddmmyy8. stock_202012 stock_202101 stock_202102 stock_202103 stock_202104;
	format date date9.;
	datalines;
1 10012021 20 23 22 27 30
2 07042021 10 10 10 10 11
3 23022021 44 44 48 48 50
4 12042021 21 24 27 30 31
5 09022021 30 40 50 43 44
run;

data want (drop=_:);
  set have;
  array stks {2020:2021,1:12} 
    _dummy       _dummy       _dummy       _dummy       _dummy _dummy _dummy _dummy _dummy _dummy _dummy stock_202012
    stock_202101 stock_202102 stock_202103 stock_202104 _dummy _dummy _dummy _dummy _dummy _dummy _dummy _dummy;

  _priordate=intnx('month',date,-1);
  dif=stks{year(date),month(date)} - stks{year(_priordate),month(_priordate)};
run;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 28 May 2021 20:13:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-the-differences-in-sales-at-different-reference/m-p/744541#M233260</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2021-05-28T20:13:22Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate the differences in sales at different reference dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-the-differences-in-sales-at-different-reference/m-p/744609#M233285</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	infile datalines;
	input id date ddmmyy8. stock_202012 stock_202101 stock_202102 stock_202103 stock_202104;
	format date date7.;
	datalines;
1 10012021 20 23 22 27 30
2 07042021 10 10 10 10 11
3 23022021 44 44 48 48 50
4 12042021 21 24 27 30 31
5 09022021 30 40 50 43 44
;
run;

data want;
 set have;
 delta_stock=
 vvaluex(cats('stock_',put(date,yymmn6.))) -
 vvaluex(cats('stock_',put(intnx('month',date,-1),yymmn6.)));
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 29 May 2021 12:05:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-the-differences-in-sales-at-different-reference/m-p/744609#M233285</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-05-29T12:05:05Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate the differences in sales at different reference dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-the-differences-in-sales-at-different-reference/m-p/744736#M233352</link>
      <description>&lt;P&gt;&lt;SPAN&gt;thanks for your solution, it works and at the same time I understand better how arrays works&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 31 May 2021 07:47:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-the-differences-in-sales-at-different-reference/m-p/744736#M233352</guid>
      <dc:creator>Ccasagran737</dc:creator>
      <dc:date>2021-05-31T07:47:52Z</dc:date>
    </item>
  </channel>
</rss>

