<?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 Tables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Merge-Tables/m-p/812665#M320649</link>
    <description>&lt;P&gt;From now on, when you get an error message, please show us the &lt;FONT color="#FF0000"&gt;ENTIRE&lt;/FONT&gt; log for this DATA step or PROC.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;where T1.TermStartDate le T2.StartDate le T1.TermEndDate;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is not valid data step syntax. There is no syntax in a data step like&amp;nbsp;&lt;FONT face="courier new,courier"&gt;T1.variablename&lt;FONT face="arial,helvetica,sans-serif"&gt;, variable names cannot have any additional text like&lt;/FONT&gt; T1. &lt;FONT face="arial,helvetica,sans-serif"&gt;pre-pended to it.&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think what you want is this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;where TermStartDate le StartDate le TermEndDate;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 11 May 2022 14:34:35 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2022-05-11T14:34:35Z</dc:date>
    <item>
      <title>Merge Tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-Tables/m-p/812662#M320648</link>
      <description>&lt;P&gt;Hi....I wonder if what I am trying to do is possible using merge. I would like to merge Tables 1 &amp;amp; 2. The StartDate is only in Table2 and the TermStartDate and TermEndDate are only in Table1. When I run this code I get an error message. Thanks...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data Table3;
     merge Table1(in=T1) Table2(in=T2);
     by ID Year Course;
     where T1.TermStartDate le T2.StartDate le T1.TermEndDate;
run;&lt;/PRE&gt;</description>
      <pubDate>Wed, 11 May 2022 14:19:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-Tables/m-p/812662#M320648</guid>
      <dc:creator>twildone</dc:creator>
      <dc:date>2022-05-11T14:19:37Z</dc:date>
    </item>
    <item>
      <title>Re: Merge Tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-Tables/m-p/812665#M320649</link>
      <description>&lt;P&gt;From now on, when you get an error message, please show us the &lt;FONT color="#FF0000"&gt;ENTIRE&lt;/FONT&gt; log for this DATA step or PROC.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;where T1.TermStartDate le T2.StartDate le T1.TermEndDate;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is not valid data step syntax. There is no syntax in a data step like&amp;nbsp;&lt;FONT face="courier new,courier"&gt;T1.variablename&lt;FONT face="arial,helvetica,sans-serif"&gt;, variable names cannot have any additional text like&lt;/FONT&gt; T1. &lt;FONT face="arial,helvetica,sans-serif"&gt;pre-pended to it.&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think what you want is this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;where TermStartDate le StartDate le TermEndDate;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 11 May 2022 14:34:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-Tables/m-p/812665#M320649</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-05-11T14:34:35Z</dc:date>
    </item>
    <item>
      <title>Re: Merge Tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-Tables/m-p/812683#M320653</link>
      <description>&lt;P&gt;This is better accomplished via SQL not a data step.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 11 May 2022 14:58:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-Tables/m-p/812683#M320653</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-05-11T14:58:47Z</dc:date>
    </item>
    <item>
      <title>Re: Merge Tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-Tables/m-p/812687#M320656</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;This is better accomplished via SQL not a data step.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I guess I'm not in agreement. It seems to me that either a DATA step or SQL will get the job done, I see no reason to prefer one over the other. Can you explain why you think this is better in SQL?&lt;/P&gt;</description>
      <pubDate>Wed, 11 May 2022 15:15:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-Tables/m-p/812687#M320656</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-05-11T15:15:55Z</dc:date>
    </item>
    <item>
      <title>Re: Merge Tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-Tables/m-p/812690#M320658</link>
      <description>&lt;P&gt;Are you attempting to do a many-to-many join?&amp;nbsp; That is: Is it possible for both TABLE1 and TABLE2 to have multiple observations for the same combination of ID YEAR and COURSE values?&amp;nbsp; If so then you do NOT want to use a data step merge, use an SQL join instead.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You cannot have periods in variable names.&lt;/P&gt;
