<?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 a new value of a variable compared with all prior variable observations by ID, YEAR in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-a-new-value-of-a-variable-compared-with-all-prior/m-p/746660#M234254</link>
    <description>&lt;P&gt;What role does the Firm variable play in this process? You do not mention it at all.&lt;/P&gt;</description>
    <pubDate>Wed, 09 Jun 2021 05:56:43 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2021-06-09T05:56:43Z</dc:date>
    <item>
      <title>How to find a new value of a variable compared with all prior variable observations by ID, YEAR</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-a-new-value-of-a-variable-compared-with-all-prior/m-p/746656#M234251</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;I have a large panel data set, I would like to find a new value in VAR compared with all prior observations within-group BY FIRM, YEAR&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
A 2005 pig
A 2005 mice
A 2006 raccoon
A 2006 pig
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
B 2005 rice
B 2005 meatloaf
B 2006 pickle
B 2006 chili
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;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 2000 dog
A 2001 cat
A 2002 bird
A 2003 chicken
A 2004 mice
A 2005 pig
A 2006 raccoon
B 2000 fries
B 2001 burger
B 2001 steak
B 2003 pasta
B 2004 cheese
B 2005 rice
B 2005 meatloaf
B 2006 pickle
B 2006 chili
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;So, for each FIRM in a given YEAR, I want to find the new value in VAR.&lt;/P&gt;&lt;P&gt;How to define it as 'new'? Compare VAR in a given YEAR with all observations of VAR in all prior YEAR&lt;/P&gt;&lt;P&gt;For example, to find the new VAR for A in 2004, I will compare VAR in 2004 with VAR in 2003, 2002, 2001, and 2000.&lt;/P&gt;&lt;P&gt;to find the new values of VAR for A in 2006, I will compare VAR in 2006 with VAR in 2005, 2004, 2003, 2002, 2001, and 2000.&lt;/P&gt;&lt;P&gt;Same logic for B.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I wonder can I do this with SQL and without a loop macro?&lt;/P&gt;&lt;P&gt;Thank you very much. Appreciate your help.&lt;/P&gt;</description>
      <pubDate>Wed, 09 Jun 2021 05:28:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-a-new-value-of-a-variable-compared-with-all-prior/m-p/746656#M234251</guid>
      <dc:creator>yanshuai</dc:creator>
      <dc:date>2021-06-09T05:28:11Z</dc:date>
    </item>
    <item>
      <title>Re: How to find a new value of a variable compared with all prior variable observations by ID, YEAR</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-a-new-value-of-a-variable-compared-with-all-prior/m-p/746660#M234254</link>
      <description>&lt;P&gt;What role does the Firm variable play in this process? You do not mention it at all.&lt;/P&gt;</description>
      <pubDate>Wed, 09 Jun 2021 05:56:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-a-new-value-of-a-variable-compared-with-all-prior/m-p/746660#M234254</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-06-09T05:56:43Z</dc:date>
    </item>
    <item>
      <title>Re: How to find a new value of a variable compared with all prior variable observations by ID, YEAR</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-a-new-value-of-a-variable-compared-with-all-prior/m-p/746673#M234255</link>
      <description>&lt;P&gt;If you don't insist on the order in "want", try&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have out=want nodupkey;
	by firm var;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 09 Jun 2021 06:27:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-a-new-value-of-a-variable-compared-with-all-prior/m-p/746673#M234255</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2021-06-09T06:27:22Z</dc:date>
    </item>
    <item>
      <title>Re: How to find a new value of a variable compared with all prior variable observations by ID, YEAR</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-a-new-value-of-a-variable-compared-with-all-prior/m-p/746675#M234256</link>
      <description>data want2; set want; by firm year var; if first.var; run;&lt;BR /&gt;&lt;BR /&gt;how about that?&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Wed, 09 Jun 2021 06:35:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-a-new-value-of-a-variable-compared-with-all-prior/m-p/746675#M234256</guid>
      <dc:creator>Josie1</dc:creator>
      <dc:date>2021-06-09T06:35:37Z</dc:date>
    </item>
    <item>
      <title>Re: How to find a new value of a variable compared with all prior variable observations by ID, YEAR</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-a-new-value-of-a-variable-compared-with-all-prior/m-p/746709#M234274</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/188461"&gt;@yanshuai&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;With PROC SQL you can do something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
select firm, min(year) as year, var
from have
group by 1,3
order by 1,2;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note, however, that the ORDER BY clause does not necessarily maintain the sort order within a (FIRM, YEAR) BY group, for example, the order of &lt;FONT face="courier new,courier"&gt;"burger"&lt;/FONT&gt; and &lt;FONT face="courier new,courier"&gt;"steak"&lt;/FONT&gt; for &lt;FONT face="courier new,courier"&gt;FIRM="B"&lt;/FONT&gt; and &lt;FONT face="courier new,courier"&gt;YEAR="2001"&lt;/FONT&gt;. To maintain this sort order you could introduce a temporary sequential number and use it as a sort key:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _tmp / view=_tmp;
set have;
by firm year;
_seqno=_n_;
run;

proc sql;
create table want(drop=_s) as
select firm, min(year) as year, var, min(_seqno) as _s
from _tmp
group by 1,3
order by 1,4;
drop view _tmp;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 09 Jun 2021 10:30:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-a-new-value-of-a-variable-compared-with-all-prior/m-p/746709#M234274</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2021-06-09T10:30:10Z</dc:date>
    </item>
    <item>
      <title>Re: How to find a new value of a variable compared with all prior variable observations by ID, YEAR</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-a-new-value-of-a-variable-compared-with-all-prior/m-p/746761#M234289</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;FIRM is the ID variable, YEAR is the time variable, VAR is the variable I look for new value&lt;/P&gt;</description>
      <pubDate>Wed, 09 Jun 2021 14:27:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-a-new-value-of-a-variable-compared-with-all-prior/m-p/746761#M234289</guid>
      <dc:creator>yanshuai</dc:creator>
      <dc:date>2021-06-09T14:27:17Z</dc:date>
    </item>
    <item>
      <title>Re: How to find a new value of a variable compared with all prior variable observations by ID, YEAR</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-a-new-value-of-a-variable-compared-with-all-prior/m-p/746764#M234290</link>
      <description>&lt;P&gt;Then the first step: How do I get the dataset WANT?&lt;/P&gt;</description>
      <pubDate>Wed, 09 Jun 2021 14:31:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-a-new-value-of-a-variable-compared-with-all-prior/m-p/746764#M234290</guid>
      <dc:creator>yanshuai</dc:creator>
      <dc:date>2021-06-09T14:31:20Z</dc:date>
    </item>
    <item>
      <title>Re: How to find a new value of a variable compared with all prior variable observations by ID, YEAR</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-a-new-value-of-a-variable-compared-with-all-prior/m-p/746765#M234291</link>
      <description>&lt;P&gt;Interesting.&lt;/P&gt;&lt;P&gt;Your code should be what I expect. Very beautiful. How could I not think of this? lol&lt;/P&gt;&lt;P&gt;Thank you very much&lt;/P&gt;</description>
      <pubDate>Wed, 09 Jun 2021 14:34:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-a-new-value-of-a-variable-compared-with-all-prior/m-p/746765#M234291</guid>
      <dc:creator>yanshuai</dc:creator>
      <dc:date>2021-06-09T14:34:47Z</dc:date>
    </item>
  </channel>
</rss>

