<?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: Look up or Joins? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Look-up-or-Joins/m-p/592976#M170103</link>
    <description>May I know how to amend the logic for non matching cases? One simple&lt;BR /&gt;example should be OK&lt;BR /&gt;</description>
    <pubDate>Tue, 01 Oct 2019 10:46:32 GMT</pubDate>
    <dc:creator>David_Billa</dc:creator>
    <dc:date>2019-10-01T10:46:32Z</dc:date>
    <item>
      <title>Look up or Joins?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Look-up-or-Joins/m-p/592958#M170091</link>
      <description>&lt;P&gt;Assume I've two datasets&amp;nbsp;Have1 and Have2. In&amp;nbsp;Have1 dataset I've two variables ERR_MSG_TXT and FLT_ID. In&amp;nbsp;Have2 dataset I've two variables called RUN_ID and STAT_CD.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Now I want to create a new dataset&amp;nbsp;WANT which should have a variables called RUN_ID and FLT_ID.So in the new dataset WANT, I've to look for ERR_MSG_TXT in dataset&amp;nbsp;Have1 and STAT_CD variable in dataset Have2. If it matches, then I've to get the respective variables RUN_ID and FLT_ID from datasets&amp;nbsp;Have1 and Have2. I'm not certain which method&amp;nbsp;(look up or join?) works better here. Appericiate if someone of you help me with the code to accomplish this Task.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Dataset Have1:&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;STRONG&gt;FLT_ID&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;ERR_MSG_TXT&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;148&lt;/TD&gt;&lt;TD&gt;Syntax error&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;149&lt;/TD&gt;&lt;TD&gt;Not a valid procedure&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;150&lt;/TD&gt;&lt;TD&gt;Warning&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;Dataset Have2:&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;STRONG&gt;RUN_ID&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;STAT_CD&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;Syntax error&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;Warning&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;Not a valid procedure&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&lt;EM&gt;&lt;U&gt;&lt;STRONG&gt;Desired Output: Dataset WANT&lt;/STRONG&gt;&lt;/U&gt;&lt;/EM&gt;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;STRONG&gt;RUN_ID&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;FLT_ID&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;148&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;150&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;149&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 01 Oct 2019 09:32:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Look-up-or-Joins/m-p/592958#M170091</guid>
      <dc:creator>David_Billa</dc:creator>
      <dc:date>2019-10-01T09:32:20Z</dc:date>
    </item>
    <item>
      <title>Re: Look up or Joins?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Look-up-or-Joins/m-p/592959#M170092</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/292396"&gt;@David_Billa&lt;/a&gt;&amp;nbsp;hi and welcome to the SAS Community &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There are multiple ways to do this, but here is one&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have1;
input FLT_ID ERR_MSG_TXT $50.;
datalines;
148 Syntax error
149 Not a valid procedure
150 Warning
;

data have2;
input RUN_ID STAT_CD $50.;
datalines;
1 Syntax error
2 Warning
3 Not a valid procedure
;

data want(keep=RUN_ID FLT_ID);
    if _N_=1 then do;
        declare hash h(dataset:'have1(rename=(ERR_MSG_TXT=STAT_CD))');
        h.definekey('STAT_CD');
        h.definedata('FLT_ID');
        h.definedone();
    end;

    set have2;
    FLT_ID=.;

    rc=h.find();
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 01 Oct 2019 09:39:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Look-up-or-Joins/m-p/592959#M170092</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-10-01T09:39:07Z</dc:date>
    </item>
    <item>
      <title>Re: Look up or Joins?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Look-up-or-Joins/m-p/592964#M170095</link>
      <description>&lt;P&gt;Great! May I know why you've assigned missing value for FLT_ID?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Also could you please tell me about the method using join to complete this task? If not join, then using any other method.&lt;/P&gt;</description>
      <pubDate>Tue, 01 Oct 2019 09:59:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Look-up-or-Joins/m-p/592964#M170095</guid>
      <dc:creator>David_Billa</dc:creator>
      <dc:date>2019-10-01T09:59:49Z</dc:date>
    </item>
    <item>
      <title>Re: Look up or Joins?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Look-up-or-Joins/m-p/592974#M170102</link>
      <description>&lt;P&gt;Here a few more ways how this can be done.&lt;/P&gt;
&lt;P&gt;Please note: In the moment there is a 1:1 relationship between the tables in your sample data. If that's different and especially if there are also non-matching cases possible then you will eventually need to amend the logic to get to your desired result.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have1;
input FLT_ID ERR_MSG_TXT $50.;
datalines;
148 Syntax error
149 Not a valid procedure
150 Warning
;

data have2;
input RUN_ID STAT_CD $50.;
datalines;
1 Syntax error
2 Warning
3 Not a valid procedure
;

