<?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: many many merge issue in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/many-many-merge-issue/m-p/883953#M349217</link>
    <description>&lt;P&gt;If you really need to replicate the action of MERGE without the NOTE about multiple repeats then you need to add a new counting variable that counts only up to the number of observations per subject in the smaller dataset.&amp;nbsp; Then you can replicate by adding that new variable to the BY statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you know ONE will have at most two observations per subject then just do:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data one;
  set one;
  by subjid;
  row = not first.subjid;
run;
data two;
  set two;
  row = not first.subjid;
run;
data want;
   merge one two;
   by subjid row;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you don't know the maximum number of observations per ID in ONE, but you know it will never be more than the maximum number in TWO then you could to this to add a ROW value to both datasets.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data oneB oneC(keep=subjid row rename=(row=maxrow));
  set one;
  by subjid ;
  if first.subjid then row=0;
  row+1;
  output oneB;
  if last.subjid then output oneC;
run;

data twoB ;
  merge two onec;
  by subjid;
  if first.subjid then row=0;
  row+1;
  row=min(row,maxrow);
  drop maxrow;
run;


data want;
  merge oneB twoB;
  by subjid row;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If it is possible that for some ids the maximum will be the TWO dataset then the logic will need to be more complicated.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data maxrow;
  set one(in=in1) two(in=in2);
  by subjid;
  if first.subjid then call missing(max1,max2);
  max1+in1;
  max2+in2;
  if last.subjid;
  maxrow=max(max1,max2);
  keep subjid maxrow;
run;

data oneB;
  merge one(in=in1) maxrow;
  by subjid;
  if first.subjid then row=0;
  row+1;
  row=min(row,maxrow);
  if in1;
  drop maxrow;
run;

data twoB;
  merge two(in=in2) maxrow;
  by subjid;
  if first.subjid then row=0;
  row+1;
  row=min(row,maxrow);
  if in2;
  drop maxrow;
run;

data want;
  merge oneB twoB ;
  by subjid row;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 07 Jul 2023 13:53:55 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2023-07-07T13:53:55Z</dc:date>
    <item>
      <title>many many merge issue</title>
      <link>https://communities.sas.com/t5/SAS-Programming/many-many-merge-issue/m-p/883725#M349144</link>
      <description>&lt;P&gt;Hello all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have this dataset:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;subjid stdy&amp;nbsp;&lt;/P&gt;&lt;P&gt;103002 1&lt;/P&gt;&lt;P&gt;103002 141&lt;/P&gt;&lt;P&gt;103003 1&lt;/P&gt;&lt;P&gt;103003 534&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;and i need to merge with this dataset:&lt;/P&gt;&lt;P&gt;subjid ady aval&lt;/P&gt;&lt;P&gt;103002&amp;nbsp; 42 stable disease&lt;/P&gt;&lt;P&gt;103002 98 progressive disease&lt;/P&gt;&lt;P&gt;103002 140 NE&lt;/P&gt;&lt;P&gt;103003 45 stable disease&lt;/P&gt;&lt;P&gt;103003 101 stable disease&lt;/P&gt;&lt;P&gt;103003 157 stable disease&amp;nbsp;&lt;/P&gt;&lt;P&gt;103003 213 stable disease&lt;/P&gt;&lt;P&gt;103003 269 stable disease&lt;/P&gt;&lt;P&gt;103003 325 stable disease&amp;nbsp;&lt;/P&gt;&lt;P&gt;103003 380 stable disease&lt;/P&gt;&lt;P&gt;103003 436 stable disease&lt;/P&gt;&lt;P&gt;103003 491 progressive disease&lt;/P&gt;&lt;P&gt;103003 549 stable disease&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need to keep that order ady in the final dataset and avoid the message : MERGE statement has more than one dataset with repeat by values.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;thanks in advance.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 06 Jul 2023 13:40:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/many-many-merge-issue/m-p/883725#M349144</guid>
      <dc:creator>mapuchito</dc:creator>
      <dc:date>2023-07-06T13:40:14Z</dc:date>
    </item>
    <item>
      <title>Re: many many merge issue</title>
      <link>https://communities.sas.com/t5/SAS-Programming/many-many-merge-issue/m-p/883754#M349153</link>
      <description>Please show expected results.</description>
      <pubDate>Thu, 06 Jul 2023 15:24:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/many-many-merge-issue/m-p/883754#M349153</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2023-07-06T15:24:10Z</dc:date>
    </item>
    <item>
      <title>Re: many many merge issue</title>
      <link>https://communities.sas.com/t5/SAS-Programming/many-many-merge-issue/m-p/883760#M349155</link>
      <description>&lt;P&gt;If you need to preserve the order after merging, you could create a sortkey that is the row number.&amp;nbsp; sortkey= _N_;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 06 Jul 2023 16:33:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/many-many-merge-issue/m-p/883760#M349155</guid>
      <dc:creator>KRusso</dc:creator>
      <dc:date>2023-07-06T16:33:58Z</dc:date>
    </item>
    <item>
      <title>Re: many many merge issue</title>
      <link>https://communities.sas.com/t5/SAS-Programming/many-many-merge-issue/m-p/883777#M349160</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/445852"&gt;@mapuchito&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I need to keep that order ady in the final dataset and avoid the message : MERGE statement has more than one dataset with repeat by values.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;To avoid this message, you need&amp;nbsp; to de-duplicate all but one of the datasets used in the MERGE.&lt;/P&gt;
