<?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 find the new value compared with prior 3-year observation in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-new-value-compared-with-prior-3-year-observation/m-p/709667#M218274</link>
    <description>&lt;P&gt;How to do this with correlated subqueries:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&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
;

proc sql;
select *
from have as a
where 
    not exists (
        select * from have 
        where 
            firm=a.firm and 
            year between a.year-3 and a.year-1 and 
            var=a.var) 
    and
    (select count(distinct year) from have
     where 
           firm=a.firm and 
           year between a.year-3 and a.year-1) = 3
    ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="PGStats_0-1609953900993.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/53258iB06112FB2C492433/image-size/medium?v=v2&amp;amp;px=400" role="button" title="PGStats_0-1609953900993.png" alt="PGStats_0-1609953900993.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 06 Jan 2021 17:25:13 GMT</pubDate>
    <dc:creator>PGStats</dc:creator>
    <dc:date>2021-01-06T17:25:13Z</dc:date>
    <item>
      <title>How to find the new value compared with prior 3-year observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-new-value-compared-with-prior-3-year-observation/m-p/709559#M218223</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;I would like to compare each year's observation with prior 3-year observations and find the unique value&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 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;So I compare each firm's each year&amp;nbsp; VAR with prior three-year VAR, and find the unique value of VAR, and this is what I want&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA WANT;
    INPUT (FIRM YEAR VAR) (:$8.);
    CARDS;
A 2003 chicken
A 2004 mice
B 2003 pasta
B 2004 cheese
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I tried something like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql;&lt;BR /&gt;select&lt;BR /&gt;b.*&lt;BR /&gt;from have a&lt;BR /&gt;RIGHT JOIN&lt;BR /&gt;have b&lt;BR /&gt;ON a.firm = b.firm and a.year = b.year-1 and a.VAR = b.VAR&lt;BR /&gt;WHERE a.VAR is null;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But this only compares the observation with the prior 1 year. How can I compare with all the prior three year observations?&lt;/P&gt;&lt;P&gt;Thank you&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Jan 2021 06:36:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-new-value-compared-with-prior-3-year-observation/m-p/709559#M218223</guid>
      <dc:creator>yanshuai</dc:creator>
      <dc:date>2021-01-06T06:36:57Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the new value compared with prior 3-year observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-new-value-compared-with-prior-3-year-observation/m-p/709562#M218226</link>
      <description>&lt;P&gt;I don't fully understand. Why should the obs&amp;nbsp;&lt;/P&gt;
&lt;PRE class="language-sas"&gt;&lt;CODE&gt;A 2003 chicken&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Be there. Please be specific &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Jan 2021 06:53:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-new-value-compared-with-prior-3-year-observation/m-p/709562#M218226</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2021-01-06T06:53:00Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the new value compared with prior 3-year observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-new-value-compared-with-prior-3-year-observation/m-p/709563#M218227</link>
      <description>&lt;P&gt;Is last year the same for all farms? (2004 in the test data).&lt;/P&gt;
&lt;P&gt;if positive then the you can run:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  create table want as 
   select distinct * from have
    where year between &amp;lt;last_year&amp;gt; - 2 and &amp;lt;last_year&amp;gt;;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If negative you need on first step to select the last year of each farm, add it to each observation and output only those which fits the requirement:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
   create table temp as
   select farm, max(year) as last_year
   from have
   group by farm;
   create table want as 
   select distinct h.* , t.last_year
   from have as h
   left join temp as t 
    on h.farm = t.farm and 
       h.year between t.last_year - 2 and t.last_year;
quit;   &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Jan 2021 07:02:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-new-value-compared-with-prior-3-year-observation/m-p/709563#M218227</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2021-01-06T07:02:03Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the new value compared with prior 3-year observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-new-value-compared-with-prior-3-year-observation/m-p/709667#M218274</link>
      <description>&lt;P&gt;How to do this with correlated subqueries:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&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
;

proc sql;
select *
from have as a
where 
    not exists (
        select * from have 
        where 
            firm=a.firm and 
            year between a.year-3 and a.year-1 and 
            var=a.var) 
    and
    (select count(distinct year) from have
     where 
           firm=a.firm and 
           year between a.year-3 and a.year-1) = 3
    ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="PGStats_0-1609953900993.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/53258iB06112FB2C492433/image-size/medium?v=v2&amp;amp;px=400" role="button" title="PGStats_0-1609953900993.png" alt="PGStats_0-1609953900993.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Jan 2021 17:25:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-new-value-compared-with-prior-3-year-observation/m-p/709667#M218274</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2021-01-06T17:25:13Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the new value compared with prior 3-year observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-new-value-compared-with-prior-3-year-observation/m-p/709773#M218341</link>
      <description>&lt;P&gt;Yes,&lt;/P&gt;&lt;P&gt;The value 'Chicken' is the new one compared with the prior three-year values.&lt;/P&gt;&lt;P&gt;In 2003, VAR = dog, chicken&lt;/P&gt;&lt;P&gt;In the prior three years (2000-2002), VAR = dog, cat, bird&lt;/P&gt;&lt;P&gt;Compared with 2000-2002, VAR in 2003 has a new value 'chicken'.&lt;/P&gt;</description>
      <pubDate>Thu, 07 Jan 2021 00:16:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-new-value-compared-with-prior-3-year-observation/m-p/709773#M218341</guid>
      <dc:creator>yanshuai</dc:creator>
      <dc:date>2021-01-07T00:16:32Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the new value compared with prior 3-year observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-new-value-compared-with-prior-3-year-observation/m-p/709778#M218344</link>
      <description>&lt;P&gt;Thank you! I figured it out!&lt;/P&gt;&lt;P&gt;Your code is very instructive. But it takes a lot of time still cannot run the result&lt;/P&gt;&lt;P&gt;I modify my code based on your instruction&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want 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 between b.year-3 and b.year-1
WHERE a.var is null;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;At least it gives me the result I look for.&lt;/P&gt;&lt;P&gt;Do you think my code is anywhere wrong?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 07 Jan 2021 01:48:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-new-value-compared-with-prior-3-year-observation/m-p/709778#M218344</guid>
      <dc:creator>yanshuai</dc:creator>
      <dc:date>2021-01-07T01:48:28Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the new value compared with prior 3-year observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-new-value-compared-with-prior-3-year-observation/m-p/709800#M218358</link>
      <description>&lt;P&gt;Running your code, I get&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="PGStats_0-1609993221700.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/53271i86B03D765755EFC2/image-size/medium?v=v2&amp;amp;px=400" role="button" title="PGStats_0-1609993221700.png" alt="PGStats_0-1609993221700.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;i.e. it returns cases that don't have data in some of the previous 3 years. That's not the result you asked for in your original post. If that's ok then what you did is fine.&lt;/P&gt;</description>
      <pubDate>Thu, 07 Jan 2021 04:24:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-new-value-compared-with-prior-3-year-observation/m-p/709800#M218358</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2021-01-07T04:24:58Z</dc:date>
    </item>
  </channel>
</rss>

