<?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: Full Outer Join in a loop in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Full-Outer-Join-in-a-loop/m-p/767098#M243154</link>
    <description>&lt;P&gt;Before converting to a macro one needs to test the code.&lt;BR /&gt;So please test the code I gave with the your data.&lt;/P&gt;
&lt;P&gt;For any category if there is no data in table2, the final_table will have data from table1 and missing values in the corresponding column of table2. In this case the Total_year will have the value from table1.&lt;/P&gt;
&lt;P&gt;Based on the information in your post I don't foresee any error occurring&lt;BR /&gt;If you do have an error please post the data for the category from both tables and the log.&lt;/P&gt;</description>
    <pubDate>Fri, 10 Sep 2021 18:46:34 GMT</pubDate>
    <dc:creator>Sajid01</dc:creator>
    <dc:date>2021-09-10T18:46:34Z</dc:date>
    <item>
      <title>Full Outer Join in a loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Full-Outer-Join-in-a-loop/m-p/767046#M243124</link>
      <description>&lt;P&gt;I have a macro with a loop inside it which creates two tables per iteration.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;First table has the count of new subscribers on a yearly basis:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data table1;&lt;/P&gt;&lt;P&gt;input fy sub_new;&lt;/P&gt;&lt;P&gt;datalines;&lt;/P&gt;&lt;P&gt;2010&amp;nbsp; 5&lt;/P&gt;&lt;P&gt;2012&amp;nbsp; 6&lt;/P&gt;&lt;P&gt;2013&amp;nbsp; 3&lt;/P&gt;&lt;P&gt;2014&amp;nbsp; 4&lt;/P&gt;&lt;P&gt;2017&amp;nbsp; 2&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The second table has the count of subscribers who leave on a yearly basis:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data table2;&lt;/P&gt;&lt;P&gt;input fy sub_left;&lt;/P&gt;&lt;P&gt;datalines;&lt;/P&gt;&lt;P&gt;2012&amp;nbsp; 5&lt;/P&gt;&lt;P&gt;2013&amp;nbsp; 1&lt;/P&gt;&lt;P&gt;2017&amp;nbsp; 4&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Now I combine both the tables table1 and table2 to create a final table with FY&amp;nbsp; sub_new&amp;nbsp; sub_left&amp;nbsp; total_year (sub_new + sub_left) as the columns. Now loop runs once for every category in my dataset.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Problem:&lt;/P&gt;&lt;P&gt;For some categories, I don't have any data in the table2. So&amp;nbsp; a blank row is passed to the full outer join in the loop iteration and it gives an error. The entire macro stops working.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there a way to ensure that the full outer join still works even when a null table (with no columns) is passed to it?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Idea is to get four columns: One with FY, other with sub_new(table1), third with sub_left(table2) and fourth is sum of col2 and col3 (say Total). Matching is done on FY basis.&lt;/P&gt;&lt;P&gt;If outer join won't work for null tables, what else can i use in the loop?&lt;/P&gt;</description>
      <pubDate>Fri, 10 Sep 2021 14:39:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Full-Outer-Join-in-a-loop/m-p/767046#M243124</guid>
      <dc:creator>vkabdwal</dc:creator>
      <dc:date>2021-09-10T14:39:11Z</dc:date>
    </item>
    <item>
      <title>Re: Full Outer Join in a loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Full-Outer-Join-in-a-loop/m-p/767049#M243126</link>
      <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;Is there a way to ensure that the full outer join still works even when a null table (with no columns) is passed to it?&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Yes, you could check the existence of the second table and the number of rows in the second table, store that information in SAS macro variables, and then something like this&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%if &amp;amp;table_exists and &amp;amp;num_rows&amp;gt;0 %then %do;
/* SQL Outer Join */
%end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;Or maybe its simpler than that, but you didn't show us your join code and you didn't show us the errors you are getting. Please show us by providing the log so we can see both code and errors in the log.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 10 Sep 2021 15:24:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Full-Outer-Join-in-a-loop/m-p/767049#M243126</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-09-10T15:24:01Z</dc:date>
    </item>
    <item>
      <title>Re: Full Outer Join in a loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Full-Outer-Join-in-a-loop/m-p/767070#M243133</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/365823"&gt;@vkabdwal&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You have this issue.&lt;/P&gt;
&lt;P&gt;For some categories, I don't have any data in the table2. &lt;STRONG&gt;So&amp;nbsp; a blank row is passed to the full outer join in the loop iteration and it gives an error. The entire macro stops working&lt;/STRONG&gt;.&lt;/P&gt;
&lt;P&gt;The following code will solve the issue&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data table1;
input fy sub_new;
datalines;
2010  5
2012  6
2013  3
2014  4
2017  2
;
run;
data table2;
input fy sub_left;
datalines;
2012  5
2013  1
2017  4
;
run;

