<?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: one to many merge does not work as expected in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/one-to-many-merge-does-not-work-as-expected/m-p/664884#M198696</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It seems that this is what you may need:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Test_nomatch;
input (constituent_ticker SecID name_cleaned) ($);
cards;
abc 1 A
def 2 B
ghi 3 C
abc 4 A
def 5 B
ghi 6 C
123 1 D
456 2 E
678 3 F
;
run;

data Test_stats;
input (fund_ticker Name name_cleaned) ($);
cards;
x a A
y b B
y c C
t a A
s b B
v c C
;
run;



data matched notmatched(drop=fund_ticker Name);
  if 0 then set Test_nomatch Test_stats;
  declare hash TS(dataset:"Test_stats", multidata:"Y");
  TS.defineKey("name_cleaned");
  TS.defineData("fund_ticker","Name");
  TS.defineDone();

  do until(EOF);
    set Test_nomatch end=EOF;

    if TS.check() = 0 then
    do;
      TS.reset_dup();
      do while(TS.do_over() = 0);
        output matched;
      end;
    end;
    else
      /*call missing(fund_ticker,Name);*/
      output notmatched;
  end;

stop;
run;&lt;/CODE&gt;&lt;/PRE&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>Thu, 25 Jun 2020 08:29:53 GMT</pubDate>
    <dc:creator>yabwon</dc:creator>
    <dc:date>2020-06-25T08:29:53Z</dc:date>
    <item>
      <title>one to many merge does not work as expected</title>
      <link>https://communities.sas.com/t5/SAS-Programming/one-to-many-merge-does-not-work-as-expected/m-p/664876#M198689</link>
      <description>&lt;P&gt;I want to merging all obs in the dataset TEST_STATS to each obs in the dataset TEST_NOMATCH if they match by the NAME_CLEANED variable. I have attached the sample datasets so they only have obs with 1 value for the NAME_CLEANED var to test. My real dataset has many values for this NAME_CLEANED variable. If TEST_STATS has 4 obs and TEST_NOMATCH has 40 obs, what I want to have is a new dataset with160(40x4) obs.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Originally, I did this using a DATA step with MERGE. However, I found out that they dont really do what I want.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test_merged; merge test_nomatch test_stats; by name_cleaned; run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The output has only 40 obs, where the values change for the first 4 obs and then remain the same for the last 36 obs.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I can achieve what I want by using PROC SQL as followed:&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table test_merged_sql as
select a.*, b.*
from test_nomatch as a, test_stats as b
where a.name_cleaned= b.name_cleaned;
run;quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;My question: how can I achieve what I want using a DATA step? and What went wrong with my original DATA step?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 25 Jun 2020 05:38:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/one-to-many-merge-does-not-work-as-expected/m-p/664876#M198689</guid>
      <dc:creator>somebody</dc:creator>
      <dc:date>2020-06-25T05:38:35Z</dc:date>
    </item>
    <item>
      <title>Re: one to many merge does not work as expected</title>
      <link>https://communities.sas.com/t5/SAS-Programming/one-to-many-merge-does-not-work-as-expected/m-p/664877#M198690</link>
      <description>&lt;P&gt;The reason I would want to use a DATA step is that I can separate between the obs that matches and the ones that do not match. How can I do this with PROC SQL?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 25 Jun 2020 05:48:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/one-to-many-merge-does-not-work-as-expected/m-p/664877#M198690</guid>
      <dc:creator>somebody</dc:creator>
      <dc:date>2020-06-25T05:48:46Z</dc:date>
    </item>
    <item>
      <title>Re: one to many merge does not work as expected</title>
      <link>https://communities.sas.com/t5/SAS-Programming/one-to-many-merge-does-not-work-as-expected/m-p/664878#M198691</link>
      <description>&lt;P&gt;Please post example data that matches your real-data as close as possible. And post data as data-step-code, that makes it easier to use. &lt;/P&gt;</description>
      <pubDate>Thu, 25 Jun 2020 05:52:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/one-to-many-merge-does-not-work-as-expected/m-p/664878#M198691</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2020-06-25T05:52:30Z</dc:date>
    </item>
    <item>
      <title>Re: one to many merge does not work as expected</title>
      <link>https://communities.sas.com/t5/SAS-Programming/one-to-many-merge-does-not-work-as-expected/m-p/664879#M198692</link>
      <description>&lt;P&gt;SAS data sets have different internal format depending OS and SAS version,&lt;/P&gt;
