<?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: Selecting the right record... in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Selecting-the-right-record/m-p/101510#M21258</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;So when the specified month falls between intervals they want the last interval before the specified month.&lt;/P&gt;&lt;P&gt;Try this simplified logic.&amp;nbsp; &lt;/P&gt;&lt;P&gt;I am assuming that your ID variables are GEO and H_POSTAL based on the sample data you posted.&lt;/P&gt;&lt;P&gt;I am also using : modifier on the &amp;lt;= operator so that the comparison will only be made up to shorter string. In this case the macro variable.&amp;nbsp; So a birth_dt of 10JAN2010 ('20100110') would be considered less than or equal to January 2010 ('201001').&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&lt;SPAN style="font-style: inherit; font-family: 'Courier New', monospace;"&gt;data sg&amp;amp;georef ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&lt;SPAN style="font-style: inherit; font-family: 'Courier New', monospace;"&gt;&amp;nbsp; set sgcme.sg&amp;amp;georef;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&lt;SPAN style="font-style: inherit; font-family: 'Courier New', monospace;"&gt;&amp;nbsp;&amp;nbsp; where birth_da &amp;lt;=: &amp;amp;refmonth ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&lt;SPAN style="font-style: inherit; font-family: 'Courier New', monospace;"&gt;&amp;nbsp;&amp;nbsp; by geo h_postal birth_da;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&lt;SPAN style="font-style: inherit; font-family: 'Courier New', monospace;"&gt;&amp;nbsp;&amp;nbsp; if last.birth_da;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&lt;SPAN style="font-style: inherit; font-family: 'Courier New', monospace;"&gt;run;&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 22 Aug 2012 14:02:26 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2012-08-22T14:02:26Z</dc:date>
    <item>
      <title>Selecting the right record...</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-the-right-record/m-p/101506#M21254</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P style="color: #2a2a2a; font-family: Calibri, sans-serif; font-size: small; background-color: #ffffff;"&gt;&lt;SPAN style="color: #000000; font-family: arial, helvetica, sans-serif; font-size: 12pt;"&gt;Hi guys,&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="color: #2a2a2a; font-family: Calibri, sans-serif; font-size: small; background-color: #ffffff;"&gt;&lt;SPAN style="color: #000000; font-family: arial, helvetica, sans-serif; font-size: 12pt;"&gt; &lt;/SPAN&gt;&lt;/P&gt;&lt;P style="color: #2a2a2a; font-family: Calibri, sans-serif; font-size: small; background-color: #ffffff;"&gt;&lt;SPAN style="color: #000000; font-family: arial, helvetica, sans-serif; font-size: 12pt;"&gt;I have a problem and I hope I can get a solution here &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="color: #2a2a2a; font-family: Calibri, sans-serif; font-size: small; background-color: #ffffff;"&gt;&lt;SPAN style="color: #000000; font-family: arial, helvetica, sans-serif; font-size: 12pt;"&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="color: #2a2a2a; font-family: Calibri, sans-serif; font-size: small; background-color: #ffffff;"&gt;&lt;SPAN style="color: #000000; font-family: arial, helvetica, sans-serif; font-size: 12pt;"&gt;I have to output a record base on a date.&amp;nbsp; The thing is that for the same h_postal, which is the key, I have 2 records.&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="color: #2a2a2a; font-family: Calibri, sans-serif; font-size: small; background-color: #ffffff;"&gt;&lt;SPAN style="color: #17365d; font-family: Arial, sans-serif;"&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="color: #2a2a2a; font-family: Calibri, sans-serif; font-size: small; background-color: #ffffff;"&gt;&lt;IMG alt="Capture.JPG" class="jive-image-thumbnail jive-image" height="81" src="https://communities.sas.com/legacyfs/online/2405_Capture.JPG" style="width: 811.8px; height: 81px;" width="812" /&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: arial, helvetica, sans-serif; font-size: 12pt;"&gt;It would be easy if I could say...&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P style="color: #2a2a2a; font-family: Calibri, sans-serif; font-size: small; background-color: #ffffff;"&gt;&lt;SPAN style="font-family: arial, helvetica, sans-serif; font-size: 12pt;"&gt; if ((&amp;amp;date &amp;gt;= birth_da) and (&amp;amp;date &amp;lt; ret_date)) then output;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="color: #2a2a2a; font-family: Calibri, sans-serif; font-size: small; background-color: #ffffff;"&gt;&lt;SPAN style="font-family: arial, helvetica, sans-serif; font-size: 12pt;"&gt; else delete;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="color: #2a2a2a; font-family: Calibri, sans-serif; font-size: small; background-color: #ffffff;"&gt;&lt;SPAN style="font-family: arial, helvetica, sans-serif; font-size: 12pt;"&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="color: #2a2a2a; font-family: Calibri, sans-serif; font-size: small; background-color: #ffffff;"&gt;&lt;SPAN style="font-family: arial, helvetica, sans-serif; font-size: 12pt;"&gt;But there is a "gap" between the dates.&amp;nbsp; So what appends if my date is "20020101"...&amp;nbsp; I loose records!&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="color: #2a2a2a; font-family: Calibri, sans-serif; font-size: small; background-color: #ffffff;"&gt;&lt;SPAN style="font-family: arial, helvetica, sans-serif; font-size: 12pt;"&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="color: #2a2a2a; font-family: Calibri, sans-serif; font-size: small; background-color: #ffffff;"&gt;&lt;SPAN style="font-family: arial, helvetica, sans-serif; font-size: 12pt;"&gt;What I would like to do is, example,&amp;nbsp; &lt;/SPAN&gt;&lt;/P&gt;&lt;P style="color: #2a2a2a; font-family: Calibri, sans-serif; font-size: small; background-color: #ffffff;"&gt;&lt;SPAN style="font-family: arial, helvetica, sans-serif; font-size: 12pt;"&gt;if date = "20040101" then output the second record.&amp;nbsp; &lt;/SPAN&gt;&lt;/P&gt;&lt;P style="color: #2a2a2a; font-family: Calibri, sans-serif; font-size: small; background-color: #ffffff;"&gt;&lt;SPAN style="font-size: 12pt; font-family: arial, helvetica, sans-serif;"&gt;if date = "20020101" then output the first record. (the one that falls into the gap)&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="color: #2a2a2a; font-family: Calibri, sans-serif; font-size: small; background-color: #ffffff;"&gt;&lt;SPAN style="font-size: 12pt; font-family: arial, helvetica, sans-serif;"&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="color: #2a2a2a; font-family: Calibri, sans-serif; font-size: small; background-color: #ffffff;"&gt;&lt;SPAN style="font-size: 12pt; font-family: arial, helvetica, sans-serif;"&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="color: #2a2a2a; font-family: Calibri, sans-serif; font-size: small; background-color: #ffffff;"&gt;&lt;SPAN style="font-size: 12pt; font-family: arial, helvetica, sans-serif;"&gt;I hope that is clear enough.&amp;nbsp; English is not my primary language &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="color: #2a2a2a; font-family: Calibri, sans-serif; font-size: small; background-color: #ffffff;"&gt;&lt;SPAN style="font-family: 'Courier New', monospace;"&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="color: #2a2a2a; font-family: Calibri, sans-serif; font-size: small; background-color: #ffffff;"&gt;&lt;SPAN style="font-family: 'Courier New', monospace;"&gt;Mylene&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 22 Aug 2012 01:00:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-the-right-record/m-p/101506#M21254</guid>
      <dc:creator>chalmyl</dc:creator>
      <dc:date>2012-08-22T01:00:12Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting the right record...</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-the-right-record/m-p/101507#M21255</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Partly it depends on what the dates mean.&lt;/P&gt;&lt;P&gt;If the dates in the database represent a interval of time and your are selecting records based on whether the date in the macro variable falls within the interval then you probably DO want to lose those records.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Also it looks like your date variables are not actual dates. 19000001 is not a valid date because you cannot have month number zero.&amp;nbsp; I suspect that '19000001' is actually an indication that the interval is open ended.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;So perhaps you just need to add that fact to your logic?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #2a2a2a; font-size: 16px; background-color: #ffffff;"&gt;if ((&amp;amp;date &amp;gt;= birth_da) and (&amp;amp;date &amp;lt; ret_date or ret_date = '19000001')) then output;&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 22 Aug 2012 01:32:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-the-right-record/m-p/101507#M21255</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2012-08-22T01:32:30Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting the right record...</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-the-right-record/m-p/101508#M21256</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Assumptions:&lt;/P&gt;&lt;P&gt;(1) no two records for the same key have overlapping dates (i.e. if birth_da for record 1 &amp;lt; birth_da for record 2 on the same key, then ret_date for record 1 &amp;lt; birth_da for record 2)&lt;/P&gt;&lt;P&gt;(2) birth_da is nonmissing&lt;/P&gt;&lt;P&gt;(3) missing value for ret_date indicates not yet retired (so can only occur in the last record)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;So for each key, you want to return the latest birth_da that is not greater than the date?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I think the easiest way to do this in simple SAS is to reverse-sort and use LAG to find the next date:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%let date='15Jul2001'd;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data have;&lt;/P&gt;&lt;P&gt;input recordid h_postal $ birth_da ret_date;&lt;/P&gt;&lt;P&gt;informat birth_da ret_date ANYDTDTE20.;&lt;/P&gt;&lt;P&gt;format birth_da ret_date DATE9.;&lt;/P&gt;&lt;P&gt;cards;&lt;/P&gt;&lt;P&gt;1 A 01Jan2000 01Jun2001&lt;/P&gt;&lt;P&gt;2 A 01Jan2002 12Feb2003&lt;/P&gt;&lt;P&gt;3 B 01Jan2000 01Jun2001&lt;/P&gt;&lt;P&gt;4 B 01Jul2001 12Feb2003&lt;/P&gt;&lt;P&gt;5 C 01Jan1999 31Dec1999&lt;/P&gt;&lt;P&gt;6 C 01Jan2001 01Feb2001&lt;/P&gt;&lt;P&gt;7 C 01Jan2003 12feb2003&lt;/P&gt;&lt;P&gt;8 D 01Jan2005 01Jan2007&lt;/P&gt;&lt;P&gt;9 D 01Jan2009 .&lt;/P&gt;&lt;P&gt;10 E 01Jan2001 .&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc sort data=have out=have_reverse;&lt;/P&gt;&lt;P&gt;by h_postal descending birth_da;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data want;&lt;/P&gt;&lt;P&gt;set have_reverse;&lt;/P&gt;&lt;P&gt;by h_postal;&lt;/P&gt;&lt;P&gt;next_birth_date=lag(birth_da);&lt;/P&gt;&lt;P&gt;if first.h_postal then next_birth_date=.;&lt;/P&gt;&lt;P&gt;/* because we have sorted in reverse date order, first.h_postal indicates that this is the most recent entry for this unit&lt;/P&gt;&lt;P&gt;(highest value of birth_da) */&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;if (next_birth_date&amp;lt;=ret_date OR missing(ret_date)) AND not missing(next_birth_date) then put "ERROR: dates overlap";&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;if (birth_da&amp;lt;=&amp;amp;date) AND ((next_birth_date&amp;gt;&amp;amp;date) OR missing(next_birth_date)) then output;&lt;/P&gt;&lt;P&gt;drop next_birth_date;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Edit: before using this you'll need to convert to SAS date variables (which you should be using anyway for this sort of work) and as per Tom's comment, if a date of "190000001" indicates "not yet retired" this should probably be interpreted as missing.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Message was edited by: Geoffrey Brent&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 22 Aug 2012 07:22:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-the-right-record/m-p/101508#M21256</guid>
      <dc:creator>GeoffreyBrent</dc:creator>
      <dc:date>2012-08-22T07:22:36Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting the right record...</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-the-right-record/m-p/101509#M21257</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I am so sorry guys!!&amp;nbsp; &lt;SPAN style="font-family: Arial, sans-serif; color: #1f497d;"&gt;I knew it was not describe properly...&amp;nbsp; let’s try again &lt;SPAN style="font-family: Wingdings; font-size: 10pt;"&gt;J&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #1f497d;"&gt; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: Arial, sans-serif; color: #1f497d;"&gt;I also just realized that I had to change the value of “infinity” for this to work.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #1f497d;"&gt; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: Arial, sans-serif; color: #1f497d;"&gt;Let’s look at my real code.&amp;nbsp; “date” is a cycle entered to process data.&amp;nbsp; In my program, it is reference to as “refmonth”, which is a char variable, length 6 (ex.:201108). &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #1f497d;"&gt; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="font-family: Courier New, monospace;"&gt;data sg&amp;amp;georef (drop= birth retired);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: Courier New, monospace;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; length birth $&lt;SPAN style="color: #008080;"&gt;&lt;STRONG&gt;6&lt;/STRONG&gt;&lt;/SPAN&gt; retired $&lt;SPAN style="color: #008080;"&gt;&lt;STRONG&gt;6&lt;/STRONG&gt;&lt;/SPAN&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: Courier New, monospace;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; set sgcme.sg&amp;amp;georef;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: Courier New, monospace;"&gt; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: Courier New, monospace;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; birth = substr(birth_da,&lt;SPAN style="color: #008080;"&gt;&lt;STRONG&gt;1&lt;/STRONG&gt;&lt;/SPAN&gt;,&lt;SPAN style="color: #008080;"&gt;&lt;STRONG&gt;6&lt;/STRONG&gt;&lt;/SPAN&gt;);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: Courier New, monospace;"&gt; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: Courier New, monospace;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if ret_date = &lt;SPAN style="color: #800080;"&gt;"19000001"&lt;/SPAN&gt; then retired = &lt;SPAN style="color: #800080;"&gt;"999999"&lt;/SPAN&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: Courier New, monospace;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else retired = substr(ret_date,&lt;SPAN style="color: #008080;"&gt;&lt;STRONG&gt;1&lt;/STRONG&gt;&lt;/SPAN&gt;,&lt;SPAN style="color: #008080;"&gt;&lt;STRONG&gt;6&lt;/STRONG&gt;&lt;/SPAN&gt;);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: Courier New, monospace;"&gt; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: Courier New, monospace;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if ((&amp;amp;refmonth &amp;gt;= birth) and (&amp;amp;refmonth &amp;lt; retired)) then output; else delete;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="font-family: Courier New, monospace;"&gt;run;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #1f497d;"&gt; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: Arial, sans-serif; color: #1f497d;"&gt;Based on my client’s specifications, if we are processing refmonth = 200201 with a postal &lt;SPAN style="color: #17365d;"&gt;code = &lt;/SPAN&gt;&lt;SPAN style="color: #17365d;"&gt;“A0A2V0&lt;/SPAN&gt;&lt;SPAN style="color: #17365d;"&gt;”&lt;/SPAN&gt;&lt;SPAN style="color: #17365d;"&gt;,&lt;/SPAN&gt;&lt;SPAN style="color: #17365d;"&gt; he wants me to output the values &lt;/SPAN&gt;&lt;SPAN style="color: #17365d;"&gt;of the first record.&amp;nbsp; With the piece of code above, I lose those records. &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: Arial, sans-serif; color: #17365d;"&gt; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: Arial, sans-serif; color: #1f497d;"&gt; &lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 22 Aug 2012 13:07:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-the-right-record/m-p/101509#M21257</guid>
      <dc:creator>chalmyl</dc:creator>
      <dc:date>2012-08-22T13:07:30Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting the right record...</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-the-right-record/m-p/101510#M21258</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;So when the specified month falls between intervals they want the last interval before the specified month.&lt;/P&gt;&lt;P&gt;Try this simplified logic.&amp;nbsp; &lt;/P&gt;&lt;P&gt;I am assuming that your ID variables are GEO and H_POSTAL based on the sample data you posted.&lt;/P&gt;&lt;P&gt;I am also using : modifier on the &amp;lt;= operator so that the comparison will only be made up to shorter string. In this case the macro variable.&amp;nbsp; So a birth_dt of 10JAN2010 ('20100110') would be considered less than or equal to January 2010 ('201001').&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&lt;SPAN style="font-style: inherit; font-family: 'Courier New', monospace;"&gt;data sg&amp;amp;georef ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&lt;SPAN style="font-style: inherit; font-family: 'Courier New', monospace;"&gt;&amp;nbsp; set sgcme.sg&amp;amp;georef;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&lt;SPAN style="font-style: inherit; font-family: 'Courier New', monospace;"&gt;&amp;nbsp;&amp;nbsp; where birth_da &amp;lt;=: &amp;amp;refmonth ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&lt;SPAN style="font-style: inherit; font-family: 'Courier New', monospace;"&gt;&amp;nbsp;&amp;nbsp; by geo h_postal birth_da;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&lt;SPAN style="font-style: inherit; font-family: 'Courier New', monospace;"&gt;&amp;nbsp;&amp;nbsp; if last.birth_da;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&lt;SPAN style="font-style: inherit; font-family: 'Courier New', monospace;"&gt;run;&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 22 Aug 2012 14:02:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-the-right-record/m-p/101510#M21258</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2012-08-22T14:02:26Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting the right record...</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-the-right-record/m-p/101511#M21259</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Still not working!!&amp;nbsp; If my refmonth = 200401, it has to pick up the second record!!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Please also keep in mind that the "birth" and "retired" variables are character, so are "refmonth",&amp;nbsp; "birth_da" and "ret_date"...&amp;nbsp; and there is nothing I can do about it!! &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 22 Aug 2012 17:39:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-the-right-record/m-p/101511#M21259</guid>
      <dc:creator>chalmyl</dc:creator>
      <dc:date>2012-08-22T17:39:57Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting the right record...</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-the-right-record/m-p/101512#M21260</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;That's because it should using LAST.H_POSTAL instead of last.birth_da.&lt;/P&gt;&lt;P&gt;Here is a test.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;data have;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp; input (geo h_postal birth_da ret_date) ($);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;cards;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;1 1 19830401 20010601&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;1 1 20030501 19000001&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;run;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;%let refmonth=200201;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;data want ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp; set have;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp; where birth_da &amp;lt;=: "&amp;amp;refmonth" ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp; by geo h_postal birth_da;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp; if last.h_postal;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp; put (_all_) (=);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;run;&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 22 Aug 2012 19:38:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-the-right-record/m-p/101512#M21260</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2012-08-22T19:38:39Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting the right record...</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-the-right-record/m-p/101513#M21261</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You didn't post enough sampe data which make your explanation vague .&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;data have;
