<?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: SQL Join Macro in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/SQL-Join-Macro/m-p/329600#M73764</link>
    <description>SAS didnt like the (keep=) part of your proc transpose so I removed it and ran the three statements. &lt;BR /&gt;&lt;BR /&gt;The result looks good. It's funny how complicated we try to make things sometimes. &lt;BR /&gt;&lt;BR /&gt;I'll mark this as an accepted solution and see how well this works with my large datasets. Appreciate the time and effort, thank you!</description>
    <pubDate>Thu, 02 Feb 2017 21:30:18 GMT</pubDate>
    <dc:creator>Ody</dc:creator>
    <dc:date>2017-02-02T21:30:18Z</dc:date>
    <item>
      <title>SQL Join Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-Join-Macro/m-p/329531#M73735</link>
      <description>&lt;P&gt;Hello all,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Looking for some direction here on a post I made a few days ago.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This code is working as intended. I am creating several child tables (child1, child2, child3, etc...) from the dataset test.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
infile datalines dsd truncover;
input 
ordernum:$3.
order:1.
code:$3.
flex:$3.	
;

datalines;
111, 1, qwe, 12a
111, 2, gfd, 52f
111, 3, sdf, 00c
222, 1, erw, 58h
222, 2, fgh, 77i
333, 1, saa, 36j
333, 2, ath, 49d
333, 3, gdw, 20a
444, 1, etg, 46c
444, 2, hef, 58r
444, 3, sfy, 86c
444, 4, hrl, 22m
555, 1, mmj, 76l
555, 2, bhg, 13a
555, 3, dgy, 66z
555, 4, ggd, 17x
555, 5, dss, 59r
;;;

data parent;
infile datalines dsd truncover;
input 
ordernum:$3.;

datalines;
111
222
333
444
555;


proc sql noprint;
select max(Order) into : order_num
from test;
quit;
%put Max  Count = &amp;amp;order_num;


options mlogic mprint;
%macro split;

data %do i = 1 %to &amp;amp;order_num;
child&amp;amp;i %end;;

set test;
select(Order);
%do i = 1 %to &amp;amp;order_num;
when ("&amp;amp;i") output child&amp;amp;i; 
%end;
otherwise;
end;
run;

%mend split;
%split;


&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to join those child tables to the parent table and add fields from them to the parent. The fields in the child tables all have the same name.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is what I've written so far. I know it doesnt work and at this point I'm pretty much stuck. I dont really know how I should write the syntax and my google-fu has reached its limits. Appreciate any help.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;

%macro joinAll;
proc sql;
create table chk as
select 
a.*,

%if (&amp;amp;i. = &amp;amp;order_num.) %then %do
b.code as code&amp;amp;i.,
b.flex as flex&amp;amp;i.
%end

from parent a

%if (&amp;amp;i. = &amp;amp;order_num.) %then %do
left join child&amp;amp;i. b on a.ordernum = b.ordernum
%end;

quit;
%mend joinAll;

%joinall;
&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;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 02 Feb 2017 18:58:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-Join-Macro/m-p/329531#M73735</guid>
      <dc:creator>Ody</dc:creator>
      <dc:date>2017-02-02T18:58:06Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Join Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-Join-Macro/m-p/329544#M73743</link>
      <description>&lt;P&gt;From where do you get the macro variable i in the macro joinall?&lt;/P&gt;</description>
      <pubDate>Thu, 02 Feb 2017 20:02:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-Join-Macro/m-p/329544#M73743</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-02-02T20:02:04Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Join Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-Join-Macro/m-p/329558#M73747</link>
      <description>&lt;P&gt;This approach is taking a medium-difficult problem and turning it into a horribly complex nightmare.&amp;nbsp; I would suggest you start over, starting with:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc transpose data=test out=codes prefix=code (keep=ordernum code: );&lt;/P&gt;