&lt;P&gt;thus attaching a sas data-set is not desirable.&lt;/P&gt;</description>
      <pubDate>Thu, 25 Jun 2020 06:10:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/one-to-many-merge-does-not-work-as-expected/m-p/664879#M198692</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2020-06-25T06:10:11Z</dc:date>
    </item>
    <item>
      <title>Re: one to many merge does not work as expected</title>
      <link>https://communities.sas.com/t5/SAS-Programming/one-to-many-merge-does-not-work-as-expected/m-p/664880#M198693</link>
      <description>&lt;P&gt;is there a way to export the dataset as a data-step code? or do I have to do this manually?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 25 Jun 2020 06:20:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/one-to-many-merge-does-not-work-as-expected/m-p/664880#M198693</guid>
      <dc:creator>somebody</dc:creator>
      <dc:date>2020-06-25T06:20:12Z</dc:date>
    </item>
    <item>
      <title>Re: one to many merge does not work as expected</title>
      <link>https://communities.sas.com/t5/SAS-Programming/one-to-many-merge-does-not-work-as-expected/m-p/664881#M198694</link>
      <description>&lt;P&gt;&lt;A href="https://blogs.sas.com/content/sastraining/2016/03/11/jedi-sas-tricks-data-to-data-step-macro/" target="_self"&gt;How to convert datasets to data steps&lt;/A&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 25 Jun 2020 06:28:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/one-to-many-merge-does-not-work-as-expected/m-p/664881#M198694</guid>
      <dc:creator>sustagens</dc:creator>
      <dc:date>2020-06-25T06:28:03Z</dc:date>
    </item>
    <item>
      <title>Re: one to many merge does not work as expected</title>
      <link>https://communities.sas.com/t5/SAS-Programming/one-to-many-merge-does-not-work-as-expected/m-p/664882#M198695</link>
      <description>&lt;P&gt;Here are the datasets:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test_stats;
   input constituent_ticker: $5. secid: $20. name_cleaned: $100.;
datalines;

AA	0P00018WO0	ALCOA
AA	0P0000008J	ALCOA
AA	0P0000A57O	ALCOA
AAI	0P00006XU1	ALCOA

run;


data test_nomatch;
   input fund_ticker: $5. name: $100. name_cleaned: $100.;
datalines;
ACWI	ALCOA	ALCOA
DGT	ALCOA	ALCOA
EUSA	ALCOA	ALCOA
GRES	ALCOA	ALCOA
HAP	ALCOA	ALCOA
IGE	ALCOA	ALCOA
ITOT	ALCOA	ALCOA
IUSV	ALCOA	ALCOA
IVE	ALCOA	ALCOA
IVV	ALCOA	ALCOA
IWB	ALCOA	ALCOA
IWD	ALCOA	ALCOA
IWR	ALCOA	ALCOA
IWS	ALCOA	ALCOA
IWV	ALCOA	ALCOA
IYM	ALCOA	ALCOA
IYY	ALCOA	ALCOA
JHML	ALCOA	ALCOA
JHMM	ALCOA	ALCOA
JKI	ALCOA	ALCOA
MXI	ALCOA	ALCOA
PBP	ALCOA	ALCOA
PHDG	ALCOA	ALCOA
PICK	ALCOA	ALCOA
PRF	ALCOA	ALCOA
PYZ	ALCOA	ALCOA
RWL	ALCOA	ALCOA
SIZE	ALCOA	ALCOA
SPHB	ALCOA	ALCOA
SPLG	ALCOA	ALCOA
SPTM	ALCOA	ALCOA
SPY	ALCOA	ALCOA
SPYV	ALCOA	ALCOA
SPYX	ALCOA	ALCOA
TOK	ALCOA	ALCOA
URTH	ALCOA	ALCOA
VLUE	ALCOA	ALCOA
XLB	ALCOA	ALCOA
XME	ALCOA	ALCOA
XMVM	ALCOA	ALCOA
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 25 Jun 2020 06:31:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/one-to-many-merge-does-not-work-as-expected/m-p/664882#M198695</guid>
      <dc:creator>somebody</dc:creator>
      <dc:date>2020-06-25T06:31:52Z</dc:date>
    </item>
    <item>
      <title>Re: one to many merge does not work as expected</title>
      <link>https://communities.sas.com/t5/SAS-Programming/one-to-many-merge-does-not-work-as-expected/m-p/664884#M198696</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It seems that this is what you may need:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Test_nomatch;