proc sql;
create table final_table as
select a.*,b.* , (coalesce(sub_left,0)+coalesce(sub_new,0)) as Total_year
from table1 a
full outer join table2 b  on 
a.fy=b.fy;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The output dataset (final_table) will be like this.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Sajid01_0-1631291572600.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/63496i8CB21D2DA84ABFF8/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Sajid01_0-1631291572600.png" alt="Sajid01_0-1631291572600.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Please let me know if you have questions.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 10 Sep 2021 16:33:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Full-Outer-Join-in-a-loop/m-p/767070#M243133</guid>
      <dc:creator>Sajid01</dc:creator>
      <dc:date>2021-09-10T16:33:33Z</dc:date>
    </item>
    <item>
      <title>Re: Full Outer Join in a loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Full-Outer-Join-in-a-loop/m-p/767091#M243149</link>
      <description>Correction:&lt;BR /&gt;Original:&lt;BR /&gt;Problem:&lt;BR /&gt;&lt;BR /&gt;For some categories, I don't have any data in the table2. So a blank row is passed to the full outer join in the loop iteration and it gives an error. The entire macro stops working.&lt;BR /&gt;&lt;BR /&gt;Updated:&lt;BR /&gt;Problem:&lt;BR /&gt;&lt;BR /&gt;For some categories, I don't have any data in the table2. So a blank table is passed to the full outer join in the loop iteration and it gives an error. The entire macro stops working.</description>
      <pubDate>Fri, 10 Sep 2021 18:39:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Full-Outer-Join-in-a-loop/m-p/767091#M243149</guid>
      <dc:creator>vkabdwal</dc:creator>
      <dc:date>2021-09-10T18:39:19Z</dc:date>
    </item>
    <item>
      <title>Re: Full Outer Join in a loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Full-Outer-Join-in-a-loop/m-p/767092#M243150</link>
      <description>Updated:&lt;BR /&gt;Problem:&lt;BR /&gt;&lt;BR /&gt;For some categories, I don't have any data in the table2. So a blank table is passed to the full outer join in the loop iteration and it gives an error. The entire macro stops working. Not a blank row in 2nd table but the entire table is null.</description>
      <pubDate>Fri, 10 Sep 2021 18:40:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Full-Outer-Join-in-a-loop/m-p/767092#M243150</guid>
      <dc:creator>vkabdwal</dc:creator>
      <dc:date>2021-09-10T18:40:07Z</dc:date>
    </item>
    <item>
      <title>Re: Full Outer Join in a loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Full-Outer-Join-in-a-loop/m-p/767098#M243154</link>
      <description>&lt;P&gt;Before converting to a macro one needs to test the code.&lt;BR /&gt;So please test the code I gave with the your data.&lt;/P&gt;
&lt;P&gt;For any category if there is no data in table2, the final_table will have data from table1 and missing values in the corresponding column of table2. In this case the Total_year will have the value from table1.&lt;/P&gt;
&lt;P&gt;Based on the information in your post I don't foresee any error occurring&lt;BR /&gt;If you do have an error please post the data for the category from both tables and the log.&lt;/P&gt;</description>
      <pubDate>Fri, 10 Sep 2021 18:46:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Full-Outer-Join-in-a-loop/m-p/767098#M243154</guid>
      <dc:creator>Sajid01</dc:creator>
      <dc:date>2021-09-10T18:46:34Z</dc:date>
    </item>
    <item>
      <title>Re: Full Outer Join in a loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Full-Outer-Join-in-a-loop/m-p/767170#M243179</link>
      <description>&lt;P&gt;The SQL &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/131732"&gt;@Sajid01&lt;/a&gt;&amp;nbsp;posted will also work if one of the tables has zero rows. Just test it out. It works.&lt;/P&gt;
&lt;P&gt;You're talking about some macro falling over so you would now need to share and explain this macro logic to us. ...and may be also explain why you need a macro at all and why this can't be done with Base SAS/SQL only.&lt;/P&gt;</description>
      <pubDate>Sat, 11 Sep 2021 02:52:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Full-Outer-Join-in-a-loop/m-p/767170#M243179</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2021-09-11T02:52:42Z</dc:date>
    </item>
    <item>
      <title>Re: Full Outer Join in a loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Full-Outer-Join-in-a-loop/m-p/767175#M243182</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/365823"&gt;@vkabdwal&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There should be no error&amp;nbsp; as long as table2 exists. The code would obviously fail if table2 does not exist.&lt;/P&gt;
&lt;P&gt;Please see the code below.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data table1;
input fy sub_new;
datalines;
2010  5
2012  6
2013  3
2014  4
2017  2
;
run;

proc sql;
create table table2(Rename=(sub_new=sub_left)) like table1;
quit;

proc sql;
create table final_table as
select a.*,b.* , (coalesce(sub_left,0)+coalesce(sub_new,0)) as Total_year
from table1 a
full outer join table2 b  on 
a.fy=b.fy;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The output will be as follows&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Sajid01_0-1631330150730.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/63511i87094E8028C7E60A/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Sajid01_0-1631330150730.png" alt="Sajid01_0-1631330150730.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 11 Sep 2021 03:17:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Full-Outer-Join-in-a-loop/m-p/767175#M243182</guid>
      <dc:creator>Sajid01</dc:creator>
      <dc:date>2021-09-11T03:17:41Z</dc:date>
    </item>
  </channel>
</rss>