&lt;P&gt;Please show the expected result of the merge of your datasets..&lt;/P&gt;</description>
      <pubDate>Thu, 06 Jul 2023 17:03:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/many-many-merge-issue/m-p/883777#M349160</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-07-06T17:03:55Z</dc:date>
    </item>
    <item>
      <title>Re: many many merge issue</title>
      <link>https://communities.sas.com/t5/SAS-Programming/many-many-merge-issue/m-p/883781#M349161</link>
      <description>&lt;P&gt;Either MERGE with both SUBJECT and DAY.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  merge one(in=in1 rename=(stdy=day)) two(in=in2 rename=(ady=day));
  by subjid day;
  if in1 then stdy=day;
  if in2 then ady=day;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or depending on what you are actually trying to do INTERLEAVE instead.&amp;nbsp; So to find the most recent STDY value for each ADY value you could use something like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set one(in=in1 rename=(stdy=day)) two(in=in2 rename=(ady=day));
  by subjid day;
  if in1 then stdy=day;
  if in2 and first.subjid then call missing(stdy);
  retain stdy;
  if in2 then ady=day;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 06 Jul 2023 17:16:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/many-many-merge-issue/m-p/883781#M349161</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-07-06T17:16:14Z</dc:date>
    </item>
    <item>
      <title>Re: many many merge issue</title>
      <link>https://communities.sas.com/t5/SAS-Programming/many-many-merge-issue/m-p/883901#M349194</link>
      <description>&lt;P&gt;the expected results in my dataset is:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;103002 1 42 stable disease&lt;/P&gt;&lt;P&gt;103002 141 98 progressive disease&lt;/P&gt;&lt;P&gt;103002 141 140 NE&lt;/P&gt;&lt;P&gt;103003 1 45 stable disease&lt;/P&gt;&lt;P&gt;103003 534 101 stable disease&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;103003 534 549 stable disease&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;the first row (stdy=1) corresponding the first ady (aval),&lt;/P&gt;&lt;P&gt;and the rest of the ady (aval) values go to to the last row (stdy)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I hope it is more clear now.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance.&lt;/P&gt;</description>
      <pubDate>Fri, 07 Jul 2023 08:43:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/many-many-merge-issue/m-p/883901#M349194</guid>
      <dc:creator>mapuchito</dc:creator>
      <dc:date>2023-07-07T08:43:14Z</dc:date>
    </item>
    <item>
      <title>Re: many many merge issue</title>
      <link>https://communities.sas.com/t5/SAS-Programming/many-many-merge-issue/m-p/883916#M349199</link>
      <description>&lt;P&gt;Which means you will get the message about multiple datasets with repeats. No way around that.&lt;/P&gt;</description>
      <pubDate>Fri, 07 Jul 2023 10:11:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/many-many-merge-issue/m-p/883916#M349199</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-07-07T10:11:37Z</dc:date>
    </item>
    <item>
      <title>Re: many many merge issue</title>
      <link>https://communities.sas.com/t5/SAS-Programming/many-many-merge-issue/m-p/883941#M349212</link>
      <description>&lt;P&gt;That is exactly what a MERGE statement will do with a many to many merge.&amp;nbsp; Ignore the note as you are expecting there to be a many to many merge.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data one ;
  input subjid stdy ;
