<?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: For each ID keep last year and if it is missing keep the year before in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/For-each-ID-keep-last-year-and-if-it-is-missing-keep-the-year/m-p/852174#M336881</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/173636"&gt;@Satori&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;the data is already sorted by ID and descending year. Running this code got me an error.&lt;/P&gt;
&lt;P&gt;ERROR: WHERE clause operator requires compatible variables.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Why would you have created the YEAR variable as a CHARACTER string?&lt;/P&gt;
&lt;P&gt;If the data is in DESCENDING order of YEAR then you want the FIRST one and not the LAST one.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  by id descending year;
  where year in ('2017' '2018');
  if first.id;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 04 Jan 2023 18:12:38 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2023-01-04T18:12:38Z</dc:date>
    <item>
      <title>For each ID keep last year and if it is missing keep the year before</title>
      <link>https://communities.sas.com/t5/SAS-Programming/For-each-ID-keep-last-year-and-if-it-is-missing-keep-the-year/m-p/852115#M336842</link>
      <description>&lt;P&gt;I have a data set like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;id&lt;/TD&gt;&lt;TD&gt;varX&lt;/TD&gt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gt;2015&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I only want to keep one observation per ID, and I want to keep only if it is 2018 or 2017. So if 2018 exists I want it, otherwise I want 2017, otherwise I don't want it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is what I want to have:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;id&lt;/TD&gt;&lt;TD&gt;varX&lt;/TD&gt;&lt;TD&gt;varY&lt;/TD&gt;&lt;TD&gt;year&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1.5&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;2018&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;3.5&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;2017&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;13&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;2018&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Wed, 04 Jan 2023 13:43:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/For-each-ID-keep-last-year-and-if-it-is-missing-keep-the-year/m-p/852115#M336842</guid>
      <dc:creator>Satori</dc:creator>
      <dc:date>2023-01-04T13:43:11Z</dc:date>
    </item>
    <item>
      <title>Re: For each ID keep last year and if it is missing keep the year before</title>
      <link>https://communities.sas.com/t5/SAS-Programming/For-each-ID-keep-last-year-and-if-it-is-missing-keep-the-year/m-p/852116#M336843</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have;
    by id year;
