<?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: Use Date Field to find last N occurances before the date field in another dataset in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Use-Date-Field-to-find-last-N-occurances-before-the-date-field/m-p/542981#M150077</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Data1;
	Input ID MonthEnd yymmn6.;
	Format MonthEnd yymmn6.;
	datalines;
	1 200801
	1 200802
	1 200803
	2 201501
	2 201502
	2 201503
	;
run;

data Data2;
	Input ID Process_Date Date9. Grade;
	Format Process_Date Date9.;
	datalines;
	1 01Aug2007 1
	1 01Sep2007 2
	1 01Oct2007 3
	1 01Nov2007 4 
	2 13Dec2014 1
	2 11Jan2015 2
	2 18Jan2015 3
	2 03Feb2015 4
	;
run;

proc sql; 
	create table data3 as 
	select * 
	from 
	data1 
	left join 
	data2 
	on data1.id=data2.id
	and process_date &amp;lt; monthend
	order by id , monthend , process_date desc
	;
quit;

data data4(where=(cnt le 3));
	set data3;
	by id monthend descending process_date;
	retain cnt;
	if first.monthend then cnt=1;
	else cnt+1 ; 
run;

proc transpose data=data4 out=want (drop=_name_); 
	by id monthend ;
	var grade;
	id cnt;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 13 Mar 2019 23:59:04 GMT</pubDate>
    <dc:creator>34reqrwe</dc:creator>
    <dc:date>2019-03-13T23:59:04Z</dc:date>
    <item>
      <title>Use Date Field to find last N occurances before the date field in another dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-Date-Field-to-find-last-N-occurances-before-the-date-field/m-p/542964#M150072</link>
      <description>&lt;P&gt;Hi SAS Users,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here are two sample datasets:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Data1;
Input ID MonthEnd :MMYYN.;
Format MonthEnd :MMYYN.;
datalines;
1 200801
1 200802
1 200803
2 201501
2 201502
2 201503
;
data Data2;
Input ID Process_Date :Date9. Grade;
Format Process_Date :Date9.;
datalines;
1 01Aug2007 1
1 01Sep2007 2
1 01Oct2007 3
1 01Nov2007 4 
2 13Dec2014 1
2 11Jan2015 2
2 18Jan2015 3
2 03Feb2015 4
;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;What I want to do is select the 3 most rescent occurances of the Grade from Data2 that happen before the MonthEnd from Data1 and transpose them into Data1. My idea of how I would like my final data to look like would be&amp;nbsp;&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;P&gt;ID&amp;nbsp; MonthEnd Previous_Grade1 Previous_Grade2 Previous_Grade3&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; 200801&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 4&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; &amp;nbsp; 3&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;2&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; 200802&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 4&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; &amp;nbsp; 3&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;2&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; 200803&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 4&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; &amp;nbsp; 3&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;2&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; 201501&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&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; &amp;nbsp; &amp;nbsp;&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; 201502&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 3&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; &amp;nbsp; 2&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;1&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; 201503&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 4&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; &amp;nbsp; 3&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;2&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The reason why all the ID=1 get assigned the same grades is because all the Process_Dates from Data2 (ID1) are less than all the MonthEnd dates so it takes the three most recent grades.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For ID=2 that has a MonthEnd=201501, there is only one ProcessDate that is before that month.&lt;/P&gt;&lt;P&gt;For ID=2 that has a MonthEnd=201502 and MonthEnd=201503, it takes the 3 most recent that are before that specific MonthEnd.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am having a hard time explaining what I need so please feel free to ask any questions if you need clarification.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Tom&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 13 Mar 2019 22:48:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-Date-Field-to-find-last-N-occurances-before-the-date-field/m-p/542964#M150072</guid>
      <dc:creator>Tommy1</dc:creator>
      <dc:date>2019-03-13T22:48:10Z</dc:date>
    </item>
    <item>
      <title>Re: Use Date Field to find last N occurances before the date field in another dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-Date-Field-to-find-last-N-occurances-before-the-date-field/m-p/542981#M150077</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Data1;
	Input ID MonthEnd yymmn6.;
	Format MonthEnd yymmn6.;
	datalines;
	1 200801
	1 200802
	1 200803
	2 201501
	2 201502
	2 201503
	;
run;

data Data2;
	Input ID Process_Date Date9. Grade;
	Format Process_Date Date9.;
	datalines;
	1 01Aug2007 1
	1 01Sep2007 2
	1 01Oct2007 3
	1 01Nov2007 4 
	2 13Dec2014 1
	2 11Jan2015 2
	2 18Jan2015 3
	2 03Feb2015 4
	;
run;

proc sql; 
	create table data3 as 
	select * 
	from 
	data1 
	left join 
	data2 
	on data1.id=data2.id
	and process_date &amp;lt; monthend
	order by id , monthend , process_date desc
	;
quit;

data data4(where=(cnt le 3));
	set data3;
	by id monthend descending process_date;
	retain cnt;
	if first.monthend then cnt=1;
	else cnt+1 ; 
run;

proc transpose data=data4 out=want (drop=_name_); 
	by id monthend ;
	var grade;
	id cnt;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 13 Mar 2019 23:59:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-Date-Field-to-find-last-N-occurances-before-the-date-field/m-p/542981#M150077</guid>
      <dc:creator>34reqrwe</dc:creator>
      <dc:date>2019-03-13T23:59:04Z</dc:date>
    </item>
    <item>
      <title>Re: Use Date Field to find last N occurances before the date field in another dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-Date-Field-to-find-last-N-occurances-before-the-date-field/m-p/543232#M150151</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/209480"&gt;@34reqrwe&lt;/a&gt;&amp;nbsp; That was such an elegant solution. Thanks for all your help!&lt;/P&gt;</description>
      <pubDate>Thu, 14 Mar 2019 16:05:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-Date-Field-to-find-last-N-occurances-before-the-date-field/m-p/543232#M150151</guid>
      <dc:creator>Tommy1</dc:creator>
      <dc:date>2019-03-14T16:05:30Z</dc:date>
    </item>
  </channel>
</rss>