input recordid h_postal $ birth_da ret_date;
informat birth_da ret_date ANYDTDTE20.;
format birth_da ret_date DATE9.;
cards;
1 1 19830401 20010601
1 1 20030501 19000001
2 A 01Jan2002 12Feb2003
3 B 01Jan2000 01Jun2001
;
run;

 

%let refmonth='01jan2004'd ;

data want(drop=_: flag);
 merge have have(firstobs=2 keep=recordid birth_da rename=(recordid=_recordid birth_da=_birth_da)) ;
 retain flag;
 if&amp;nbsp; recordid ne lag(recordid) then flag=0;
 if&amp;nbsp; (recordid eq _recordid) and (&amp;amp;refmonth lt _birth_da) then do;flag=1;output;end;
&amp;nbsp; else if not flag and (recordid ne _recordid) then output;
run;




&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Ksharp&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 23 Aug 2012 03:14:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-the-right-record/m-p/101513#M21261</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2012-08-23T03:14:56Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting the right record...</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-the-right-record/m-p/101514#M21262</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You didn't post enough sampe data which make your explanation vague .&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;data have;
input recordid h_postal $ birth_da ret_date;
informat birth_da ret_date ANYDTDTE20.;
format birth_da ret_date DATE9.;
cards;
1 1 19830401 20010601
1 1 20030501 19000001
2 A 01Jan2002 12Feb2003
3 B 01Jan2000 01Jun2001
;
run;

 