&lt;P&gt;by ordernum;&lt;/P&gt;
&lt;P&gt;var code;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc transpose data=test out=flexes prefix=flex (keep=ordernum flex: );&lt;/P&gt;
&lt;P&gt;by ordernum;&lt;/P&gt;
&lt;P&gt;var flex;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Take a look at the results at that point ... they should be easy to join your parent table.&lt;/P&gt;</description>
      <pubDate>Thu, 02 Feb 2017 20:21:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-Join-Macro/m-p/329558#M73747</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-02-02T20:21:41Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Join Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-Join-Macro/m-p/329566#M73749</link>
      <description>Ah, you're right. I didn't define i = 1.</description>
      <pubDate>Thu, 02 Feb 2017 20:29:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-Join-Macro/m-p/329566#M73749</guid>
      <dc:creator>Ody</dc:creator>
      <dc:date>2017-02-02T20:29:48Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Join Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-Join-Macro/m-p/329572#M73751</link>
      <description>Thanks for the response. &lt;BR /&gt;&lt;BR /&gt;I need to separate a large data set into smaller chunks by the order field and then merge certain fields from those data sets back to the parent table. On the parent table the ordernum is a unique key. &lt;BR /&gt;&lt;BR /&gt;This is a small example I'm trying to understand so that I can apply it to a much larger dataset. The number of orders is an unknown variable, could be 3, could be 8, etc, which means I will have an unknown number of datasets. That's why didnt use proc transpose. &lt;BR /&gt;&lt;BR /&gt;Joining tables is not a problem, it's doing it via macro and selecting the same field from those tables thats giving me a problem&lt;BR /&gt;</description>
      <pubDate>Thu, 02 Feb 2017 20:40:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-Join-Macro/m-p/329572#M73751</guid>
      <dc:creator>Ody</dc:creator>
      <dc:date>2017-02-02T20:40:01Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Join Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-Join-Macro/m-p/329577#M73755</link>
      <description>&lt;P&gt;OK, I'll try not to jump to conclusions here.&amp;nbsp; Why not take the sample data that you have already posted, and illustrate the result you would like to obtain?&lt;/P&gt;</description>
      <pubDate>Thu, 02 Feb 2017 20:44:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-Join-Macro/m-p/329577#M73755</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-02-02T20:44:49Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Join Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-Join-Macro/m-p/329579#M73756</link>
      <description>&lt;P&gt;I would expect the macro result to look like the result from this query. I'm likely way off base with my macro attempt...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table chk as
select 
a.*,
b.code as code1,
b.flex as flex1,
c.code as code2,
c.flex as flex2,
d.code as code3,
d.flex as flex3,
e.code as code4,
e.flex as flex4,
f.code as code5,
f.flex as flex5

from parent a 
left join child1 b on a.ordernum = b.ordernum
left join child2 c on a.ordernum = c.ordernum
left join child3 d on a.ordernum = d.ordernum
left join child4 e on a.ordernum = e.ordernum
left join child5 f on a.ordernum = f.ordernum

;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 02 Feb 2017 20:55:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-Join-Macro/m-p/329579#M73756</guid>
      <dc:creator>Ody</dc:creator>
      <dc:date>2017-02-02T20:55:19Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Join Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-Join-Macro/m-p/329583#M73759</link>
      <description>&lt;P&gt;This looks like exactly what you would get if you transposed the data and joined.&amp;nbsp; PROC TRANSPOSE creates as many variables as are needed without you needing to know ahead of time what that number will be.&lt;/P&gt;</description>
      <pubDate>Thu, 02 Feb 2017 21:00:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-Join-Macro/m-p/329583#M73759</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-02-02T21:00:39Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Join Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-Join-Macro/m-p/329591#M73761</link>
      <description>Sorry, I guess I misunderstood what you were trying to do with proc transpose.&lt;BR /&gt;&lt;BR /&gt;Could you provide an example of the merge for me using this example data?</description>
      <pubDate>Thu, 02 Feb 2017 21:14:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-Join-Macro/m-p/329591#M73761</guid>
      <dc:creator>Ody</dc:creator>
      <dc:date>2017-02-02T21:14:39Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Join Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-Join-Macro/m-p/329593#M73762</link>
      <description>&lt;P&gt;After the two PROC TRANSPOSE steps above, the merge would look like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;merge codes (in=in1) flexes (in=in2) parent (in=in3);&lt;/P&gt;
&lt;P&gt;by ordernum;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want all the records, regardless of mismatches, that's the program.&amp;nbsp; If you want to select based on matches/mismatches with the parent data set, you can use the in= variables to do that.&lt;/P&gt;</description>
      <pubDate>Thu, 02 Feb 2017 21:20:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-Join-Macro/m-p/329593#M73762</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-02-02T21:20:36Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Join Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-Join-Macro/m-p/329600#M73764</link>
      <description>SAS didnt like the (keep=) part of your proc transpose so I removed it and ran the three statements. &lt;BR /&gt;&lt;BR /&gt;The result looks good. It's funny how complicated we try to make things sometimes. &lt;BR /&gt;&lt;BR /&gt;I'll mark this as an accepted solution and see how well this works with my large datasets. Appreciate the time and effort, thank you!</description>
      <pubDate>Thu, 02 Feb 2017 21:30:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-Join-Macro/m-p/329600#M73764</guid>
      <dc:creator>Ody</dc:creator>
      <dc:date>2017-02-02T21:30:18Z</dc:date>
    </item>
  </channel>
</rss>

