<?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: Country and Data data - calculate the difference between today and yesterday within country in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Country-and-Data-data-calculate-the-difference-between-today-and/m-p/632020#M187333</link>
    <description>&lt;P&gt;The way I solved this was to use proc sql. I added a URN to each record using _N_ in a datastep and then joined on urn=urn-1.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks for the other replies.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Regards&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Andrew&lt;/P&gt;</description>
    <pubDate>Fri, 13 Mar 2020 18:46:20 GMT</pubDate>
    <dc:creator>andrewjmdata</dc:creator>
    <dc:date>2020-03-13T18:46:20Z</dc:date>
    <item>
      <title>Country and Data data - calculate the difference between today and yesterday within country</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Country-and-Data-data-calculate-the-difference-between-today-and/m-p/630657#M186718</link>
      <description>&lt;P&gt;Hi, I have a large data set that has country and dates within each country and a value col1. I want to calculate the percentage change between each day and previous day of col1. The first date for each country, the value should be 0.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;At moment my code just does difference (diff) If I can get this to work in my code then % should be easy. At moment can't get difference to work correctly.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The code I have looks like this and almost works...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sort data=have;&lt;BR /&gt;by country;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;BR /&gt;set have;&lt;BR /&gt;by country;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;/* this gives me a number 1..n within each country */&lt;BR /&gt;if first.country then seq=1;&lt;BR /&gt;else seq+1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;/* this gives each country a number effectively... prob no necessary */&lt;/P&gt;
&lt;P&gt;if seq=1 then counter+1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;/* Here I am hoping that it will give me the difference between current and last record in group */&lt;BR /&gt;if not first.country then diff=col1-lag(col1);&lt;BR /&gt;else diff=0;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Where it fails is when there is a 0 in col1 it seems to get the difference between the current record and where there was a value in the col1 last!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any ideas appreciated!&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;P&gt;Andrew&lt;/P&gt;</description>
      <pubDate>Mon, 09 Mar 2020 15:10:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Country-and-Data-data-calculate-the-difference-between-today-and/m-p/630657#M186718</guid>
      <dc:creator>andrewjmdata</dc:creator>
      <dc:date>2020-03-09T15:10:59Z</dc:date>
    </item>
    <item>
      <title>Re: Country and Data data - calculate the difference between today and yesterday within country</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Country-and-Data-data-calculate-the-difference-between-today-and/m-p/630680#M186730</link>
      <description>&lt;P&gt;Yes, LAG should not be used on just some of the observations or (as you noticed) it gives the wrong answer.&amp;nbsp; Here is a simple change:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  by country;
  diff = dif(col1);
  if first.country then diff=0;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 09 Mar 2020 16:56:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Country-and-Data-data-calculate-the-difference-between-today-and/m-p/630680#M186730</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2020-03-09T16:56:01Z</dc:date>
    </item>
    <item>
      <title>Re: Country and Data data - calculate the difference between today and yesterday within country</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Country-and-Data-data-calculate-the-difference-between-today-and/m-p/632020#M187333</link>
      <description>&lt;P&gt;The way I solved this was to use proc sql. I added a URN to each record using _N_ in a datastep and then joined on urn=urn-1.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks for the other replies.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Regards&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Andrew&lt;/P&gt;</description>
      <pubDate>Fri, 13 Mar 2020 18:46:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Country-and-Data-data-calculate-the-difference-between-today-and/m-p/632020#M187333</guid>
      <dc:creator>andrewjmdata</dc:creator>
      <dc:date>2020-03-13T18:46:20Z</dc:date>
    </item>
  </channel>
</rss>

