<?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: find data point from one table based on date, and pull to another table in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/find-data-point-from-one-table-based-on-date-and-pull-to-another/m-p/816902#M34388</link>
    <description>&lt;P&gt;A simple way is to interleave the observations and retain the WEIGHT value in a new variable.&lt;/P&gt;
&lt;P&gt;First let's make some more complex test data.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data file1;
  input id date :datetime. weight;
  format date datetime19.;
datalines;
1  07FEB2020:08:10:08 68.9
1  07FEB2020:09:10:08 68.2
1  10FEB2020:10:10:08 68.4
1  13FEB2020:11:10:08 68.7
1  14FEB2020:12:10:08 68.1
1  16FEB2020:13:10:08 67.4
1  18FEB2020:14:10:08 67.9
2  21FEB2020:15:10:08 80.2
2  07MAR2020:16:10:08 81.2
;

data file2; 
  input id date :datetime. ;
  format date datetime19.;
datalines; 
1  07FEB2020:10:40:08
1  10FEB2020:16:45:08
1  14FEB2020:12:20:08
2  20FEB2020:11:54:08
2  02MAR2020:12:58:08
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So now just use a BY statement with SET to interleave the records.&amp;nbsp; Use the IN= dataset option to figure out which observations are from FILE2.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set file1 file2(in=in2);
  by id date;
  if first.id then last_wt=.;
  last_wt = coalesce(weight, last_wt);
  retain last_wt;
  if in2;
  drop weight;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results&lt;/P&gt;
&lt;PRE&gt;Obs    id                   date    last_wt

 1      1     07FEB2020:10:40:08      68.2
 2      1     10FEB2020:16:45:08      68.4
 3      1     14FEB2020:12:20:08      68.1
 4      2     20FEB2020:11:54:08        .
 5      2     02MAR2020:12:58:08      80.2

&lt;/PRE&gt;</description>
    <pubDate>Tue, 07 Jun 2022 17:44:26 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2022-06-07T17:44:26Z</dc:date>
    <item>
      <title>find data point from one table based on date, and pull to another table</title>
      <link>https://communities.sas.com/t5/New-SAS-User/find-data-point-from-one-table-based-on-date-and-pull-to-another/m-p/816883#M34381</link>
      <description>&lt;P&gt;Hi SAS forum,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have two data tables that look like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data file1;
input date $ weight;
datalines;
07FEB20:08:10:08 68.9
07FEB20:09:10:08 68.2
07FEB20:10:10:08 68.4
07FEB20:11:10:08 68.7
07FEB20:12:10:08 68.1
07FEB20:13:10:08 67.4
07FEB20:14:10:08 67.9
07FEB20:15:10:08 80.2
07FEB20:16:10:08 81.2;
run;
&lt;BR /&gt;data file2; &lt;BR /&gt;input date $; &lt;BR /&gt;datalines; &lt;BR /&gt;07FEB20:10:40:08&lt;BR /&gt;10FEB20:16:45:08&lt;BR /&gt;14FEB20:12:20:08&lt;BR /&gt;20FEB20:11:54:08&lt;BR /&gt;02JAN20:12:58:08;&lt;BR /&gt;run; &lt;/PRE&gt;&lt;P&gt;As you can see, file1 has hourly values of weight, whereas file2 just has particular date-times. What I want to do is, for each row (date-time) in file2, look up the first date-time in file1&lt;FONT face="inherit"&gt;&amp;nbsp;that &lt;/FONT&gt;precedes&lt;FONT face="inherit"&gt;&amp;nbsp;this date-time, and pull this date-time and the associated weight into file2. So, for this example I would want file2 to be modified to be file2_new where the first row of data would read as:&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;CODE class=""&gt;&lt;/CODE&gt;&lt;/P&gt;&lt;PRE&gt;data file2_new; 
input date $ date_pulled $ weight_pulled; 
datalines; 
07FEB20:10:40:08 07FEB20:10:10:08 68.4
run; &lt;/PRE&gt;&lt;P&gt;&lt;CODE class=""&gt;&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;Any tips on how to do this would be really appreciated!&lt;BR /&gt;&lt;BR /&gt;Thanks&lt;BR /&gt;linlin87&lt;/P&gt;</description>
      <pubDate>Tue, 07 Jun 2022 16:02:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/find-data-point-from-one-table-based-on-date-and-pull-to-another/m-p/816883#M34381</guid>
      <dc:creator>linlin87</dc:creator>
      <dc:date>2022-06-07T16:02:45Z</dc:date>
    </item>
    <item>
      <title>Re: find data point from one table based on date, and pull to another table</title>
      <link>https://communities.sas.com/t5/New-SAS-User/find-data-point-from-one-table-based-on-date-and-pull-to-another/m-p/816896#M34385</link>
      <description>&lt;P&gt;First step:&lt;/P&gt;
