<?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: Matching main dataset to sub datasets based on date var in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Matching-main-dataset-to-sub-datasets-based-on-date-var/m-p/231052#M41990</link>
    <description>&lt;P&gt;It appears that the "date" for your sub-datasets is only available via data set name. You would need to derive the "date" from these names (what are the exact data set names?).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you can't derive exact dates from your data set names: What does the year part stand for? End of year, start of year, ...?&lt;/P&gt;
&lt;P&gt;That's relevant in order to match based on "closest" to an exact date in your main dataset.&lt;/P&gt;</description>
    <pubDate>Wed, 21 Oct 2015 21:07:50 GMT</pubDate>
    <dc:creator>Patrick</dc:creator>
    <dc:date>2015-10-21T21:07:50Z</dc:date>
    <item>
      <title>Matching main dataset to sub datasets based on date var</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Matching-main-dataset-to-sub-datasets-based-on-date-var/m-p/231046#M41988</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a dataset at the person level with an ID and a date variable, and 5 sub-datasets, each of which represents the patients' data in a single year.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I want is to join the dataset to the sub-datasets on ID, but I only want to pull in data from the sub-datasets in the sub-dataset representing the year closest to the year of the date variable, eg:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Main dataset:&lt;/P&gt;&lt;P&gt;ID &amp;nbsp; Date &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp;10/5/2010&lt;/P&gt;&lt;P&gt;2 &amp;nbsp; &amp;nbsp; 1/3/2012&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Sub dataset 2011:&lt;/P&gt;&lt;P&gt;ID &amp;nbsp; var1 &amp;nbsp; var2 &amp;nbsp; var3&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp;a &amp;nbsp; &amp;nbsp; &amp;nbsp; b &amp;nbsp; &amp;nbsp; &amp;nbsp; c&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Sub dataset 2012:&lt;/P&gt;&lt;P&gt;ID &amp;nbsp; var1 &amp;nbsp; var2 &amp;nbsp; var3&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;d&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;e&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; f&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So after the join, it should be like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID &amp;nbsp; &amp;nbsp; Date &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;closest_var1 &amp;nbsp; &amp;nbsp;closest_var2 &amp;nbsp; &amp;nbsp;closest_var3&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp;10/5/2010 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;SPAN&gt;a &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;b &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;c&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;2 &amp;nbsp; &amp;nbsp; 1/3/2012 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;SPAN&gt;d&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;e &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; f&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help is much appreciated.&lt;/P&gt;</description>
      <pubDate>Wed, 21 Oct 2015 20:26:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Matching-main-dataset-to-sub-datasets-based-on-date-var/m-p/231046#M41988</guid>
      <dc:creator>Walternate</dc:creator>
      <dc:date>2015-10-21T20:26:44Z</dc:date>
    </item>
    <item>
      <title>Re: Matching main dataset to sub datasets based on date var</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Matching-main-dataset-to-sub-datasets-based-on-date-var/m-p/231052#M41990</link>
      <description>&lt;P&gt;It appears that the "date" for your sub-datasets is only available via data set name. You would need to derive the "date" from these names (what are the exact data set names?).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you can't derive exact dates from your data set names: What does the year part stand for? End of year, start of year, ...?&lt;/P&gt;
