<?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 Data in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Merge-Data/m-p/399256#M96691</link>
    <description>&lt;P&gt;So what is your key for making the merge? Why not add one?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data dat1_fixed;
  set dat1 ;
  by x ;
  rep+1;
  if first.x then rep=1;
run;
data dat2_fixed;
  set dat2 ;
  rep+1;
run;
proc sql ;
  create table want as
    select a.*,b.y
    from dat1_fixed a
    inner join dat2_fixed b
   on a.rep = b.rep
   order by 1,2
 ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;Obs    x    rep    y

  1    1     1     a
  2    1     2     b
  3    1     3     c
  4    1     4     d
  5    2     1     a
  6    2     2     b
  7    2     3     c
  8    2     4     d
  9    3     1     a
 10    3     2     b
 11    3     3     c
 12    3     4     d&lt;/PRE&gt;</description>
    <pubDate>Wed, 27 Sep 2017 16:24:03 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2017-09-27T16:24:03Z</dc:date>
    <item>
      <title>Merge Data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-Data/m-p/399207#M96673</link>
      <description>&lt;P&gt;Hello all,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a question about merging two datasets of different lengths. Datasets look like this:&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data dat1; 
input x; 
datalines; 
1
1
1
1
2
2
2
2
3
3
3
3
;
run; 

data dat2;
input y; 
datalines; 
a
b
c
d
;
run; 

&lt;/PRE&gt;&lt;P&gt;And I want this:&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data want; 
input x y; 
datalines; 
1 a
1 b
1 c
1 d
2 a
2 b
2 c
2 d
3 a
3 b
3 c
3 d
;
run;&lt;/PRE&gt;&lt;P&gt;Since I don't have a unique identifier, I'm not sure how to merge this and allow the variable y to repeat down.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have tried:&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data attempt; 
merge dat1 dat2; 
run; &lt;/PRE&gt;&lt;P&gt;but, this only repeats dat2 once, throuhg the "1"s of dat1. How do I allow dat2 to repeat all the way down the values of dat1?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you!&lt;/P&gt;</description>
      <pubDate>Wed, 27 Sep 2017 15:14:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-Data/m-p/399207#M96673</guid>
      <dc:creator>pamplemouse22</dc:creator>
      <dc:date>2017-09-27T15:14:03Z</dc:date>
    </item>
    <item>
      <title>Re: Merge Data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-Data/m-p/399222#M96680</link>
      <description>I'm puzzled. What is the real life scenario here?&lt;BR /&gt;Merging without common key is usually hazardous.</description>
      <pubDate>Wed, 27 Sep 2017 15:18:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-Data/m-p/399222#M96680</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2017-09-27T15:18:39Z</dc:date>
    </item>
    <item>
      <title>Re: Merge Data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-Data/m-p/399223#M96681</link>
      <description>&lt;P&gt;One SQL way. this does cartesian join and duplicates are removed.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table attempt as
select distinct x,y from dat1,dat2;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 27 Sep 2017 15:21:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-Data/m-p/399223#M96681</guid>
      <dc:creator>PBsas</dc:creator>
      <dc:date>2017-09-27T15:21:46Z</dc:date>
    </item>
    <item>
      <title>Re: Merge Data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-Data/m-p/399252#M96688</link>
      <description>&lt;P&gt;The proc sql is much more compact programming, but might take longer the a data step for large files.&amp;nbsp; After all, it first does a cartesian crossing only to delete the majority of cross-records (3/4 in your case).&amp;nbsp; Of course, that could be solved by having only unique X values in DAT1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But here, as requested is a data step solution, assuming that DAT1 is sorted by X&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=_:);
  set dat1 ;
  by x;

  if _n_=1 then do;
    if 0 then set dat2 (obs=0) nobs=ndat2;
    declare hash d2 (dataset:'need2', ordered:'Y');
      d2.definekey('_key');
      d2.definedata(all:'Y');
      d2.definedone();
  end;

  _key+1;
  if first.x then _key=1;
  _rc=d2.find();
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This will work as expected if DAT1 has exactly NDAT2 observation per X value.&amp;nbsp; (NDAT2&amp;nbsp;is numeber of obs in DAT2).&amp;nbsp; It won't cause any "erroneous" values if a given X value has fewer than NDAT2 records.&amp;nbsp; But if an X group has more than NDAT2, then note that the last value of Y in NDAT2 will prevail in all the "extra" dat1 records - i.e. you'll get a duplicate X/Y combination.&amp;nbsp; If you want to protect against that, then precede the "_rc=d2.find()" statement with a "call missing(y)", protecting against duplicate non-missing Y's.&lt;/P&gt;</description>
      <pubDate>Wed, 27 Sep 2017 16:12:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-Data/m-p/399252#M96688</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-09-27T16:12:57Z</dc:date>
    </item>
    <item>
      <title>Re: Merge Data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-Data/m-p/399256#M96691</link>
      <description>&lt;P&gt;So what is your key for making the merge? Why not add one?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data dat1_fixed;
  set dat1 ;
  by x ;
  rep+1;
  if first.x then rep=1;
