<?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: generatae new variable by group in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/generatae-new-variable-by-group/m-p/618443#M181434</link>
    <description>&lt;P&gt;Logically, this statement does the wrong thing:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;   if first.year then maxyear = year;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Instead, use:&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;   &lt;SPAN class="token keyword"&gt;if&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;first&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;.zipcode&lt;/SPAN&gt; &lt;SPAN class="token keyword"&gt;then&lt;/SPAN&gt; maxyear &lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;year&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 20 Jan 2020 01:53:48 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2020-01-20T01:53:48Z</dc:date>
    <item>
      <title>generatae new variable by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/generatae-new-variable-by-group/m-p/618440#M181431</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=ret.ra;
 by Name zipcode descending year;
run;

data ret.rb;
   set ret.ra;
   by Name zipcode;
   retain maxyear;
   if first.year then maxyear = year;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I ran the code above to create maxyear variable by group (Name and Zipcode)&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, the result just shows the same year.. Could you please help how to do it? Thank you.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Name&amp;nbsp; &amp;nbsp;Zipcode&amp;nbsp; &amp;nbsp;year&amp;nbsp; &amp;nbsp;&lt;U&gt;maxyear (what I want)&lt;/U&gt;&lt;/P&gt;&lt;P&gt;ABC&amp;nbsp; &amp;nbsp; 30010&amp;nbsp; &amp;nbsp; &amp;nbsp;2006&amp;nbsp; &amp;nbsp; &amp;nbsp;2007&lt;/P&gt;&lt;P&gt;ABC&amp;nbsp; &amp;nbsp; 30010&amp;nbsp; &amp;nbsp; &amp;nbsp;2007&amp;nbsp; &amp;nbsp; &amp;nbsp;2007&lt;/P&gt;&lt;P&gt;ABC&amp;nbsp; &amp;nbsp; 30011&amp;nbsp; &amp;nbsp; &amp;nbsp;2005&amp;nbsp; &amp;nbsp; &amp;nbsp;2005&lt;/P&gt;&lt;P&gt;BCC&amp;nbsp; &amp;nbsp; 30010&amp;nbsp; &amp;nbsp; 2001&amp;nbsp; &amp;nbsp; &amp;nbsp;2003&lt;/P&gt;&lt;P&gt;BCC&amp;nbsp; &amp;nbsp; 30010&amp;nbsp; &amp;nbsp; 2002&amp;nbsp; &amp;nbsp; &amp;nbsp;2003&lt;/P&gt;&lt;P&gt;BCC&amp;nbsp; &amp;nbsp; 30010&amp;nbsp; &amp;nbsp; 2003&amp;nbsp; &amp;nbsp; &amp;nbsp;2003&lt;/P&gt;</description>
      <pubDate>Mon, 20 Jan 2020 01:43:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/generatae-new-variable-by-group/m-p/618440#M181431</guid>
      <dc:creator>cphd</dc:creator>
      <dc:date>2020-01-20T01:43:59Z</dc:date>
    </item>
    <item>
      <title>Re: generatae new variable by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/generatae-new-variable-by-group/m-p/618441#M181432</link>
      <description>&lt;P&gt;Switch to Proc SQL and enjoy autoremerge&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
select *,max(year) as max_year
from have
group by name,zipcode;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The advantage is sorting isn't required&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 20 Jan 2020 02:56:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/generatae-new-variable-by-group/m-p/618441#M181432</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-01-20T02:56:24Z</dc:date>
    </item>
    <item>
      <title>Re: generatae new variable by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/generatae-new-variable-by-group/m-p/618443#M181434</link>
      <description>&lt;P&gt;Logically, this statement does the wrong thing:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;   if first.year then maxyear = year;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Instead, use:&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;   &lt;SPAN class="token keyword"&gt;if&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;first&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;.zipcode&lt;/SPAN&gt; &lt;SPAN class="token keyword"&gt;then&lt;/SPAN&gt; maxyear &lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;year&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 20 Jan 2020 01:53:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/generatae-new-variable-by-group/m-p/618443#M181434</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2020-01-20T01:53:48Z</dc:date>
    </item>
    <item>
      <title>Re: generatae new variable by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/generatae-new-variable-by-group/m-p/618448#M181439</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/306356"&gt;@cphd&lt;/a&gt;,&amp;nbsp; as&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt;&amp;nbsp;pointed out, just change first.year to &lt;STRONG&gt;first.zipcode&lt;/STRONG&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	input name :$3. zipcode year;
	datalines;
ABC 30010 2006
ABC 30010 2007
ABC 30011 2005
BCC 30010 2001
BCC 30010 2002
BCC 30010 2003
;
run;

data desired;
	input name :$3. zipcode year maxyear;
	datalines;
ABC 30010 2006 2007
ABC 30010 2007 2007
ABC 30011 2005 2005
BCC 30010 2001 2003
BCC 30010 2002 2003
BCC 30010 2003 2003
;
run;

proc sort data=have;
	by name zipcode descending year;
run;

data want;
	set have;
	by name zipcode;

	if first.zipcode then
		maxyear=year;
	retain maxyear;
run;

*Checking that desired=want;
proc sort data=desired;
	by name zipcode descending year;
run;
proc compare base=desired compare=want;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 20 Jan 2020 02:46:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/generatae-new-variable-by-group/m-p/618448#M181439</guid>
      <dc:creator>unison</dc:creator>
      <dc:date>2020-01-20T02:46:34Z</dc:date>
    </item>
  </channel>
</rss>

