<?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: Merge two datasets to obtain first date in which condition happened in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Merge-two-datasets-to-obtain-first-date-in-which-condition/m-p/736368#M229388</link>
    <description>Yes, data is sorted by ID and Date.&lt;BR /&gt;&lt;BR /&gt;Sorry for that, fixed that format error, now it should be fine.&lt;BR /&gt;&lt;BR /&gt;I also forgot to add that if it could be a proc sql answer that would be great, cause the rest of my code is all in proc sql&lt;BR /&gt;</description>
    <pubDate>Thu, 22 Apr 2021 11:18:33 GMT</pubDate>
    <dc:creator>catkat96</dc:creator>
    <dc:date>2021-04-22T11:18:33Z</dc:date>
    <item>
      <title>Merge two datasets to obtain first date in which condition happened</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-two-datasets-to-obtain-first-date-in-which-condition/m-p/736353#M229378</link>
      <description>&lt;P&gt;I have two datasets that I'm trying to merge to get a new variable I'm interested in.&lt;/P&gt;&lt;P&gt;The first dataset,&lt;SPAN&gt;&amp;nbsp;data1&lt;/SPAN&gt;, has one row per ID. The second one,&lt;SPAN&gt;&amp;nbsp;data&lt;/SPAN&gt;, has many rows (dates) per ID, like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="sas"&gt;data data1;