&lt;P&gt;Do not use character values for datetimes. With character values such as you show 14Feb would come before 20Jan.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Second: a warning bad things can happen to folks that habitually use 2-digit years.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Third: define in a bit more detail what "look up the first date-time in file1&lt;FONT face="inherit"&gt;&amp;nbsp;that &lt;/FONT&gt;precedes&lt;FONT face="inherit"&gt;&amp;nbsp;this date-time" means. It would appear to me that except for the Jan date that 07FEB20:08:10:08 is the "first date that precedes" the values in file_2. There is something missing about your definition of "first". You really should supply more than one result example. That we can check whether any suggestion is correct. &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="inherit"&gt;Fourth: make sure that the semicolon that ends datalines is on it's own line.&lt;/FONT&gt;&lt;/P&gt;
&lt;PRE&gt;data file1;
input date :datetime. weight;
format date datetime19.;
datalines;
07FEB20:08:10:08 68.9
07FEB20:09:10:08 68.2
07FEB20:10:10:08 68.4
07FEB20:11:10:08 68.7
07FEB20:12:10:08 68.1
07FEB20:13:10:08 67.4
07FEB20:14:10:08 67.9
07FEB20:15:10:08 80.2
07FEB20:16:10:08 81.2
;
run;

data file2; 
input date :datetime. ;
format date datetime19.;
datalines; 
07FEB20:10:40:08
10FEB20:16:45:08
14FEB20:12:20:08
20FEB20:11:54:08
02JAN20:12:58:08
;
run;  &lt;/PRE&gt;
&lt;P&gt;&lt;FONT face="inherit"&gt;If your values are character you will need to convert them to numeric values similar to above so any comparisons resolve correctly.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Jun 2022 16:55:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/find-data-point-from-one-table-based-on-date-and-pull-to-another/m-p/816896#M34385</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-06-07T16:55:13Z</dc:date>
    </item>
    <item>
      <title>Re: find data point from one table based on date, and pull to another table</title>
      <link>https://communities.sas.com/t5/New-SAS-User/find-data-point-from-one-table-based-on-date-and-pull-to-another/m-p/816902#M34388</link>
      <description>&lt;P&gt;A simple way is to interleave the observations and retain the WEIGHT value in a new variable.&lt;/P&gt;
&lt;P&gt;First let's make some more complex test data.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data file1;
  input id date :datetime. weight;
  format date datetime19.;
datalines;
1  07FEB2020:08:10:08 68.9
1  07FEB2020:09:10:08 68.2
1  10FEB2020:10:10:08 68.4
1  13FEB2020:11:10:08 68.7
1  14FEB2020:12:10:08 68.1
1  16FEB2020:13:10:08 67.4
1  18FEB2020:14:10:08 67.9
2  21FEB2020:15:10:08 80.2
2  07MAR2020:16:10:08 81.2
;

data file2; 
  input id date :datetime. ;
  format date datetime19.;