%let refmonth='01jan2004'd ;

data want(drop=_: flag);
 merge have have(firstobs=2 keep=recordid birth_da rename=(recordid=_recordid birth_da=_birth_da)) ;
 retain flag;
 if&amp;nbsp; recordid ne lag(recordid) then flag=0;
 if&amp;nbsp; (recordid eq _recordid) and (&amp;amp;refmonth lt _birth_da) then do;flag=1;output;end;
&amp;nbsp; else if not flag and (recordid ne _recordid) then output;
run;




&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Ksharp&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 23 Aug 2012 03:16:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-the-right-record/m-p/101514#M21262</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2012-08-23T03:16:04Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting the right record...</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-the-right-record/m-p/101515#M21263</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Does this make sense?&amp;nbsp; If you get to the final observation for GEO/H_POSTAL, and nothing has been output yet, then output that last one regardless of the dates.&amp;nbsp; That would be relatively easy to code:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data selected;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; set have;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; by geo h_postal;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; if first.h_postal then flag=0;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; length birth retired $ 6;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; birth = birth_da;&amp;nbsp; /* substr is unnecessary when length is already set */&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; if ret_date='19000001' then retired='999999';&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; else retired = ret_date;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; if (birth &amp;lt;= &amp;amp;refmonth &amp;lt; retired) then do;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; output;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; flag + 1;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; end;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; if last.h_postal and flag=0 then output;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Setting the rules is most of the battle here. &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 23 Aug 2012 12:58:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-the-right-record/m-p/101515#M21263</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2012-08-23T12:58:07Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting the right record...</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-the-right-record/m-p/101516#M21264</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Tom....&amp;nbsp; you are a genius!!!&amp;nbsp; Thanks a lot!! &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;And sorry for the other answers I got, that I haven't tried.&amp;nbsp; Maybe there were good answers, but this one is working.&amp;nbsp; Thanks to all of you anyway &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 23 Aug 2012 13:08:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-the-right-record/m-p/101516#M21264</guid>
      <dc:creator>chalmyl</dc:creator>
      <dc:date>2012-08-23T13:08:00Z</dc:date>
    </item>
  </channel>
</rss>

