<?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: Merging datasets and selecting the latest date in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Merging-datasets-and-selecting-the-latest-date/m-p/888919#M39489</link>
    <description>Yes, you need to sort by trace_dt to able to use the if last.trace_dt logic. &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt; pionted out you need to do your merge in away that you a record for each trace_dt. Without have seeing your original data, I would suffest that your do a merge on both icustomerid and trace_dt. Then you can do the if last.trace_dt in the same step.</description>
    <pubDate>Fri, 11 Aug 2023 11:34:41 GMT</pubDate>
    <dc:creator>LinusH</dc:creator>
    <dc:date>2023-08-11T11:34:41Z</dc:date>
    <item>
      <title>Merging datasets and selecting the latest date</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Merging-datasets-and-selecting-the-latest-date/m-p/888892#M39485</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;
&lt;P&gt;I am merging three datasets. I have done by icustomerid. All three datasets have trace_dt (it's a date). I want the output data to show the latest trace_dt out of all the three datasets. I don't know how to approach for this. Could you please help?&lt;/P&gt;
&lt;P&gt;Sample dataset:&lt;/P&gt;
&lt;P&gt;Data Test; &lt;BR /&gt;infile cards expandtabs; &lt;BR /&gt;input icustomerid debt_code Trace_Month$ Trace_DT :date9. N Distinct_Lima_accounts2 Distinct_TPT_accounts2 Distinct_Man_accounts2;&lt;BR /&gt;format Trace_DT date9.;&lt;BR /&gt;datalines ; &lt;BR /&gt;1071 1001452 Sep-18 28SEP2018:03:05:06.310 1 1 0 0&lt;BR /&gt;1226 270840689 Dec-17 09DEC2017:13:57:43.730 1 1 0 0&lt;BR /&gt;1234 379021629 Mar-21 03MAY2022:00:00:00.000 1 1 1 0&lt;BR /&gt;1234 379059801 Mar-21 03MAY2022:00:00:00.000 1 1 1 0&lt;BR /&gt;1234 396978322 Feb-22 03MAY2022:00:00:00.000 1 1 1 0&lt;BR /&gt;1234 401458237 Apr-22 03MAY2022:00:00:00.000 1 1 1 0&lt;BR /&gt;; &lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Fri, 11 Aug 2023 08:36:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Merging-datasets-and-selecting-the-latest-date/m-p/888892#M39485</guid>
      <dc:creator>Sandeep77</dc:creator>
      <dc:date>2023-08-11T08:36:14Z</dc:date>
    </item>
    <item>
      <title>Re: Merging datasets and selecting the latest date</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Merging-datasets-and-selecting-the-latest-date/m-p/888893#M39486</link>
      <description>&lt;P&gt;Assuming this is your merged dataset: make sure it sorted on icustomerid and trace_dt.&lt;/P&gt;
&lt;P&gt;Then do a SET BY, and if last.trace_dt filter.&lt;/P&gt;</description>
      <pubDate>Fri, 11 Aug 2023 08:45:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Merging-datasets-and-selecting-the-latest-date/m-p/888893#M39486</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2023-08-11T08:45:57Z</dc:date>
    </item>
    <item>
      <title>Re: Merging datasets and selecting the latest date</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Merging-datasets-and-selecting-the-latest-date/m-p/888894#M39487</link>
      <description>Hi, yes this is merged dataset and I have sorted by icustomerid and then merged. Do I need to sort by trace_dt also? I didn't get exactly how should I do set by and if last.trace_dt filter. Could you please explain in detail?</description>
      <pubDate>Fri, 11 Aug 2023 08:55:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Merging-datasets-and-selecting-the-latest-date/m-p/888894#M39487</guid>
      <dc:creator>Sandeep77</dc:creator>
      <dc:date>2023-08-11T08:55:27Z</dc:date>
    </item>
    <item>
      <title>Re: Merging datasets and selecting the latest date</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Merging-datasets-and-selecting-the-latest-date/m-p/888897#M39488</link>
      <description>&lt;P&gt;If you MERGE datasets which all have the same variable (besides the BY variable(s)), you will lose values because they are overwritten. Please supply examples for the datasets before the "merge", and the code you used for this.&lt;/P&gt;</description>
      <pubDate>Fri, 11 Aug 2023 09:18:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Merging-datasets-and-selecting-the-latest-date/m-p/888897#M39488</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-08-11T09:18:13Z</dc:date>
    </item>
    <item>
      <title>Re: Merging datasets and selecting the latest date</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Merging-datasets-and-selecting-the-latest-date/m-p/888919#M39489</link>
      <description>Yes, you need to sort by trace_dt to able to use the if last.trace_dt logic. &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt; pionted out you need to do your merge in away that you a record for each trace_dt. Without have seeing your original data, I would suffest that your do a merge on both icustomerid and trace_dt. Then you can do the if last.trace_dt in the same step.</description>
      <pubDate>Fri, 11 Aug 2023 11:34:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Merging-datasets-and-selecting-the-latest-date/m-p/888919#M39489</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2023-08-11T11:34:41Z</dc:date>
    </item>
    <item>
      <title>Re: Merging datasets and selecting the latest date</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Merging-datasets-and-selecting-the-latest-date/m-p/888969#M39491</link>
      <description>&lt;P&gt;If all three datasets have the ID and DATETIME variables then you do NOT want to merge just on the ID variable.&lt;/P&gt;
&lt;P&gt;You should either merge by BOTH.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  merge ds1 ds2 ds3 ;
  by id datetime;
  if last.id;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or interleave by BOTH.&lt;/P&gt;
&lt;P&gt;So this will combine the datesets (not merge them) and keep only the latest observation per ID.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set ds1 ds2 ds3 ;
  by id datetime;
  if last.id;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;NOTE: Datasets have to be sorted already.&amp;nbsp; Use your dataset names and variable names.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If there is only one observation per ID per dataset (or at most one dataset that has multiple observations) then you can&amp;nbsp; merge by ID.&amp;nbsp; But to keep all three DATETIME values you will need to rename two of them.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  merge ds1 ds2(rename=(datetime=dateime2)) ds3(rename=(datetime=datetime3));
  by id;
  datetime = max(datetime,datetime2,datetime3);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If one of them did have multiple observations per ID then you will need add a new SORT step and data step to pick the last date after this since taking the max of the three datetime values might have changed the related order of the date values.&lt;/P&gt;</description>
      <pubDate>Fri, 11 Aug 2023 17:39:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Merging-datasets-and-selecting-the-latest-date/m-p/888969#M39491</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-08-11T17:39:56Z</dc:date>
    </item>
  </channel>
</rss>