datalines; 
1  07FEB2020:10:40:08
1  10FEB2020:16:45:08
1  14FEB2020:12:20:08
2  20FEB2020:11:54:08
2  02MAR2020:12:58:08
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So now just use a BY statement with SET to interleave the records.&amp;nbsp; Use the IN= dataset option to figure out which observations are from FILE2.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set file1 file2(in=in2);
  by id date;
  if first.id then last_wt=.;
  last_wt = coalesce(weight, last_wt);
  retain last_wt;
  if in2;
  drop weight;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results&lt;/P&gt;
&lt;PRE&gt;Obs    id                   date    last_wt

 1      1     07FEB2020:10:40:08      68.2
 2      1     10FEB2020:16:45:08      68.4
 3      1     14FEB2020:12:20:08      68.1
 4      2     20FEB2020:11:54:08        .
 5      2     02MAR2020:12:58:08      80.2

&lt;/PRE&gt;</description>
      <pubDate>Tue, 07 Jun 2022 17:44:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/find-data-point-from-one-table-based-on-date-and-pull-to-another/m-p/816902#M34388</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-06-07T17:44:26Z</dc:date>
    </item>
    <item>
      <title>Re: find data point from one table based on date, and pull to another table</title>
      <link>https://communities.sas.com/t5/New-SAS-User/find-data-point-from-one-table-based-on-date-and-pull-to-another/m-p/816939#M34399</link>
      <description>&lt;P&gt;Thank you Tom, that is brilliant. It certainly looks to be doing what I was hoping. But why have you introduced this id variable? Because that is not how my data is structured. I do not have this ID variable in my dataset, and I am not sure how this variable you have generated relates to the original data structures.&lt;/P&gt;</description>
      <pubDate>Tue, 07 Jun 2022 19:21:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/find-data-point-from-one-table-based-on-date-and-pull-to-another/m-p/816939#M34399</guid>
      <dc:creator>u60655191</dc:creator>
      <dc:date>2022-06-07T19:21:02Z</dc:date>
    </item>
    <item>
      <title>Re: find data point from one table based on date, and pull to another table</title>
      <link>https://communities.sas.com/t5/New-SAS-User/find-data-point-from-one-table-based-on-date-and-pull-to-another/m-p/816943#M34401</link>
      <description>&lt;P&gt;Apologies, ballardw. You are correct in that I incorrectly wrote down this problem.&lt;/P&gt;&lt;P&gt;The problem should have been: for each date-time in file2, find the LAST date-time and weight that precedes it. Saying "find the first that precedes it" is a bit ambiguous.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I also did have the date-time variable formatted as Tom states below (not as a character variable as I originally posted).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Also, thank you for correcting the semicolon on datalines.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How would you tackle this problem? I would be very grateful for your input!&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Kind regards,&lt;BR /&gt;linlin87&lt;/P&gt;</description>
      <pubDate>Tue, 07 Jun 2022 19:24:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/find-data-point-from-one-table-based-on-date-and-pull-to-another/m-p/816943#M34401</guid>
      <dc:creator>u60655191</dc:creator>
      <dc:date>2022-06-07T19:24:31Z</dc:date>
    </item>
    <item>
      <title>Re: find data point from one table based on date, and pull to another table</title>
      <link>https://communities.sas.com/t5/New-SAS-User/find-data-point-from-one-table-based-on-date-and-pull-to-another/m-p/816944#M34402</link>
      <description>&lt;P&gt;The extra ID variable was to demonstrate what to do when you have data from multiple people/places/things/sample.&amp;nbsp; In those cases you do not want the last WEIGHT value to be taken from a different person.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you don't have multiple sampling units it will be very difficult to do any statistical analysis.&lt;/P&gt;</description>
      <pubDate>Tue, 07 Jun 2022 19:26:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/find-data-point-from-one-table-based-on-date-and-pull-to-another/m-p/816944#M34402</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-06-07T19:26:46Z</dc:date>
    </item>
    <item>
      <title>Re: find data point from one table based on date, and pull to another table</title>
      <link>https://communities.sas.com/t5/New-SAS-User/find-data-point-from-one-table-based-on-date-and-pull-to-another/m-p/816945#M34403</link>
      <description>&lt;P&gt;I see, thanks Tom. That is indeed very useful and needed.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How would I pull over the datetime from file1, as well as the weight?&lt;/P&gt;</description>
      <pubDate>Tue, 07 Jun 2022 19:29:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/find-data-point-from-one-table-based-on-date-and-pull-to-another/m-p/816945#M34403</guid>
      <dc:creator>u60655191</dc:creator>
      <dc:date>2022-06-07T19:29:43Z</dc:date>
    </item>
    <item>
      <title>Re: find data point from one table based on date, and pull to another table</title>
      <link>https://communities.sas.com/t5/New-SAS-User/find-data-point-from-one-table-based-on-date-and-pull-to-another/m-p/816946#M34404</link>
      <description>&lt;P&gt;Just extend the process to include that variable.&lt;/P&gt;