&lt;P&gt;IN= option sets the name of the VARIABLE that indicates it that dataset contributed to the observation.&lt;/P&gt;
&lt;P&gt;You cannot use variables in a WHERE statement that are not on ALL of the input datasets.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Use a subsetting IF instead.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Table3;
     merge Table1(in=T1) Table2(in=T2);
     by ID Year Course;
     if t1 and t2;
     if TermStartDate le StartDate le TermEndDate;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 11 May 2022 15:32:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-Tables/m-p/812690#M320658</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-05-11T15:32:13Z</dc:date>
    </item>
    <item>
      <title>Re: Merge Tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-Tables/m-p/812703#M320663</link>
      <description>Hi Tom.....Yes the merging of the tables would be a many-to-many join. I did use SQL to join the tables and it did work but the processing time was long. I tried to use a Data Step with Merge (with and without T1. &amp;amp; T2.) and both times it wouldn't work so from your response it must be because TermStartDate, StartDate and TermEndDate are not in both tables. Thanks for the explanation.</description>
      <pubDate>Wed, 11 May 2022 16:38:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-Tables/m-p/812703#M320663</guid>
      <dc:creator>twildone</dc:creator>
      <dc:date>2022-05-11T16:38:24Z</dc:date>
    </item>
    <item>
      <title>Re: Merge Tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-Tables/m-p/812773#M320700</link>
      <description>Data step will merge all the data together and then filter out the records not needed via an IF statement. The WHERE statement will not work. &lt;BR /&gt;&lt;BR /&gt;SQL only writes the required data to a dataset in the first place from my understanding. &lt;BR /&gt;&lt;BR /&gt;This probably only becomes important as your data sets get larger.</description>
      <pubDate>Wed, 11 May 2022 18:47:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-Tables/m-p/812773#M320700</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-05-11T18:47:45Z</dc:date>
    </item>
    <item>
      <title>Re: Merge Tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-Tables/m-p/812784#M320704</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4061"&gt;@twildone&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Hi Tom.....Yes the merging of the tables would be a many-to-many join. I did use SQL to join the tables and it did work but the processing time was long. I tried to use a Data Step with Merge (with and without T1. &amp;amp; T2.) and both times it wouldn't work so from your response it must be because TermStartDate, StartDate and TermEndDate are not in both tables. Thanks for the explanation.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;It might be possible to combine the data via a interleaving SET statement instead of MERGE, but it really depends on how many variables are you trying to carry over.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So perhaps something like this to attach VAR1 from table2 to observations from table1 where the date falls into the interval in table2 and rename it as VAR1_COPY.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   set table1(in=in1 rename=(startdate=date)) table2(in=in2 rename=(termstartdate=date termenddate=date2));
   by id&amp;nbsp;year&amp;nbsp;course&amp;nbsp;date;
&amp;nbsp;&amp;nbsp;&amp;nbsp;if&amp;nbsp;first.course&amp;nbsp;then&amp;nbsp;call misisng(termstartdate,termenddate,var1_copy);
&amp;nbsp;&amp;nbsp;&amp;nbsp;retain&amp;nbsp;termstartdate&amp;nbsp;termenddate var1_copy;
&amp;nbsp;&amp;nbsp;&amp;nbsp;format termstartdate&amp;nbsp;termenddate date9.;
&amp;nbsp;&amp;nbsp;&amp;nbsp;if&amp;nbsp;in2&amp;nbsp;then&amp;nbsp;do;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;termstartdate=date;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;termenddate=date2;
      var1_copy = var1;
&amp;nbsp;&amp;nbsp;&amp;nbsp;end;
&amp;nbsp;&amp;nbsp;&amp;nbsp;if&amp;nbsp;in1 and (termstartdate&amp;nbsp;&amp;lt;=&amp;nbsp;date &amp;lt;= termenddate);
&amp;nbsp;&amp;nbsp;&amp;nbsp;rename&amp;nbsp;date=startdate;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 11 May 2022 19:16:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-Tables/m-p/812784#M320704</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-05-11T19:16:26Z</dc:date>
    </item>
  </channel>
</rss>