/*** option 1 ***/
proc sql;
/*  create table want1 as*/
  select r.run_id, l.flt_id
  from have1 l inner join have2 r
  on l.ERR_MSG_TXT=r.STAT_CD
  ;
quit;

/*** option 2 ***/
/* create format from data set */
data cntl;
  set have1(rename=(ERR_MSG_TXT=start FLT_ID=label));
  retain fmtname '$FLT_ID' type 'c';
run;
proc format cntlin=cntl;
run;

/* option 2, variant 1: Use format to populate a new variable */
data want2;
  set have2;
  FLT_ID=put(STAT_CD,$FLT_ID3.);
  keep run_id FLT_ID;
run;
proc print;
run;

/* option 2, variant 2: Apply format for printing values of existing variable */
proc print data=have2;
  format STAT_CD $FLT_ID3.;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 01 Oct 2019 10:24:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Look-up-or-Joins/m-p/592974#M170102</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2019-10-01T10:24:12Z</dc:date>
    </item>
    <item>
      <title>Re: Look up or Joins?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Look-up-or-Joins/m-p/592976#M170103</link>
      <description>May I know how to amend the logic for non matching cases? One simple&lt;BR /&gt;example should be OK&lt;BR /&gt;</description>
      <pubDate>Tue, 01 Oct 2019 10:46:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Look-up-or-Joins/m-p/592976#M170103</guid>
      <dc:creator>David_Billa</dc:creator>
      <dc:date>2019-10-01T10:46:32Z</dc:date>
    </item>
    <item>
      <title>Re: Look up or Joins?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Look-up-or-Joins/m-p/592980#M170107</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/292396"&gt;@David_Billa&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;May I know how to amend the logic for non matching cases? One simple&lt;BR /&gt;example should be OK&lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;That really depends how your desired result needs to look like, i.e. do you want to include or exclude non-matching cases, do you want to include all non matching rows from one table or from both ...and so on.&lt;/P&gt;
&lt;P&gt;Using the code already provided why don't you give it a go yourself first and then eventually come back and ask targeted questions for things where you get stuck with your code.&lt;/P&gt;
&lt;P&gt;You need of course first to create additional sample data for such cases.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;With SQL: Look up in the documentation SQL Left Join, Right Join, Full Outer Join.... and for formats how to add an OTHER case&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 01 Oct 2019 11:01:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Look-up-or-Joins/m-p/592980#M170107</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2019-10-01T11:01:27Z</dc:date>
    </item>
    <item>
      <title>Re: Look up or Joins?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Look-up-or-Joins/m-p/592995#M170113</link>
      <description>&lt;P&gt;Yet another way to do this, and to get the non-matches output as well.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Assuming that HAVE1 is your lookup dataset, and HAVE2 is the result of various runs, which you want to check:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have1;
input FLT_ID ERR_MSG_TXT $50.;
datalines;
148 Syntax error
149 Not a valid procedure
150 Warning
;

data have2;
input RUN_ID STAT_CD $50.;
datalines;
1 Syntax error
2 Warning
3 Not a valid procedure
;

proc sql;
  create index ERR_MSG_TXT on have1(ERR_MSG_TXT);
quit;

data want(keep=RUN_ID FLT_ID) notfound(keep=RUN_ID STAT_CD);
  set have2;
  ERR_MSG_TXT=STAT_CD;
  set have1 key=ERR_MSG_TXT/unique;
  if _iorc_ then do;
    output notfound;
    _error_=0;
    end;
  else output want;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you omit the "_error_=0" line, you get a message in the log when a value is not in the lookup dataset, which may be nice.&lt;/P&gt;</description>
      <pubDate>Tue, 01 Oct 2019 11:37:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Look-up-or-Joins/m-p/592995#M170113</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2019-10-01T11:37:12Z</dc:date>
    </item>
    <item>
      <title>Re: Look up or Joins?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Look-up-or-Joins/m-p/593082#M170144</link>
      <description>&lt;P&gt;Left join can handle mismatches in a reasonable manner:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have1;
input FLT_ID ERR_MSG_TXT $50.;
datalines;
148 Syntax error
149 Not a valid procedure
150 Warning
999 Unknown message
;

data have2;
input RUN_ID STAT_CD $50.;
datalines;
1 Syntax error
2 Warning
3 Not a valid procedure
4 Bravo!
;

proc sql;
create table want as
select 
	a.RUN_ID,
	coalesce(b.FLT_ID, 999) as FLT_ID
from 
	have2 as a left join 
	have1 as b on b.ERR_MSG_TXT = a.STAT_CD;
select * from want;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;RUN_ID 	FLT_ID
4 	999
3 	149
1 	148
2 	150&lt;/PRE&gt;</description>
      <pubDate>Tue, 01 Oct 2019 16:06:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Look-up-or-Joins/m-p/593082#M170144</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2019-10-01T16:06:01Z</dc:date>
    </item>
  </channel>
</rss>