&lt;P&gt;You probably want to only consider the dates where there is actually a WEIGHT value.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set file1 file2(in=in2);
  by id date;
  if first.id then call missing(last_wt,last_dt);
  if not missing(weight) then do;
     last_wt = coalesce(weight, last_wt);
     last_dt = date;
  end;
  retain last_wt last_dt;
  format last_dt datetime19. ;
  if in2;
  drop weight;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 07 Jun 2022 19:33:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/find-data-point-from-one-table-based-on-date-and-pull-to-another/m-p/816946#M34404</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-06-07T19:33:43Z</dc:date>
    </item>
    <item>
      <title>Re: find data point from one table based on date, and pull to another table</title>
      <link>https://communities.sas.com/t5/New-SAS-User/find-data-point-from-one-table-based-on-date-and-pull-to-another/m-p/816949#M34406</link>
      <description>&lt;P&gt;Thank you Tom I really appreciate your help. I want to accept this, but I still don't understand how it works. What does in=in2 do? What is the argument in2? What is the coalesce command doing?&lt;/P&gt;</description>
      <pubDate>Tue, 07 Jun 2022 19:43:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/find-data-point-from-one-table-based-on-date-and-pull-to-another/m-p/816949#M34406</guid>
      <dc:creator>u60655191</dc:creator>
      <dc:date>2022-06-07T19:43:06Z</dc:date>
    </item>
    <item>
      <title>Re: find data point from one table based on date, and pull to another table</title>
      <link>https://communities.sas.com/t5/New-SAS-User/find-data-point-from-one-table-based-on-date-and-pull-to-another/m-p/816952#M34407</link>
      <description>&lt;P&gt;Read about &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/ledsoptsref/titlepage.htm" target="_self"&gt;dataset options&lt;/A&gt;. IN= allows you to provide the NAME of a variable that will be TRUE when that dataset contributed to the current observation.&amp;nbsp; So because of the &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lestmtsref/p1cxl8ifdt8u0gn12wqbji8o5fq1.htm" target="_self"&gt;subsetting IF statement&lt;/A&gt; only the observations read from TABLE2 will be make to the output dataset.&lt;/P&gt;
&lt;P&gt;Read about &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/titlepage.htm" target="_self"&gt;functions&lt;/A&gt;. COALESCE() just means take the first non missing value in the argument list.&amp;nbsp; So if WEIGTH is not empty it replaces LAST_WT.&amp;nbsp; With the extra IF NOT MISSING(weight) test in the last code I posted it is no longer needed.&lt;/P&gt;</description>
      <pubDate>Tue, 07 Jun 2022 20:00:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/find-data-point-from-one-table-based-on-date-and-pull-to-another/m-p/816952#M34407</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-06-07T20:00:26Z</dc:date>
    </item>
    <item>
      <title>Re: find data point from one table based on date, and pull to another table</title>
      <link>https://communities.sas.com/t5/New-SAS-User/find-data-point-from-one-table-based-on-date-and-pull-to-another/m-p/817005#M34412</link>
      <description>&lt;P&gt;Hi Tom,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for this. It is working but not quite how I expect, because of one difference in the datafile to how I described in my original post. The files are as follows:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data file1;
  input id date :datetime. weight;
  format date datetime19.;
