<?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 in proc sql when some of data sets are not exisiting in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Merge-in-proc-sql-when-some-of-data-sets-are-not-exisiting/m-p/715991#M221209</link>
    <description>&lt;P&gt;I would try with something like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro if_existsSQL(dataset, base=a, id=id);
%if %sysfunc(exist(&amp;amp;dataset)) %then 
%do;
	left join &amp;amp;dataset.(rename=(&amp;amp;id.=&amp;amp;id._&amp;amp;dataset.))
	on &amp;amp;base..&amp;amp;id.=&amp;amp;dataset..&amp;amp;id._&amp;amp;dataset.
%end;
%mend if_existsSQL;

PROC SQL;
	create table wanted2(drop=id_:)  as
	select * 	   
	from  a
  %if_existsSQL(b)
  %if_existsSQL(c)
  %if_existsSQL(d)
  %if_existsSQL(e)
  %if_existsSQL(f)
;
QUIT;
proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;but, this SQL is not the same as merge is. You have left joins here and they are not equivalent to merge. You have to have the "right side" dataset to perform left join on it and merge doesn't care about it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The renaming part is to avoid this:&amp;nbsp;&lt;A href="https://support.sas.com/kb/9/963.html" target="_self"&gt;https://support.sas.com/kb/9/963.html&lt;/A&gt;&amp;nbsp;warning message. I assume that all other variables names do not interfere.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;All the best&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
    <pubDate>Tue, 02 Feb 2021 10:04:10 GMT</pubDate>
    <dc:creator>yabwon</dc:creator>
    <dc:date>2021-02-02T10:04:10Z</dc:date>
    <item>
      <title>Merge in proc sql when some of data sets are not exisiting</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-in-proc-sql-when-some-of-data-sets-are-not-exisiting/m-p/715983#M221206</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;I know how to merge data sets in data step .&lt;/P&gt;
&lt;P&gt;I know how to dealt with situation when some of the data sets are not existing.&lt;/P&gt;
&lt;P&gt;My question:&lt;/P&gt;
&lt;P&gt;What i the way to merge via proc sql when some of the data sets are not exisiting?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a;
input id x;
cards;
1 10
2 20
;
run;
data b;
input id w;
cards;
1 12
2 23
;
run;

data c;
input id y;
cards;
1 30
2 50
;
run;
 
%macro if_exists(dataset);
%if %sysfunc(exist(&amp;amp;dataset)) %then &amp;amp;dataset ;
%mend if_exists;

/**Way1-working well**/
data wanted1;
merge
%if_exists(a)
%if_exists(b)
%if_exists(c)
%if_exists(d)
%if_exists(e)
%if_exists(f)
;
by id;
run;

/**Way2-How can I do it??**/
PROC SQL;
	create table wanted2  as
	select  	   
	from  a
	left join  b
	on a.id=b.id
	left join  c
	on a.id=c.id
	left join  d
	on a.id=d.id
	left join e
	on a.id=e.id
	left join  f
	on a.id=f.id
;
QUIT;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 02 Feb 2021 09:27:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-in-proc-sql-when-some-of-data-sets-are-not-exisiting/m-p/715983#M221206</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2021-02-02T09:27:19Z</dc:date>
    </item>
    <item>
      <title>Re: Merge in proc sql when some of data sets are not exisiting</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-in-proc-sql-when-some-of-data-sets-are-not-exisiting/m-p/715991#M221209</link>
      <description>&lt;P&gt;I would try with something like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro if_existsSQL(dataset, base=a, id=id);
%if %sysfunc(exist(&amp;amp;dataset)) %then 
%do;
	left join &amp;amp;dataset.(rename=(&amp;amp;id.=&amp;amp;id._&amp;amp;dataset.))
	on &amp;amp;base..&amp;amp;id.=&amp;amp;dataset..&amp;amp;id._&amp;amp;dataset.
%end;
%mend if_existsSQL;

PROC SQL;
	create table wanted2(drop=id_:)  as
	select * 	   
	from  a
  %if_existsSQL(b)
  %if_existsSQL(c)
  %if_existsSQL(d)
  %if_existsSQL(e)
  %if_existsSQL(f)
;
QUIT;
proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;but, this SQL is not the same as merge is. You have left joins here and they are not equivalent to merge. You have to have the "right side" dataset to perform left join on it and merge doesn't care about it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The renaming part is to avoid this:&amp;nbsp;&lt;A href="https://support.sas.com/kb/9/963.html" target="_self"&gt;https://support.sas.com/kb/9/963.html&lt;/A&gt;&amp;nbsp;warning message. I assume that all other variables names do not interfere.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;All the best&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Tue, 02 Feb 2021 10:04:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-in-proc-sql-when-some-of-data-sets-are-not-exisiting/m-p/715991#M221209</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2021-02-02T10:04:10Z</dc:date>
    </item>
    <item>
      <title>Re: Merge in proc sql when some of data sets are not exisiting</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-in-proc-sql-when-some-of-data-sets-are-not-exisiting/m-p/716045#M221233</link>
      <description>Thank you but I am looking for same solution as in data step but with proc sql.&lt;BR /&gt;It means that I want to perform same merge as in data step  with sql method&lt;BR /&gt;</description>
      <pubDate>Tue, 02 Feb 2021 12:35:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-in-proc-sql-when-some-of-data-sets-are-not-exisiting/m-p/716045#M221233</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2021-02-02T12:35:29Z</dc:date>
    </item>
    <item>
      <title>Re: Merge in proc sql when some of data sets are not exisiting</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-in-proc-sql-when-some-of-data-sets-are-not-exisiting/m-p/716047#M221234</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159549"&gt;@Ronein&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Thank you but I am looking for same solution as in data step but with proc sql.&lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Then use a DATA step merge. &lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/Maxims-of-Maximally-Efficient-SAS-Programmers/ta-p/352068" target="_self"&gt;Maxim 14&lt;/A&gt;: Use the right tool.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Don't assume that SQL does everything identically to DATA step merge.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 02 Feb 2021 12:40:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-in-proc-sql-when-some-of-data-sets-are-not-exisiting/m-p/716047#M221234</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-02-02T12:40:51Z</dc:date>
    </item>
    <item>
      <title>Re: Merge in proc sql when some of data sets are not exisiting</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-in-proc-sql-when-some-of-data-sets-are-not-exisiting/m-p/716135#M221274</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159549"&gt;@Ronein&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Thank you but I am looking for same solution as in data step but with proc sql.&lt;BR /&gt;It means that I want to perform same merge as in data step with sql method&lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;There is no easy SQL that replicates the data step MERGE statement.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The way the observations are combined is different when any of the dataset have multiple observations per BY group.&amp;nbsp; Which dataset's value is used when you have non-key common variables is different.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 02 Feb 2021 17:02:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-in-proc-sql-when-some-of-data-sets-are-not-exisiting/m-p/716135#M221274</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-02-02T17:02:15Z</dc:date>
    </item>
  </channel>
</rss>

