<?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: Choosing obs by monyr in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Choosing-obs-by-monyr/m-p/723187#M224374</link>
    <description>&lt;P&gt;The problem is MONYY is a SAS date value (number of days from 01/01/1960) so each date is the last occurrance of that date.&lt;BR /&gt;What you need to do is change the MONYY to a character value and format it:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data date;
 infile datalines delimiter = ',';
 input ID $ DATE:mmddyy10. ;
 format  date mmddyy10.;
 datalines;
1,  05/03/2017
1,  05/02/2017
1,  04/26/2017
1,  04/18/2017
1,  04/10/2017
;
run;

/* Change this datastep */
data date; 
	set date; 
		MONYR=putn(DATE,"monyy7.");
	
run; 

proc sort data=date;
by id monyr date;
run;

data want_date;
set date;
by id monyr date;
if last.monyr;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 03 Mar 2021 15:58:19 GMT</pubDate>
    <dc:creator>AMSAS</dc:creator>
    <dc:date>2021-03-03T15:58:19Z</dc:date>
    <item>
      <title>Choosing obs by monyr</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Choosing-obs-by-monyr/m-p/723177#M224368</link>
      <description>&lt;P&gt;Hello all, I have the following data and code below. What I would like to do is choose the last monyr by id. Example the id listed below I would like to choose 04/26/2017 and 05/03/2017. I tried using the code below but no obs are chosen.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data date;&lt;BR /&gt; infile datalines delimiter = ',';&lt;BR /&gt; input ID $ DATE:mmddyy10. ;&lt;BR /&gt; format  date mmddyy10.;&lt;BR /&gt; datalines;&lt;BR /&gt;1,  05/03/2017&lt;BR /&gt;1,  05/02/2017&lt;BR /&gt;1,  04/26/2017&lt;BR /&gt;1,  04/18/2017&lt;BR /&gt;1,  04/10/2017&lt;BR /&gt;;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;data date; &lt;BR /&gt;	set date; &lt;BR /&gt;		MONYR=DATE;&lt;BR /&gt;	format monyr monyy7.;&lt;BR /&gt;run; &lt;BR /&gt;&lt;BR /&gt;proc sort data=date;&lt;BR /&gt;by id monyr date;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;data want_date;&lt;BR /&gt;set date;&lt;BR /&gt;by id monyr date;&lt;BR /&gt;if last.monyr;&lt;BR /&gt;run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 03 Mar 2021 15:37:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Choosing-obs-by-monyr/m-p/723177#M224368</guid>
      <dc:creator>luvscandy27</dc:creator>
      <dc:date>2021-03-03T15:37:59Z</dc:date>
    </item>
    <item>
      <title>Re: Choosing obs by monyr</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Choosing-obs-by-monyr/m-p/723182#M224372</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data date;
    set date;
    month=intnx('month',date,0,'b');
run;
proc sort data=date;
    by id month date;
run;
data want_date;
    set date;
    by id month;
    if last.month;
    drop month;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Your variable MONYR serves no purpose, it has the exact same value as the variable DATE, and so adds nothing to the solution..Formatting a variable only changes its appearance, it does not change the underlying value which is used for sorting and all arithmetic.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Instead, you want a variable (which I have named MONTH), which is a valid SAS date value, hence the use of the INTNX function, which here finds the first day of each month, and essentially gives you a month/year that can be sorted properly.&lt;/P&gt;</description>
      <pubDate>Wed, 03 Mar 2021 15:57:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Choosing-obs-by-monyr/m-p/723182#M224372</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-03-03T15:57:16Z</dc:date>
    </item>
    <item>
      <title>Re: Choosing obs by monyr</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Choosing-obs-by-monyr/m-p/723186#M224373</link>
      <description>&lt;P&gt;Got it. Thank you!&lt;/P&gt;</description>
      <pubDate>Wed, 03 Mar 2021 15:58:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Choosing-obs-by-monyr/m-p/723186#M224373</guid>
      <dc:creator>luvscandy27</dc:creator>
      <dc:date>2021-03-03T15:58:06Z</dc:date>
    </item>
    <item>
      <title>Re: Choosing obs by monyr</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Choosing-obs-by-monyr/m-p/723187#M224374</link>
      <description>&lt;P&gt;The problem is MONYY is a SAS date value (number of days from 01/01/1960) so each date is the last occurrance of that date.&lt;BR /&gt;What you need to do is change the MONYY to a character value and format it:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data date;
 infile datalines delimiter = ',';
 input ID $ DATE:mmddyy10. ;
 format  date mmddyy10.;
 datalines;
1,  05/03/2017
1,  05/02/2017
1,  04/26/2017
1,  04/18/2017
1,  04/10/2017
;
run;

/* Change this datastep */
data date; 
	set date; 
		MONYR=putn(DATE,"monyy7.");
	
run; 

proc sort data=date;
by id monyr date;
run;

data want_date;
set date;
by id monyr date;
if last.monyr;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 03 Mar 2021 15:58:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Choosing-obs-by-monyr/m-p/723187#M224374</guid>
      <dc:creator>AMSAS</dc:creator>
      <dc:date>2021-03-03T15:58:19Z</dc:date>
    </item>
    <item>
      <title>Re: Choosing obs by monyr</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Choosing-obs-by-monyr/m-p/723189#M224375</link>
      <description>&lt;P&gt;Character formatted dates as MONYY. do not sort properly. There's really no reason to use character strings as dates here.&lt;/P&gt;</description>
      <pubDate>Wed, 03 Mar 2021 16:02:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Choosing-obs-by-monyr/m-p/723189#M224375</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-03-03T16:02:36Z</dc:date>
    </item>
  </channel>
</rss>