run;
data dat2_fixed;
  set dat2 ;
  rep+1;
run;
proc sql ;
  create table want as
    select a.*,b.y
    from dat1_fixed a
    inner join dat2_fixed b
   on a.rep = b.rep
   order by 1,2
 ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;Obs    x    rep    y

  1    1     1     a
  2    1     2     b
  3    1     3     c
  4    1     4     d
  5    2     1     a
  6    2     2     b
  7    2     3     c
  8    2     4     d
  9    3     1     a
 10    3     2     b
 11    3     3     c
 12    3     4     d&lt;/PRE&gt;</description>
      <pubDate>Wed, 27 Sep 2017 16:24:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-Data/m-p/399256#M96691</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-09-27T16:24:03Z</dc:date>
    </item>
    <item>
      <title>Re: Merge Data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-Data/m-p/399305#M96705</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/112197"&gt;@pamplemouse22&lt;/a&gt;&amp;nbsp; &amp;nbsp;Hiter the hash can help here I think. Assuming your dat1 is sorted as shown in your example-&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;data&lt;/STRONG&gt; dat1;&lt;/P&gt;&lt;P&gt;input x;&lt;/P&gt;&lt;P&gt;datalines;&lt;/P&gt;&lt;P&gt;1&lt;/P&gt;&lt;P&gt;1&lt;/P&gt;&lt;P&gt;1&lt;/P&gt;&lt;P&gt;1&lt;/P&gt;&lt;P&gt;2&lt;/P&gt;&lt;P&gt;2&lt;/P&gt;&lt;P&gt;2&lt;/P&gt;&lt;P&gt;2&lt;/P&gt;&lt;P&gt;3&lt;/P&gt;&lt;P&gt;3&lt;/P&gt;&lt;P&gt;3&lt;/P&gt;&lt;P&gt;3&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;data&lt;/STRONG&gt; dat2;&lt;/P&gt;&lt;P&gt;input y $;&lt;/P&gt;&lt;P&gt;datalines;&lt;/P&gt;&lt;P&gt;a&lt;/P&gt;&lt;P&gt;b&lt;/P&gt;&lt;P&gt;c&lt;/P&gt;&lt;P&gt;d&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;;&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;&lt;P&gt;&lt;STRONG&gt;data&lt;/STRONG&gt; want;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; if _N_ = &lt;STRONG&gt;1&lt;/STRONG&gt; then do;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if &lt;STRONG&gt;0&lt;/STRONG&gt; then set dat2;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; declare hash h(dataset:"dat2", ordered: 'yes');&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; declare hiter iter('h');&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; h.defineKey('y');&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; h.defineData('y');&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; h.defineDone();&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; end;&lt;/P&gt;&lt;P&gt;set dat1;&lt;/P&gt;&lt;P&gt;by x;&lt;/P&gt;&lt;P&gt;&amp;nbsp;if first.x then&amp;nbsp; iter.first();&lt;/P&gt;&lt;P&gt;&amp;nbsp;else iter.next();&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Naveen Srinivasan&lt;/P&gt;</description>
      <pubDate>Wed, 27 Sep 2017 19:07:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-Data/m-p/399305#M96705</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2017-09-27T19:07:01Z</dc:date>
    </item>
    <item>
      <title>Re: Merge Data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-Data/m-p/399444#M96750</link>
      <description>&lt;PRE&gt;data want;
  do _N_=1 by 1 until(last.x);
    set dat1;
    by x;
    if _N_&amp;lt;=n2 then
      set dat2 point=_N_ nobs=n2;
    else y=.;
    output;
    end;
run;

&lt;/PRE&gt;&lt;P&gt;This solution puts Y values on as many X-s as there are, and if it runs out of Y values, it sets Y missing.&lt;/P&gt;</description>
      <pubDate>Thu, 28 Sep 2017 10:05:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-Data/m-p/399444#M96750</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2017-09-28T10:05:59Z</dc:date>
    </item>
  </channel>
</rss>

