<?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 How to understand the a.YEAR-1 = b.YEAR in the SQL when match a VAR with prior YEAR obs in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-understand-the-a-YEAR-1-b-YEAR-in-the-SQL-when-match-a/m-p/747236#M234497</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I would like to find the new value in a VAR in a given year compared with its prior year value. I luckily figured out the code, but I am very confused by the 'a.YEAR-1 = b.YEAR' in the code.&lt;/P&gt;&lt;P&gt;This is what I have&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA HAVE;
    INPUT FIRM :$8. YEAR VAR :$8.;
    CARDS;
A 2000 dog
A 2001 dog
A 2001 cat
A 2002 dog
A 2002 bird
A 2003 dog
A 2003 chicken
A 2004 chicken
A 2004 mice
B 2000 fries
B 2001 burger
B 2001 steak
B 2002 burger
B 2002 fries
B 2003 steak
B 2003 pasta
B 2004 pasta
B 2004 cheese
;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;This is the code I figured out and it gives me the outcome I expect&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as select
distinct a.firm, a.year, a.var 
from have as a
LEFT JOIN
have as b on a.firm = b.firm and a.var=b.var and a.YEAR-1=b.YEAR
WHERE b.var is null;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;However, I am curious why it is &lt;CODE class=" language-sas"&gt;a.YEAR-1=b.YEAR&lt;/CODE&gt; not &lt;CODE class=" language-sas"&gt;a.YEAR=b.YEAR-1&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;a&lt;/STRONG&gt; should be the focal data and compare it with its prior 1 year observations (&lt;STRONG&gt;b&lt;/STRONG&gt;), I thought it should be a.YEAR = b.YEAR-1&lt;/P&gt;&lt;P&gt;Do I understand it wrong?&lt;/P&gt;&lt;P&gt;Thank you&lt;/P&gt;</description>
    <pubDate>Thu, 10 Jun 2021 23:19:36 GMT</pubDate>
    <dc:creator>yanshuai</dc:creator>
    <dc:date>2021-06-10T23:19:36Z</dc:date>
    <item>
      <title>How to understand the a.YEAR-1 = b.YEAR in the SQL when match a VAR with prior YEAR obs</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-understand-the-a-YEAR-1-b-YEAR-in-the-SQL-when-match-a/m-p/747236#M234497</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I would like to find the new value in a VAR in a given year compared with its prior year value. I luckily figured out the code, but I am very confused by the 'a.YEAR-1 = b.YEAR' in the code.&lt;/P&gt;&lt;P&gt;This is what I have&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA HAVE;
    INPUT FIRM :$8. YEAR VAR :$8.;
    CARDS;
A 2000 dog
A 2001 dog
A 2001 cat
A 2002 dog
A 2002 bird
A 2003 dog
A 2003 chicken
A 2004 chicken
A 2004 mice
B 2000 fries
B 2001 burger
B 2001 steak
B 2002 burger
B 2002 fries
B 2003 steak
B 2003 pasta
B 2004 pasta
B 2004 cheese
;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;This is the code I figured out and it gives me the outcome I expect&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as select
distinct a.firm, a.year, a.var 
from have as a
LEFT JOIN
have as b on a.firm = b.firm and a.var=b.var and a.YEAR-1=b.YEAR
WHERE b.var is null;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;However, I am curious why it is &lt;CODE class=" language-sas"&gt;a.YEAR-1=b.YEAR&lt;/CODE&gt; not &lt;CODE class=" language-sas"&gt;a.YEAR=b.YEAR-1&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;a&lt;/STRONG&gt; should be the focal data and compare it with its prior 1 year observations (&lt;STRONG&gt;b&lt;/STRONG&gt;), I thought it should be a.YEAR = b.YEAR-1&lt;/P&gt;&lt;P&gt;Do I understand it wrong?&lt;/P&gt;&lt;P&gt;Thank you&lt;/P&gt;</description>
      <pubDate>Thu, 10 Jun 2021 23:19:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-understand-the-a-YEAR-1-b-YEAR-in-the-SQL-when-match-a/m-p/747236#M234497</guid>
      <dc:creator>yanshuai</dc:creator>
      <dc:date>2021-06-10T23:19:36Z</dc:date>
    </item>
    <item>
      <title>Re: How to understand the a.YEAR-1 = b.YEAR in the SQL when match a VAR with prior YEAR obs</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-understand-the-a-YEAR-1-b-YEAR-in-the-SQL-when-match-a/m-p/747240#M234500</link>
      <description>&lt;P&gt;The direction of the join makes a difference depending on exactly what you want.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;a.year -1&amp;nbsp; = b.year&amp;nbsp;&amp;nbsp; means : compare the value of the variable year in set A minus one with the value of year in set b.&lt;/P&gt;
&lt;P&gt;The place you might want the "- 1" part could depend on whether you are using a Left or Right join and the desired result.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;See the differences in this code and then check the result with yours:&lt;/P&gt;
&lt;PRE&gt;proc sql;
create table want2 as select
distinct b.firm, b.year, b.var 
from have as a
Right JOIN
have as b on a.firm = b.firm and a.var=b.var and a.YEAR=b.YEAR-1
WHERE a.var is null;
QUIT;&lt;/PRE&gt;
&lt;P&gt;Note where a. has been changed to b. or b. to a.&amp;nbsp; and the join is now a right join.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 11 Jun 2021 00:00:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-understand-the-a-YEAR-1-b-YEAR-in-the-SQL-when-match-a/m-p/747240#M234500</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-06-11T00:00:56Z</dc:date>
    </item>
  </channel>
</rss>