input (constituent_ticker SecID name_cleaned) ($);
cards;
abc 1 A
def 2 B
ghi 3 C
abc 4 A
def 5 B
ghi 6 C
123 1 D
456 2 E
678 3 F
;
run;

data Test_stats;
input (fund_ticker Name name_cleaned) ($);
cards;
x a A
y b B
y c C
t a A
s b B
v c C
;
run;



data matched notmatched(drop=fund_ticker Name);
  if 0 then set Test_nomatch Test_stats;
  declare hash TS(dataset:"Test_stats", multidata:"Y");
  TS.defineKey("name_cleaned");
  TS.defineData("fund_ticker","Name");
  TS.defineDone();

  do until(EOF);
    set Test_nomatch end=EOF;

    if TS.check() = 0 then
    do;
      TS.reset_dup();
      do while(TS.do_over() = 0);
        output matched;
      end;
    end;
    else
      /*call missing(fund_ticker,Name);*/
      output notmatched;
  end;

stop;
run;&lt;/CODE&gt;&lt;/PRE&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>Thu, 25 Jun 2020 08:29:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/one-to-many-merge-does-not-work-as-expected/m-p/664884#M198696</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2020-06-25T08:29:53Z</dc:date>
    </item>
    <item>
      <title>Re: one to many merge does not work as expected</title>
      <link>https://communities.sas.com/t5/SAS-Programming/one-to-many-merge-does-not-work-as-expected/m-p/664901#M198699</link>
      <description>&lt;P&gt;Your notmatched dataset will have incorrect non-missing values for fund_ticker and name; you should set those to missing before the OUTPUT, or drop the variables with a dataset option in the first place.&lt;/P&gt;</description>
      <pubDate>Thu, 25 Jun 2020 08:16:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/one-to-many-merge-does-not-work-as-expected/m-p/664901#M198699</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-06-25T08:16:43Z</dc:date>
    </item>
    <item>
      <title>Re: one to many merge does not work as expected</title>
      <link>https://communities.sas.com/t5/SAS-Programming/one-to-many-merge-does-not-work-as-expected/m-p/664906#M198703</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;thanks a million!&amp;nbsp; I've updated the code.&lt;/P&gt;</description>
      <pubDate>Thu, 25 Jun 2020 08:31:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/one-to-many-merge-does-not-work-as-expected/m-p/664906#M198703</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2020-06-25T08:31:57Z</dc:date>
    </item>
    <item>
      <title>Re: one to many merge does not work as expected</title>
      <link>https://communities.sas.com/t5/SAS-Programming/one-to-many-merge-does-not-work-as-expected/m-p/664907#M198704</link>
      <description>&lt;P&gt;"Given enough eyeballs, all bugs are shallow." (Linus's law, formulated by Eric S. Raymond)&lt;/P&gt;</description>
      <pubDate>Thu, 25 Jun 2020 08:36:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/one-to-many-merge-does-not-work-as-expected/m-p/664907#M198704</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-06-25T08:36:32Z</dc:date>
    </item>
    <item>
      <title>Re: one to many merge does not work as expected</title>
      <link>https://communities.sas.com/t5/SAS-Programming/one-to-many-merge-does-not-work-as-expected/m-p/664910#M198707</link>
      <description>&lt;P&gt;I'm calling it "programming on 4 hands" and I'm practicing it as often as I can during production programing.&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Thu, 25 Jun 2020 08:43:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/one-to-many-merge-does-not-work-as-expected/m-p/664910#M198707</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2020-06-25T08:43:50Z</dc:date>
    </item>
  </channel>
</rss>