cards;
103002 1
103002 141
103003 1
103003 534
;

data two;
  input subjid ady aval $40. ;
cards;
103002  42 stable disease
103002  98 progressive disease
103002 140 NE
103003  45 stable disease
103003 101 stable disease
103003 157 stable disease 
103003 213 stable disease
103003 269 stable disease
103003 325 stable disease 
103003 380 stable disease
103003 436 stable disease
103003 491 progressive disease
103003 549 stable disease
;

data expect ;
  input subjid stdy ady aval $40.;
cards;
103002 1 42 stable disease
103002 141 98 progressive disease
103002 141 140 NE
103003 1 45 stable disease
103003 534 101 stable disease
;

data want;
  merge one two;
  by subjid;
run;

proc compare data=want compare=expect;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;What do you want to happen if there is only one observation in the second dataset for a subject?&lt;/P&gt;
&lt;P&gt;If you want to avoid having ADY and AVAL repeated onto every value of STDY then add and explicit OUTPUT and a CALL MISSING().&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  merge one two;
  by subjid;
  output;
  call missing(ady,aval);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 07 Jul 2023 12:29:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/many-many-merge-issue/m-p/883941#M349212</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-07-07T12:29:00Z</dc:date>
    </item>
    <item>
      <title>Re: many many merge issue</title>
      <link>https://communities.sas.com/t5/SAS-Programming/many-many-merge-issue/m-p/883943#M349213</link>
      <description>&lt;P&gt;i cannot ignore the note, i am afraid.&amp;nbsp;&lt;/P&gt;&lt;P&gt;it should be away to do it, to avoid the note.&lt;/P&gt;&lt;P&gt;thanks.&lt;/P&gt;</description>
      <pubDate>Fri, 07 Jul 2023 12:32:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/many-many-merge-issue/m-p/883943#M349213</guid>
      <dc:creator>mapuchito</dc:creator>
      <dc:date>2023-07-07T12:32:17Z</dc:date>
    </item>
    <item>
      <title>Re: many many merge issue</title>
      <link>https://communities.sas.com/t5/SAS-Programming/many-many-merge-issue/m-p/883945#M349215</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/445852"&gt;@mapuchito&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;i cannot ignore the note, i am afraid.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;it should be away to do it, to avoid the note.&lt;/P&gt;
&lt;P&gt;thanks.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Why?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can add your own note.&lt;/P&gt;
&lt;PRE&gt;79   data want;
80     merge one two;
81     by subjid;
82     if _N_=1 then put 'NOTE: MERGE statement is EXPECTED to have more than one data set with repeats of BY values.';
83   run;

NOTE: MERGE statement is EXPECTED to have more than one data set with repeats of BY values.
NOTE: MERGE statement has more than one data set with repeats of BY values.
NOTE: There were 4 observations read from the data set WORK.ONE.
NOTE: There were 13 observations read from the data set WORK.TWO.
NOTE: The data set WORK.WANT has 13 observations and 4 variables.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds
&lt;/PRE&gt;</description>
      <pubDate>Fri, 07 Jul 2023 12:52:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/many-many-merge-issue/m-p/883945#M349215</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-07-07T12:52:42Z</dc:date>
    </item>
    <item>
      <title>Re: many many merge issue</title>
      <link>https://communities.sas.com/t5/SAS-Programming/many-many-merge-issue/m-p/883953#M349217</link>
      <description>&lt;P&gt;If you really need to replicate the action of MERGE without the NOTE about multiple repeats then you need to add a new counting variable that counts only up to the number of observations per subject in the smaller dataset.&amp;nbsp; Then you can replicate by adding that new variable to the BY statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you know ONE will have at most two observations per subject then just do:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data one;
  set one;
  by subjid;
  row = not first.subjid;
