<?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: Filling values of ID from most recent ID by values of another variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Filling-values-of-ID-from-most-recent-ID-by-values-of-another/m-p/249670#M46987</link>
    <description>&lt;P&gt;Well, few ways to do it, you could sort and retain, or do it in SQL like:&lt;/P&gt;
&lt;PRE&gt;data have;
  input id first_name $ last_name $ month categorical_var $;
datalines;
1 abc def 200901 a
1 abc def 200902 a
2 abc def 200903 a
3 abc def 200901 b
4 abc def 200902 b
;
run;

proc sql;
  create table WANT as
  select  B.ID,
          A.FIRST_NAME,
          A.LAST_NAME,
          A.MONTH,
          A.CATEGORICAL_VAR 
  from    HAVE A
  left join (select CATEGORICAL_VAR,max(ID) as ID from HAVE group by CATEGORICAL_VAR) B
  on      A.CATEGORICAL_VAR=B.CATEGORICAL_VAR;
quit;&lt;/PRE&gt;</description>
    <pubDate>Fri, 12 Feb 2016 14:28:02 GMT</pubDate>
    <dc:creator>RW9</dc:creator>
    <dc:date>2016-02-12T14:28:02Z</dc:date>
    <item>
      <title>Filling values of ID from most recent ID by values of another variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Filling-values-of-ID-from-most-recent-ID-by-values-of-another/m-p/249665#M46985</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a dataset at the person/month/categorical group variable level, like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID &amp;nbsp; &amp;nbsp; &amp;nbsp;First name &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Last name &amp;nbsp; &amp;nbsp; &amp;nbsp; Month &amp;nbsp; &amp;nbsp; &amp;nbsp; Categorical var&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;abc &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;def &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;200901 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;a&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;abc &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;def &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;200902 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;a&lt;/P&gt;&lt;P&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;abc &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;def &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;200903 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;a&lt;/P&gt;&lt;P&gt;3 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;abc &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;def &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;200901 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;b&lt;/P&gt;&lt;P&gt;4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; abc &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;def &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;200902 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;b&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm trying to clean the ID variable by setting all values of ID within levels of the categorical variable, as the value of ID as of the most recent month. So basically, as long as a person has the same name, I assume they're the same person and overwrite older values of ID (within categorical var=a) with the newest value, like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID &amp;nbsp; &amp;nbsp; &amp;nbsp;First name &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Last name &amp;nbsp; &amp;nbsp; &amp;nbsp; Month &amp;nbsp; &amp;nbsp; &amp;nbsp; Categorical var&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; abc &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;def &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;200901 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;a&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; abc &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;def &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;200902 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;a&lt;/P&gt;&lt;P&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;abc &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;def &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;200903 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;a&lt;/P&gt;&lt;P&gt;4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; abc &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;def &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;200901 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;b&lt;/P&gt;&lt;P&gt;4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; abc &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;def &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;200902 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;b&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So basically, I&amp;nbsp;want to&amp;nbsp;retain all the same data, but with ID now consistent within each level of categorical var for each person.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help is much appreciated.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 12 Feb 2016 13:58:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Filling-values-of-ID-from-most-recent-ID-by-values-of-another/m-p/249665#M46985</guid>
      <dc:creator>Walternate</dc:creator>
      <dc:date>2016-02-12T13:58:12Z</dc:date>
    </item>
    <item>
      <title>Re: Filling values of ID from most recent ID by values of another variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Filling-values-of-ID-from-most-recent-ID-by-values-of-another/m-p/249670#M46987</link>
      <description>&lt;P&gt;Well, few ways to do it, you could sort and retain, or do it in SQL like:&lt;/P&gt;
&lt;PRE&gt;data have;
  input id first_name $ last_name $ month categorical_var $;
datalines;
1 abc def 200901 a
1 abc def 200902 a
2 abc def 200903 a
3 abc def 200901 b
4 abc def 200902 b
;
run;

proc sql;
  create table WANT as
  select  B.ID,
          A.FIRST_NAME,
          A.LAST_NAME,
          A.MONTH,
          A.CATEGORICAL_VAR 
  from    HAVE A
  left join (select CATEGORICAL_VAR,max(ID) as ID from HAVE group by CATEGORICAL_VAR) B
  on      A.CATEGORICAL_VAR=B.CATEGORICAL_VAR;
quit;&lt;/PRE&gt;</description>
      <pubDate>Fri, 12 Feb 2016 14:28:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Filling-values-of-ID-from-most-recent-ID-by-values-of-another/m-p/249670#M46987</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-02-12T14:28:02Z</dc:date>
    </item>
    <item>
      <title>Re: Filling values of ID from most recent ID by values of another variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Filling-values-of-ID-from-most-recent-ID-by-values-of-another/m-p/249681#M46989</link>
      <description>&lt;P&gt;I realize that in the example the IDs got higher the later the month was, but that is not the case in my source data, so I don't think selecting max ID would get me what I want.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 12 Feb 2016 14:41:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Filling-values-of-ID-from-most-recent-ID-by-values-of-another/m-p/249681#M46989</guid>
      <dc:creator>Walternate</dc:creator>
      <dc:date>2016-02-12T14:41:38Z</dc:date>
    </item>
    <item>
      <title>Re: Filling values of ID from most recent ID by values of another variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Filling-values-of-ID-from-most-recent-ID-by-values-of-another/m-p/249686#M46990</link>
      <description>&lt;P&gt;Then change the second group per:&lt;/P&gt;
&lt;PRE&gt;  left join (select distinct CATEGORICAL_VAR,ID as ID from HAVE group by CATEGORICAL_VAR having max(MONTH)) B&lt;/PRE&gt;
&lt;P&gt;However that will cause duplicates if there are more than one cat/id in the last month. &amp;nbsp;If you can post a datastep with some test data, exactly matching your requirements the code can be adapted, can only go on what I see.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 12 Feb 2016 14:48:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Filling-values-of-ID-from-most-recent-ID-by-values-of-another/m-p/249686#M46990</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-02-12T14:48:52Z</dc:date>
    </item>
    <item>
      <title>Re: Filling values of ID from most recent ID by values of another variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Filling-values-of-ID-from-most-recent-ID-by-values-of-another/m-p/250014#M47109</link>
      <description>&lt;P&gt;Assuming the data has been sorted by month like your sample data did.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input id first_name $ last_name $ month categorical_var $;
datalines;
1 abc def 200901 a
1 abc def 200902 a
2 abc def 200903 a
3 abc def 200901 b
4 abc def 200902 b
;
run;
data want;
do until(last.categorical_var);
 set have;
 by first_name  last_name  categorical_var ;
end;
_id=id;
do until(last.categorical_var);
 set have;
 by first_name  last_name  categorical_var ;
 id=_id;output;
end;
drop _id;
run;
 &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 15 Feb 2016 03:26:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Filling-values-of-ID-from-most-recent-ID-by-values-of-another/m-p/250014#M47109</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-02-15T03:26:53Z</dc:date>
    </item>
  </channel>
</rss>