datalines;
1  07FEB2020:08:10:08 68.9
1  07FEB2020:09:10:08 68.2
1  10FEB2020:10:10:08 68.4
1  13FEB2020:11:10:08 68.7
1  14FEB2020:12:10:08 68.1
1  16FEB2020:13:10:08 67.4
1  18FEB2020:14:10:08 67.9
2  21FEB2020:15:10:08 80.2
2  08MAR2020:16:10:08 81.2
2  07FEB2020:08:10:08 68.9
2  07FEB2020:09:10:08 68.2
2  10FEB2020:10:10:08 68.4
2  13FEB2020:09:10:08 68.7
3  14FEB2020:12:10:08 68.1
3  16FEB2020:10:10:08 67.4
3  18FEB2020:14:10:08 67.9
3  21FEB2020:11:10:08 80.2
3  10FEB2020:23:10:08 82.9
;
run;

data file2; 
  input id refdate :datetime. ;
  format date datetime19.;
datalines; 
1  07FEB2020:10:40:08
1  10FEB2020:16:45:08
1  14FEB2020:12:20:08
1  20FEB2020:11:54:08
1  02MAR2020:12:58:08
1  07FEB2020:10:40:08
1  10FEB2020:16:45:08
1  14FEB2020:12:20:08
2  20FEB2020:11:54:08
2  02MAR2020:12:58:08
2  07FEB2020:13:40:08
2  10FEB2020:17:40:08
2  14FEB2020:12:20:08
2  20FEB2020:09:54:08
2  02MAR2020:12:08:08
3  07FEB2020:10:40:08
3  10FEB2020:14:40:08
3  14FEB2020:12:20:08
3  20FEB2020:19:54:08
3  02MAR2020:12:58:08
3  07FEB2020:11:00:08
3  10FEB2020:16:45:08
;
run;&lt;/PRE&gt;&lt;P&gt;Now obviously the code doesn't work because the variable name in file2 is refdtm not dtm. How would I make your code work for this? I could just change the name of refdtm to dtm, but wondering if you have a better fix.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Kind regards,&lt;BR /&gt;linlin87&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 08 Jun 2022 08:05:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/find-data-point-from-one-table-based-on-date-and-pull-to-another/m-p/817005#M34412</guid>
      <dc:creator>linlin87</dc:creator>
      <dc:date>2022-06-08T08:05:32Z</dc:date>
    </item>
    <item>
      <title>Re: find data point from one table based on date, and pull to another table</title>
      <link>https://communities.sas.com/t5/New-SAS-User/find-data-point-from-one-table-based-on-date-and-pull-to-another/m-p/817058#M34426</link>
      <description>&lt;P&gt;Did you read the documentation on dataset options I sent the link for?&amp;nbsp; For this you need the RENAME= dataset option.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;merge table1(rename=(date=refdate)) .....&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Make sure to modify the references to DATE to use new REFDATE name instead in the rest of the data step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PS Using DATE for a variable that does not actually contain DATE (number of days) values will cause you confusion later on.&amp;nbsp; Your variables contain&amp;nbsp;DATETIME (number of seconds) values instead of DATE values.&lt;/P&gt;</description>
      <pubDate>Wed, 08 Jun 2022 12:33:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/find-data-point-from-one-table-based-on-date-and-pull-to-another/m-p/817058#M34426</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-06-08T12:33:45Z</dc:date>
    </item>
  </channel>
</rss>

