<?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: How to flag the first row of each obeservation while joining using SQL in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-flag-the-first-row-of-each-obeservation-while-joining/m-p/577571#M163663</link>
    <description>&lt;P&gt;You can add the flag by a sas data step after the sql join or do it in one step using merge instead sql join:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1) add to your sql code the line&amp;nbsp;&lt;STRONG&gt;order by ID, start_date&lt;/STRONG&gt; then add the next step:&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
 set want;
   by id;
        if first.ID then flag=1;
        else flag=0;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2) alternatively:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   merge have (in=in1)
         have2;
   by ID;
        if in1;   /* equivalent to sql left join */
        if first.id then flag = 1;
        else flag = 0;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 30 Jul 2019 00:39:26 GMT</pubDate>
    <dc:creator>Shmuel</dc:creator>
    <dc:date>2019-07-30T00:39:26Z</dc:date>
    <item>
      <title>How to flag the first row of each obeservation while joining using SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-flag-the-first-row-of-each-obeservation-while-joining/m-p/577544#M163654</link>
      <description>&lt;P&gt;&lt;SPAN&gt;Hello,&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I have two tables that I need to join on but want to create a flag on the left table so that when it joins with the right table I get a flag in my final table indicating which row is from the left table. For example, I have one that has 683 rows, this include client ID, admit date, discharge date, service type and 30 other columns. I would join this table with a table that has 8 million rows. I would join on the condition of if the Client ID are the same and the admit date from the left table (683 row)&amp;nbsp;is less then or equal to the admit date on the right table (8 million rows). when I join these two tables I get 1250 rows. Now I want to be able to flag the 1250 rows to indicate the first row that was from the 683 table.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Here is the initial table I would have: &lt;/SPAN&gt;&lt;/P&gt;&lt;PRE&gt;data have; 
informat ID $1. start_date mmddyy10. end_date mmddyy10.;
input ID  start_date  end_date ;
format start_date end_date mmddyy10.;
datalines; 
1	01/01/2018	01/10/2018
2	01/05/2018	01/06/2018
3	02/05/2018	02/15/2018

; run;&lt;/PRE&gt;&lt;P&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;&amp;nbsp;This is the table I would left join with:&lt;/FONT&gt;&lt;/P&gt;&lt;PRE&gt;data have2; 
informat ID $1. start_date mmddyy10. end_date mmddyy10.;
input ID  start_date  end_date ;
format start_date end_date mmddyy10.;
datalines; 
1	01/01/2018	01/10/2018
1	01/11/2018	01/31/2018
1	02/05/2018	02/15/2018
1	02/19/2018	05/30/2018
1	06/15/2018	06/30/2018
1	07/05/2018	07/20/2018
2	01/12/2018	01/15/2018
2	01/25/2018	01/30/2018
3	02/05/2018	02/15/2018
3	03/20/2018	04/15/2018
3	04/20/2018	07/31/2018
3	08/14/2018	09/10/2018
; run;&lt;/PRE&gt;&lt;P&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;This is the table I would want &lt;/FONT&gt;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;ID&lt;/TD&gt;&lt;TD&gt;start_date&lt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&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;2/5/2018&lt;/TD&gt;&lt;TD&gt;2/15/2018&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;3/20/2018&lt;/TD&gt;&lt;TD&gt;4/15/2018&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;4/20/2018&lt;/TD&gt;&lt;TD&gt;7/31/2018&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;8/14/2018&lt;/TD&gt;&lt;TD&gt;9/10/2018&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;I have a case where the date in the first table is not exactly equal to the date that I am joining with, see ID 2 above, so&amp;nbsp;I would need to make a condition of sort that may be similar to first. but how would I do that using SQL join? &lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;Thanks&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 29 Jul 2019 21:39:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-flag-the-first-row-of-each-obeservation-while-joining/m-p/577544#M163654</guid>
      <dc:creator>sas_student1</dc:creator>
      <dc:date>2019-07-29T21:39:58Z</dc:date>
    </item>
    <item>
      <title>Re: How to flag the first row of each obeservation while joining using SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-flag-the-first-row-of-each-obeservation-while-joining/m-p/577571#M163663</link>
      <description>&lt;P&gt;You can add the flag by a sas data step after the sql join or do it in one step using merge instead sql join:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1) add to your sql code the line&amp;nbsp;&lt;STRONG&gt;order by ID, start_date&lt;/STRONG&gt; then add the next step:&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
 set want;
   by id;
        if first.ID then flag=1;
        else flag=0;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2) alternatively:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   merge have (in=in1)
         have2;
   by ID;
        if in1;   /* equivalent to sql left join */
        if first.id then flag = 1;
        else flag = 0;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 30 Jul 2019 00:39:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-flag-the-first-row-of-each-obeservation-while-joining/m-p/577571#M163663</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2019-07-30T00:39:26Z</dc:date>
    </item>
    <item>
      <title>Re: How to flag the first row of each obeservation while joining using SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-flag-the-first-row-of-each-obeservation-while-joining/m-p/577591#M163671</link>
      <description>&lt;P&gt;Please show the SQL you have been using.&lt;/P&gt;
&lt;P&gt;Your description of the desired result is not clear.&lt;/P&gt;
&lt;P&gt;Does the small table have duplicate observations for the same ID?&lt;/P&gt;
&lt;P&gt;Are you just asking to take the MINinum date?&lt;/P&gt;</description>
      <pubDate>Tue, 30 Jul 2019 04:28:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-flag-the-first-row-of-each-obeservation-while-joining/m-p/577591#M163671</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-07-30T04:28:57Z</dc:date>
    </item>
    <item>
      <title>Re: How to flag the first row of each obeservation while joining using SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-flag-the-first-row-of-each-obeservation-while-joining/m-p/577606#M163675</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/103523"&gt;@sas_student1&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;I don't quite get how your join criteria correspond to the proposed output WANT. You say that a match occurs when start_date from HAVE is &lt;EM&gt;less than&lt;/EM&gt; &lt;EM&gt;OR equal&lt;/EM&gt; to start_date from HAVE1. Hence, even though start_date=01/05/2018 from HAVE for ID=2 isn't &lt;EM&gt;equal&lt;/EM&gt; to any start_date from HAVE1 for ID=2, it should be in the output file since it is &lt;EM&gt;less than&lt;/EM&gt; any of the start_date in the ID=2 group in HAVE1.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 30 Jul 2019 05:31:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-flag-the-first-row-of-each-obeservation-while-joining/m-p/577606#M163675</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-07-30T05:31:44Z</dc:date>
    </item>
  </channel>
</rss>