input id :$2. var1;
datalines;
01 1
02 2
03 3
;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE class="lang-sql s-code-block hljs"&gt;&lt;CODE&gt;data Data2;
infile datalines dlm="|";
input ID :$2. date :ddmmyy. (code1 code2 code3) ($) payment;
format callDate ddmmyy10.;
datalines;
01 | 01/01/2020 | AA    | AA    | AA    | 1
01 | 02/01/2020 | AA    | AA    | AA    | 1
01 | 03/01/2020 | AA    | AA    | AA    | 1
01 | 12/02/2020 | AA    | AA    | AA    | 2
01 | 13/02/2020 | BB    | AA    | AA    | 2
01 | 14/01/2020 | BB    | AA    | AA    | 2
01 | 15/01/2020 | CC    | BB    | AA    | 2
02 | 03/01/2020 | .     | .     | .     | 1
02 | 04/01/2020 | AA    | .     | .     | 1
02 | 05/01/2020 | AA    | .     | .     | 1 
02 | 06/01/2020 | CC    | AA    | .     | 1
02 | 07/01/2020 | CC    | AA    | .     | 1
03 | 20/03/2020 | AA    | AA    | .     | 2
03 | 21/03/2020 | AA    | AA    | .     | 3 
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I want to merge them so that I get all the columns in&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;CODE&gt;data1&lt;/CODE&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;and I get a two new columns:&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;CODE&gt;position&lt;/CODE&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;and&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;CODE&gt;date&lt;/CODE&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;that would have the firs date in which&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;CODE&gt;code1&lt;/CODE&gt;,&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;CODE&gt;code2&lt;/CODE&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;or&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;CODE&gt;code3&lt;/CODE&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;get BB or CC OR&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;CODE&gt;payment&lt;/CODE&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;is larger or equal than 3, per ID.&lt;/P&gt;&lt;P&gt;Basically in this case this should be the outcome:&lt;/P&gt;&lt;PRE class="lang-sql s-code-block hljs"&gt;&lt;CODE&gt;ID &lt;SPAN class="hljs-operator"&gt;|&lt;/SPAN&gt; Var1 &lt;SPAN class="hljs-operator"&gt;|&lt;/SPAN&gt; &lt;SPAN class="hljs-type"&gt;Date&lt;/SPAN&gt;       &lt;SPAN class="hljs-operator"&gt;|&lt;/SPAN&gt; Position 
&lt;SPAN class="hljs-number"&gt;01&lt;/SPAN&gt; &lt;SPAN class="hljs-operator"&gt;|&lt;/SPAN&gt; &lt;SPAN class="hljs-number"&gt;1&lt;/SPAN&gt;    &lt;SPAN class="hljs-operator"&gt;|&lt;/SPAN&gt; &lt;SPAN class="hljs-number"&gt;13&lt;/SPAN&gt;&lt;SPAN class="hljs-operator"&gt;/&lt;/SPAN&gt;&lt;SPAN class="hljs-number"&gt;02&lt;/SPAN&gt;&lt;SPAN class="hljs-operator"&gt;/&lt;/SPAN&gt;&lt;SPAN class="hljs-number"&gt;2020&lt;/SPAN&gt; &lt;SPAN class="hljs-operator"&gt;|&lt;/SPAN&gt; code
&lt;SPAN class="hljs-number"&gt;02&lt;/SPAN&gt; &lt;SPAN class="hljs-operator"&gt;|&lt;/SPAN&gt; &lt;SPAN class="hljs-number"&gt;2&lt;/SPAN&gt;    &lt;SPAN class="hljs-operator"&gt;|&lt;/SPAN&gt; &lt;SPAN class="hljs-number"&gt;06&lt;/SPAN&gt;&lt;SPAN class="hljs-operator"&gt;/&lt;/SPAN&gt;&lt;SPAN class="hljs-number"&gt;01&lt;/SPAN&gt;&lt;SPAN class="hljs-operator"&gt;/&lt;/SPAN&gt;&lt;SPAN class="hljs-number"&gt;2020&lt;/SPAN&gt; &lt;SPAN class="hljs-operator"&gt;|&lt;/SPAN&gt; code   
&lt;SPAN class="hljs-number"&gt;03&lt;/SPAN&gt; &lt;SPAN class="hljs-operator"&gt;|&lt;/SPAN&gt; &lt;SPAN class="hljs-number"&gt;3&lt;/SPAN&gt;    &lt;SPAN class="hljs-operator"&gt;|&lt;/SPAN&gt; &lt;SPAN class="hljs-number"&gt;21&lt;/SPAN&gt;&lt;SPAN class="hljs-operator"&gt;/&lt;/SPAN&gt;&lt;SPAN class="hljs-number"&gt;03&lt;/SPAN&gt;&lt;SPAN class="hljs-operator"&gt;/&lt;/SPAN&gt;&lt;SPAN class="hljs-number"&gt;2020&lt;/SPAN&gt; &lt;SPAN class="hljs-operator"&gt;|&lt;/SPAN&gt; payment&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;CODE&gt;Var1&lt;/CODE&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;is just an example of the variables in&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;CODE&gt;data1&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;This is the code I tried, but it's not working as it should. I'm sure there's a detail wrong but I can't figure out what it is.&lt;/P&gt;&lt;PRE class="lang-sql s-code-block hljs"&gt;&lt;CODE&gt;proc &lt;SPAN class="hljs-keyword"&gt;sql&lt;/SPAN&gt;;
&lt;SPAN class="hljs-keyword"&gt;create&lt;/SPAN&gt; &lt;SPAN class="hljs-keyword"&gt;table&lt;/SPAN&gt; &lt;SPAN class="hljs-keyword"&gt;new&lt;/SPAN&gt; &lt;SPAN class="hljs-keyword"&gt;as&lt;/SPAN&gt;
&lt;SPAN class="hljs-keyword"&gt;select&lt;/SPAN&gt; a.&lt;SPAN class="hljs-operator"&gt;*&lt;/SPAN&gt;
 , b.position
 &lt;SPAN class="hljs-keyword"&gt;from&lt;/SPAN&gt; data1 a
 &lt;SPAN class="hljs-keyword"&gt;left&lt;/SPAN&gt; &lt;SPAN class="hljs-keyword"&gt;join&lt;/SPAN&gt; (&lt;SPAN class="hljs-keyword"&gt;select&lt;/SPAN&gt; &lt;SPAN class="hljs-keyword"&gt;distinct&lt;/SPAN&gt; id
            , datestamp format&lt;SPAN class="hljs-operator"&gt;=&lt;/SPAN&gt;ddmmyy10.
            , &lt;SPAN class="hljs-keyword"&gt;case&lt;/SPAN&gt; &lt;SPAN class="hljs-keyword"&gt;when&lt;/SPAN&gt; whichc("AA", code1, code2, code3) &lt;SPAN class="hljs-operator"&gt;&amp;gt;&lt;/SPAN&gt; &lt;SPAN class="hljs-number"&gt;0&lt;/SPAN&gt; &lt;SPAN class="hljs-keyword"&gt;then&lt;/SPAN&gt; "code"
                   &lt;SPAN class="hljs-keyword"&gt;when&lt;/SPAN&gt; whichc("BB", code1, code2, code3) &lt;SPAN class="hljs-operator"&gt;&amp;gt;&lt;/SPAN&gt; &lt;SPAN class="hljs-number"&gt;0&lt;/SPAN&gt; &lt;SPAN class="hljs-keyword"&gt;then&lt;/SPAN&gt; "code"
                   &lt;SPAN class="hljs-keyword"&gt;when&lt;/SPAN&gt; payment &lt;SPAN class="hljs-operator"&gt;&amp;gt;=&lt;/SPAN&gt; &lt;SPAN class="hljs-number"&gt;3&lt;/SPAN&gt; &lt;SPAN class="hljs-keyword"&gt;then&lt;/SPAN&gt; "payment" &lt;SPAN class="hljs-keyword"&gt;else&lt;/SPAN&gt; . &lt;SPAN class="hljs-keyword"&gt;end&lt;/SPAN&gt; &lt;SPAN class="hljs-keyword"&gt;as&lt;/SPAN&gt; position
            &lt;SPAN class="hljs-keyword"&gt;from&lt;/SPAN&gt; data2
            &lt;SPAN class="hljs-keyword"&gt;group&lt;/SPAN&gt; &lt;SPAN class="hljs-keyword"&gt;by&lt;/SPAN&gt; id
            &lt;SPAN class="hljs-keyword"&gt;having&lt;/SPAN&gt; datestamp&lt;SPAN class="hljs-operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="hljs-built_in"&gt;min&lt;/SPAN&gt;(datestamp)) b
    &lt;SPAN class="hljs-keyword"&gt;on&lt;/SPAN&gt; a.id &lt;SPAN class="hljs-operator"&gt;=&lt;/SPAN&gt; b.id;
        quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Any ideas on how I could make this work?&lt;/P&gt;&lt;P&gt;SQL code is prefered since the rest of my code is in that format.&lt;/P&gt;</description>
      <pubDate>Thu, 22 Apr 2021 11:20:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-two-datasets-to-obtain-first-date-in-which-condition/m-p/736353#M229378</guid>
      <dc:creator>catkat96</dc:creator>
      <dc:date>2021-04-22T11:20:39Z</dc:date>
    </item>
    <item>
      <title>Re: Merge two datasets to obtain first date in which condition happened</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-two-datasets-to-obtain-first-date-in-which-condition/m-p/736355#M229379</link>
      <description>&lt;P&gt;Please test your datalines code in the future, so we do not have to fix it first.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;See this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Data1;
infile datalines dlm="|";
input ID :$2. date :ddmmyy. (code1 code2 code3) ($) payment;
format date ddmmyy10.;
datalines;
01 | 01/01/2020 | AA    | AA    | AA    | 1
01 | 02/01/2020 | AA    | AA    | AA    | 1
01 | 03/01/2020 | AA    | AA    | AA    | 1
01 | 12/02/2020 | AA    | AA    | AA    | 2
01 | 13/02/2020 | BB    | AA    | AA    | 2
01 | 14/01/2020 | BB    | AA    | AA    | 2
01 | 15/01/2020 | CC    | BB    | AA    | 2
02 | 03/01/2020 | .     | .     | .     | 1
02 | 04/01/2020 | AA    | .     | .     | 1
02 | 05/01/2020 | AA    | .     | .     | 1 
02 | 06/01/2020 | CC    | AA    | .     | 1
02 | 07/01/2020 | CC    | AA    | .     | 1
03 | 20/03/2020 | AA    | AA    | .     | 2
03 | 21/03/2020 | AA    | AA    | .     | 3 
;

data data2;
input id :$2. var1;
datalines;
01 1
02 2
03 3
;

data want;
merge
  data2
  data1
;
by id;
retain flag;
length position $7;
if first.id then flag = 1;
if flag
then do;
  if code1 in ('BB','CC') or code2 in ('BB','CC') or code3 in ('BB','CC')
  then do;
    flag = 0;
    position = "code";
  end;
  else if payment ge 3
  then do;
    flag = 0;
    position = "payment";
  end;
  if flag = 0 then output;
end;
keep id var1 date position;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 22 Apr 2021 09:38:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-two-datasets-to-obtain-first-date-in-which-condition/m-p/736355#M229379</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-04-22T09:38:54Z</dc:date>
    </item>
    <item>
      <title>Re: Merge two datasets to obtain first date in which condition happened</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-two-datasets-to-obtain-first-date-in-which-condition/m-p/736356#M229380</link>
      <description>&lt;P&gt;First off, you refer to the data set called 'data1' as both the data that has a single and multiple rows per ID.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;Can you post examples of both data sets?&lt;/P&gt;</description>
      <pubDate>Thu, 22 Apr 2021 09:39:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-two-datasets-to-obtain-first-date-in-which-condition/m-p/736356#M229380</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2021-04-22T09:39:05Z</dc:date>
    </item>
    <item>
      <title>Re: Merge two datasets to obtain first date in which condition happened</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-two-datasets-to-obtain-first-date-in-which-condition/m-p/736361#M229383</link>
      <description>&lt;P&gt;Also, is it safe to assume that your data is sorted by ID and date?&lt;/P&gt;</description>
      <pubDate>Thu, 22 Apr 2021 10:18:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-two-datasets-to-obtain-first-date-in-which-condition/m-p/736361#M229383</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2021-04-22T10:18:36Z</dc:date>
    </item>
    <item>
      <title>Re: Merge two datasets to obtain first date in which condition happened</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-two-datasets-to-obtain-first-date-in-which-condition/m-p/736368#M229388</link>
      <description>Yes, data is sorted by ID and Date.&lt;BR /&gt;&lt;BR /&gt;Sorry for that, fixed that format error, now it should be fine.&lt;BR /&gt;&lt;BR /&gt;I also forgot to add that if it could be a proc sql answer that would be great, cause the rest of my code is all in proc sql&lt;BR /&gt;</description>
      <pubDate>Thu, 22 Apr 2021 11:18:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-two-datasets-to-obtain-first-date-in-which-condition/m-p/736368#M229388</guid>
      <dc:creator>catkat96</dc:creator>
      <dc:date>2021-04-22T11:18:33Z</dc:date>
    </item>
    <item>
      <title>Re: Merge two datasets to obtain first date in which condition happened</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-two-datasets-to-obtain-first-date-in-which-condition/m-p/736372#M229390</link>
      <description>&lt;P&gt;Maxim 14: Use the Right Tool.&lt;/P&gt;
&lt;P&gt;In this case, it's the data step.&lt;/P&gt;
&lt;P&gt;The fact that you seem to not be very versed in data step programming yet does not mean that you should not use it, but that you have to learn it. NOW. It is THE "Swiss Army Knife" of SAS.&lt;/P&gt;</description>
      <pubDate>Thu, 22 Apr 2021 11:35:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-two-datasets-to-obtain-first-date-in-which-condition/m-p/736372#M229390</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-04-22T11:35:22Z</dc:date>
    </item>
  </channel>
</rss>