&lt;P&gt;That's relevant in order to match based on "closest" to an exact date in your main dataset.&lt;/P&gt;</description>
      <pubDate>Wed, 21 Oct 2015 21:07:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Matching-main-dataset-to-sub-datasets-based-on-date-var/m-p/231052#M41990</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2015-10-21T21:07:50Z</dc:date>
    </item>
    <item>
      <title>Re: Matching main dataset to sub datasets based on date var</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Matching-main-dataset-to-sub-datasets-based-on-date-var/m-p/231135#M42017</link>
      <description>&lt;P&gt;Sub-dataset names:&lt;/P&gt;&lt;P&gt;addl_vars_2010&lt;/P&gt;&lt;P&gt;addl_vars_2011&lt;/P&gt;&lt;P&gt;addl_vars_2012&lt;/P&gt;&lt;P&gt;addl_vars_2013&lt;/P&gt;&lt;P&gt;addl_vars_2014&lt;/P&gt;&lt;P&gt;addl_vars_2015&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The exact date should be counted as the end of the year (ie, Dec 31st).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Thu, 22 Oct 2015 13:02:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Matching-main-dataset-to-sub-datasets-based-on-date-var/m-p/231135#M42017</guid>
      <dc:creator>Walternate</dc:creator>
      <dc:date>2015-10-22T13:02:23Z</dc:date>
    </item>
    <item>
      <title>Re: Matching main dataset to sub datasets based on date var</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Matching-main-dataset-to-sub-datasets-based-on-date-var/m-p/231195#M42034</link>
      <description>&lt;P&gt;Hi, I'm having trouble posting my answer. Another try:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Three ways to do this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
select m.date, s.* from main as m inner join sub2010 as s on m.id=s.id where year(m.date)=2010
union all
select m.date, s.* from main as m inner join sub2011 as s on m.id=s.id where year(m.date)=2011
union all
select m.date, s.* from main as m inner join sub2012 as s on m.id=s.id where year(m.date)=2012
union all
select m.date, s.* from main as m inner join sub2013 as s on m.id=s.id where year(m.date)=2013
union all
select m.date, s.* from main as m inner join sub2014 as s on m.id=s.id where year(m.date)=2014;
quit;

proc sql;
create table want as
select m.date, s.* from
	main as m inner join
	(	select 2010 as year, * from sub2010 union all
		select 2011 as year, * from sub2011 union all
		select 2012 as year, * from sub2012 union all
		select 2013 as year, * from sub2013 union all
		select 2014 as year, * from sub2014 ) as s
	on m.id=s.id and year(m.date) = s.year;
quit;

data s;
set sub: indsname=ds;
year = input(compress(ds,,"DK"), best.);
run;

proc sql;
create table want as
select m.date, s.* from main as m inner join s on m.id=s.id and year(m.date)=s.year;
drop table s;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;(untested)&lt;/P&gt;</description>
      <pubDate>Thu, 22 Oct 2015 15:59:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Matching-main-dataset-to-sub-datasets-based-on-date-var/m-p/231195#M42034</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2015-10-22T15:59:26Z</dc:date>
    </item>
    <item>
      <title>Re: Matching main dataset to sub datasets based on date var</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Matching-main-dataset-to-sub-datasets-based-on-date-var/m-p/231267#M42047</link>
      <description>&lt;P&gt;Below a first code drop. I'm not sure what "matching to closest data" actually means for you. May be you provide some more sample data to demonstrate how matching needs to work.&lt;/P&gt;
&lt;P&gt;Hope below will give you already some ideas of how to solve your problem.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data main;
  input ID $ Date:ddmmyy10.;
  format date date9.;
  datalines;
1 10/5/2010
2 1/3/2012
;
run;

data addl_vars_2010;
  input (ID var1 var2 var3) ($);
  datalines;
1 a b c
;
run;

data addl_vars_2012;
  input (ID var1 var2 var3) ($);
  datalines;
2 d e f
;
run;

data All_Sub;
  set addl_vars_: indsname=inds;
  format date date9.;
  date=mdy(12,31,input(scan(inds,-1,'_'),16.));
run;

proc sql;
  create table want as 
  select L.*, R.var1, R.var2, R.var3
  from main L left join all_sub R
    on L.id=R.id and year(L.date)=year(R.date)
  ;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 22 Oct 2015 21:09:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Matching-main-dataset-to-sub-datasets-based-on-date-var/m-p/231267#M42047</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2015-10-22T21:09:00Z</dc:date>
    </item>
  </channel>
</rss>