run;
data want;
    set have;
    by id;
    if last.id and year in (2017,2018);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 04 Jan 2023 14:06:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/For-each-ID-keep-last-year-and-if-it-is-missing-keep-the-year/m-p/852116#M336843</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-01-04T14:06:15Z</dc:date>
    </item>
    <item>
      <title>Re: For each ID keep last year and if it is missing keep the year before</title>
      <link>https://communities.sas.com/t5/SAS-Programming/For-each-ID-keep-last-year-and-if-it-is-missing-keep-the-year/m-p/852117#M336844</link>
      <description>&lt;P&gt;Can you post the code you have tried (and post the data as a DATA step with cards data)?&amp;nbsp; Are you familiar with BY-group processing?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You could sort by ID and year, then use by group processing (if last.ID) to check the value of the year for that record, and only output it if it's 2017 or 2018.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or if you're a SQL person you could group by ID, and select the record having having year=max(year) and year IN (2017,2018).&lt;/P&gt;</description>
      <pubDate>Wed, 04 Jan 2023 13:49:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/For-each-ID-keep-last-year-and-if-it-is-missing-keep-the-year/m-p/852117#M336844</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2023-01-04T13:49:50Z</dc:date>
    </item>
    <item>
      <title>Re: For each ID keep last year and if it is missing keep the year before</title>
      <link>https://communities.sas.com/t5/SAS-Programming/For-each-ID-keep-last-year-and-if-it-is-missing-keep-the-year/m-p/852119#M336845</link>
      <description>&lt;P&gt;From where do you determine which years to select? Should that be taken from the dataset itself?&lt;/P&gt;</description>
      <pubDate>Wed, 04 Jan 2023 13:56:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/For-each-ID-keep-last-year-and-if-it-is-missing-keep-the-year/m-p/852119#M336845</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-01-04T13:56:53Z</dc:date>
    </item>
    <item>
      <title>Re: For each ID keep last year and if it is missing keep the year before</title>
      <link>https://communities.sas.com/t5/SAS-Programming/For-each-ID-keep-last-year-and-if-it-is-missing-keep-the-year/m-p/852121#M336847</link>
      <description>&lt;P&gt;Assuming the data is already sorted by ID and YEAR then just use BY group process and WHERE filtering.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  by id year;
  where year in (2017 2018);
  if last.id;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 04 Jan 2023 14:22:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/For-each-ID-keep-last-year-and-if-it-is-missing-keep-the-year/m-p/852121#M336847</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-01-04T14:22:06Z</dc:date>
    </item>
    <item>
      <title>Re: For each ID keep last year and if it is missing keep the year before</title>
      <link>https://communities.sas.com/t5/SAS-Programming/For-each-ID-keep-last-year-and-if-it-is-missing-keep-the-year/m-p/852161#M336874</link>
      <description>&lt;P&gt;I start with 2180 observations where 139 are from 2018. With the code you provided, I end up with 5 observations. Don't know why.&lt;/P&gt;</description>
      <pubDate>Wed, 04 Jan 2023 17:33:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/For-each-ID-keep-last-year-and-if-it-is-missing-keep-the-year/m-p/852161#M336874</guid>
      <dc:creator>Satori</dc:creator>
      <dc:date>2023-01-04T17:33:44Z</dc:date>
    </item>
    <item>
      <title>Re: For each ID keep last year and if it is missing keep the year before</title>
      <link>https://communities.sas.com/t5/SAS-Programming/For-each-ID-keep-last-year-and-if-it-is-missing-keep-the-year/m-p/852162#M336875</link>
      <description>&lt;P&gt;Could you provide the code for that?&lt;/P&gt;</description>
      <pubDate>Wed, 04 Jan 2023 17:34:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/For-each-ID-keep-last-year-and-if-it-is-missing-keep-the-year/m-p/852162#M336875</guid>
      <dc:creator>Satori</dc:creator>
      <dc:date>2023-01-04T17:34:38Z</dc:date>
    </item>
    <item>
      <title>Re: For each ID keep last year and if it is missing keep the year before</title>
      <link>https://communities.sas.com/t5/SAS-Programming/For-each-ID-keep-last-year-and-if-it-is-missing-keep-the-year/m-p/852163#M336876</link>
      <description>&lt;P&gt;I determined the years: I want it only if it's for 2018, and if I there isn't for 2018, I want 2017, but not both.&lt;/P&gt;&lt;P&gt;the dataset has more a date variable which I converted to just year variable, and for each ID there are many years.&lt;/P&gt;</description>
      <pubDate>Wed, 04 Jan 2023 17:36:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/For-each-ID-keep-last-year-and-if-it-is-missing-keep-the-year/m-p/852163#M336876</guid>
      <dc:creator>Satori</dc:creator>
      <dc:date>2023-01-04T17:36:19Z</dc:date>
    </item>
    <item>
      <title>Re: For each ID keep last year and if it is missing keep the year before</title>
      <link>https://communities.sas.com/t5/SAS-Programming/For-each-ID-keep-last-year-and-if-it-is-missing-keep-the-year/m-p/852165#M336877</link>
      <description>&lt;P&gt;the data is already sorted by ID and descending year. Running this code got me an error.&lt;/P&gt;&lt;P&gt;ERROR: WHERE clause operator requires compatible variables.&lt;/P&gt;</description>
      <pubDate>Wed, 04 Jan 2023 17:38:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/For-each-ID-keep-last-year-and-if-it-is-missing-keep-the-year/m-p/852165#M336877</guid>
      <dc:creator>Satori</dc:creator>
      <dc:date>2023-01-04T17:38:55Z</dc:date>
    </item>
    <item>
      <title>Re: For each ID keep last year and if it is missing keep the year before</title>
      <link>https://communities.sas.com/t5/SAS-Programming/For-each-ID-keep-last-year-and-if-it-is-missing-keep-the-year/m-p/852166#M336878</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/173636"&gt;@Satori&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I start with 2180 observations where 139 are from 2018. With the code you provided, I end up with 5 observations. Don't know why.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;The data you showed does not indicate any problems when I run my code against it. Please provide a larger and more representative sample of your data. Please provide this sample of your data as &lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;working SAS data step code&lt;/STRONG&gt;&lt;/FONT&gt;, and not as screen capture or file attachment.&lt;/P&gt;</description>
      <pubDate>Wed, 04 Jan 2023 17:43:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/For-each-ID-keep-last-year-and-if-it-is-missing-keep-the-year/m-p/852166#M336878</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-01-04T17:43:41Z</dc:date>
    </item>
    <item>
      <title>Re: For each ID keep last year and if it is missing keep the year before</title>
      <link>https://communities.sas.com/t5/SAS-Programming/For-each-ID-keep-last-year-and-if-it-is-missing-keep-the-year/m-p/852174#M336881</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/173636"&gt;@Satori&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;the data is already sorted by ID and descending year. Running this code got me an error.&lt;/P&gt;
&lt;P&gt;ERROR: WHERE clause operator requires compatible variables.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Why would you have created the YEAR variable as a CHARACTER string?&lt;/P&gt;
&lt;P&gt;If the data is in DESCENDING order of YEAR then you want the FIRST one and not the LAST one.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  by id descending year;
  where year in ('2017' '2018');
  if first.id;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 04 Jan 2023 18:12:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/For-each-ID-keep-last-year-and-if-it-is-missing-keep-the-year/m-p/852174#M336881</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-01-04T18:12:38Z</dc:date>
    </item>
  </channel>
</rss>