run;
data two;
  set two;
  row = not first.subjid;
run;
data want;
   merge one two;
   by subjid row;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you don't know the maximum number of observations per ID in ONE, but you know it will never be more than the maximum number in TWO then you could to this to add a ROW value to both datasets.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data oneB oneC(keep=subjid row rename=(row=maxrow));
  set one;
  by subjid ;
  if first.subjid then row=0;
  row+1;
  output oneB;
  if last.subjid then output oneC;
run;

data twoB ;
  merge two onec;
  by subjid;
  if first.subjid then row=0;
  row+1;
  row=min(row,maxrow);
  drop maxrow;
run;


data want;
  merge oneB twoB;
  by subjid row;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If it is possible that for some ids the maximum will be the TWO dataset then the logic will need to be more complicated.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data maxrow;
  set one(in=in1) two(in=in2);
  by subjid;
  if first.subjid then call missing(max1,max2);
  max1+in1;
  max2+in2;
  if last.subjid;
  maxrow=max(max1,max2);
  keep subjid maxrow;
run;

data oneB;
  merge one(in=in1) maxrow;
  by subjid;
  if first.subjid then row=0;
  row+1;
  row=min(row,maxrow);
  if in1;
  drop maxrow;
run;

data twoB;
  merge two(in=in2) maxrow;
  by subjid;
  if first.subjid then row=0;
  row+1;
  row=min(row,maxrow);
  if in2;
  drop maxrow;
run;

data want;
  merge oneB twoB ;
  by subjid row;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 07 Jul 2023 13:53:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/many-many-merge-issue/m-p/883953#M349217</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-07-07T13:53:55Z</dc:date>
    </item>
    <item>
      <title>Re: many many merge issue</title>
      <link>https://communities.sas.com/t5/SAS-Programming/many-many-merge-issue/m-p/884045#M349255</link>
      <description>&lt;P&gt;You can replicate the merge results in a single data step and avoid the "multiple data sets have repeats of by values" as below.&amp;nbsp; Why one would need to do this, I don't see.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data merge_without_notes (drop=_:);
  /* For each subjid, read and count obs in each dataset */
  do until (last.subjid);
    set one (in=in1)  two (in=in2);
    by subjid;
    _n1+in1;
    _n2+in2;
  end;
  /* Reread both datasets, output, and  decrement each count to zero */
  do until (max(_n1,_n2)=0);
    if _n1&amp;gt;0 then set one;
    if _n2&amp;gt;0 then set two;
    output;
    _n1=ifn(_n1&amp;gt;0,_n1-1,0);
    _n2=ifn(_n2&amp;gt;0,_n2-1,0);
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 08 Jul 2023 13:50:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/many-many-merge-issue/m-p/884045#M349255</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-07-08T13:50:02Z</dc:date>
    </item>
    <item>
      <title>Re: many many merge issue</title>
      <link>https://communities.sas.com/t5/SAS-Programming/many-many-merge-issue/m-p/884047#M349257</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/445852"&gt;@mapuchito&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;i cannot ignore the note, i am afraid.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Why? It's a NOTE, not an ERROR or WARNING. It does not cause a non-zero exit code in batch mode.&lt;/P&gt;</description>
      <pubDate>Sat, 08 Jul 2023 06:02:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/many-many-merge-issue/m-p/884047#M349257</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-07-08T06:02:23Z</dc:date>
    </item>
    <item>
      <title>Re: many many merge issue</title>
      <link>https://communities.sas.com/t5/SAS-Programming/many-many-merge-issue/m-p/884576#M349476</link>
      <description>Brilliant, this is exactly what I wanted. Thanks a lot.</description>
      <pubDate>Wed, 12 Jul 2023 20:20:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/many-many-merge-issue/m-p/884576#M349476</guid>
      <dc:creator>mapuchito</dc:creator>
      <dc:date>2023-07-12T20:20:11Z</dc:date>
    </item>
  </channel>
</rss>

