<?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: Conditional matching of two tables with different variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Conditional-matching-of-two-tables-with-different-variables/m-p/788816#M252315</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15475"&gt;@andreas_lds&lt;/a&gt;&amp;nbsp;Coalesce is one of the functions also available in data bases and though SAS can push this function to the DB. Which functions can get pushed will depend on the DB and its version and is documented. As an example &lt;A href="https://go.documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/acreldb/p0f64yzzxbsg8un1uwgstc6fivjd.htm" target="_self"&gt;here&lt;/A&gt; for Oracle.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The data step syntax you shared would drop observations which I assume is not what the OP wants. Below how I believe it should look like.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  /* defining the hash */
  if _n_ = 1 then
    do;
      if 0 then set rawcode(keep=code);
      declare hash h(dataset: 'rawcode');
      h.defineKey('code');
      h.defineData('code');
      h.defineDone();
    end;
  call missing(of _all_);

  set ab_code;

  /* the find-method returns 0 if something is found */
  if h.find(key: bcode) ne 0 then code=acode;

  drop acode bcode;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 07 Jan 2022 10:00:35 GMT</pubDate>
    <dc:creator>Patrick</dc:creator>
    <dc:date>2022-01-07T10:00:35Z</dc:date>
    <item>
      <title>Conditional matching of two tables with different variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-matching-of-two-tables-with-different-variables/m-p/788726#M252252</link>
      <description>&lt;P&gt;I have two data sets. One data set contains a variable called code, which is the product code. The other data set has variable acode (for eastern US stores) and bcode (for western US stores), along with the item's description. acode usually is not the same as bocde.&lt;/P&gt;&lt;P&gt;Below is a trivial example.&lt;/P&gt;&lt;PRE&gt;data rawcode; input $3 code;
cards;
001  
026  
099  
126  
523  
525  &lt;BR /&gt;;

data ab_code; input $3 acode $3 bcode $7 desc;
cards;
001 001  chairs
027 026  cups
099 027  plates
126 098  pencils
399  112   books
400  113   pens
522  523  clips
525  525  pillows
;&lt;/PRE&gt;&lt;P&gt;What I want to do is to see if code in rawcode matches a value for bcode in ab_code. If so, then code will not change, but if it doesn't then I want to replace it with the acode value. The final data set would look like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;code&amp;nbsp; desc&lt;/P&gt;&lt;P&gt;001&amp;nbsp; &amp;nbsp;chairs&lt;/P&gt;&lt;P&gt;026&amp;nbsp; &amp;nbsp;cups&lt;/P&gt;&lt;P&gt;099&amp;nbsp; &amp;nbsp;plates&lt;/P&gt;&lt;P&gt;126&amp;nbsp; &amp;nbsp;pencils&lt;/P&gt;&lt;P&gt;523&amp;nbsp; &amp;nbsp;clips&lt;/P&gt;&lt;P&gt;525&amp;nbsp; &amp;nbsp;pillows&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Rather than do a bunch of sorts and merges, or use a hash table, is there a simple way to accomplish this using POC SQL?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Andrew&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;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 06 Jan 2022 19:12:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-matching-of-two-tables-with-different-variables/m-p/788726#M252252</guid>
      <dc:creator>DocMartin</dc:creator>
      <dc:date>2022-01-06T19:12:19Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional matching of two tables with different variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-matching-of-two-tables-with-different-variables/m-p/788729#M252255</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data rawcode; 
input  code $3.;
cards;
001  
026  
099  
126  
523  
525  
;

data ab_code; 
input  acode :$3. bcode :$7. desc :$10.;
cards;
001 001  chairs
027 026  cups
099 027  plates
126 098  pencils
399  112   books
400  113   pens
522  523  clips
525  525  pillows
;

proc sql;
  create table want as
  select a.code, coalesce(b.desc, c.desc) as desc
  from rawcode a left join ab_code b
  on a.code=b.bcode
  left join ab_code c
  on a.code=c.acode;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I hope the above helps&lt;/P&gt;</description>
      <pubDate>Thu, 06 Jan 2022 19:57:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-matching-of-two-tables-with-different-variables/m-p/788729#M252255</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2022-01-06T19:57:12Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional matching of two tables with different variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-matching-of-two-tables-with-different-variables/m-p/788732#M252258</link>
      <description>&lt;P&gt;Thanks! I didn't know about the coalesce function in SQL.&lt;/P&gt;</description>
      <pubDate>Thu, 06 Jan 2022 20:13:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-matching-of-two-tables-with-different-variables/m-p/788732#M252258</guid>
      <dc:creator>DocMartin</dc:creator>
      <dc:date>2022-01-06T20:13:00Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional matching of two tables with different variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-matching-of-two-tables-with-different-variables/m-p/788807#M252310</link>
      <description>&lt;P&gt;coalesce is a normal sas function, that can also be used in proc sql, but afaik only if the sql is not passed to the database.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Imho a data step is almost always easier to read than any proc sql, i would use a hash-object&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
	if 0 then set rawcode; /* load variable(s) defined in the dataset used as source for the hash */
	
	set ab_code;
	
    /* defining the hash */
	if _n_ = 1 then do;
		declare hash h(dataset: 'rawcode');
		h.defineKey('code');
		h.defineDone();		
	end;
	
    /* the find-method returns 0 if something is found */
	if h.find(key: bcode) = 0 then output; 
	else if h.find(key: acode) = 0 then output;
	
	drop acode bcode;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 07 Jan 2022 06:49:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-matching-of-two-tables-with-different-variables/m-p/788807#M252310</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2022-01-07T06:49:05Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional matching of two tables with different variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-matching-of-two-tables-with-different-variables/m-p/788814#M252314</link>
      <description>&lt;PRE&gt;data rawcode; 
input  code $3.;
cards;
001  
026  
099  
126  
523  
525  
;

data ab_code; 
input  acode :$3. bcode :$7. desc :$10.;
cards;
001 001  chairs
027 026  cups
099 027  plates
126 098  pencils
399  112   books
400  113   pens
522  523  clips
525  525  pillows
;
run; 
  
proc sql;
 create table want as 
  select c.code, ab.desc
   from rawcode C
   inner join ab_code ab 
   on (c.code =ab.acode  or  c.code =ab.bcode);
quit;
&lt;/PRE&gt;</description>
      <pubDate>Fri, 07 Jan 2022 09:31:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-matching-of-two-tables-with-different-variables/m-p/788814#M252314</guid>
      <dc:creator>AndreaVianello</dc:creator>
      <dc:date>2022-01-07T09:31:15Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional matching of two tables with different variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-matching-of-two-tables-with-different-variables/m-p/788816#M252315</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15475"&gt;@andreas_lds&lt;/a&gt;&amp;nbsp;Coalesce is one of the functions also available in data bases and though SAS can push this function to the DB. Which functions can get pushed will depend on the DB and its version and is documented. As an example &lt;A href="https://go.documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/acreldb/p0f64yzzxbsg8un1uwgstc6fivjd.htm" target="_self"&gt;here&lt;/A&gt; for Oracle.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The data step syntax you shared would drop observations which I assume is not what the OP wants. Below how I believe it should look like.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  /* defining the hash */
  if _n_ = 1 then
    do;
      if 0 then set rawcode(keep=code);
      declare hash h(dataset: 'rawcode');
      h.defineKey('code');
      h.defineData('code');
      h.defineDone();
    end;
  call missing(of _all_);

  set ab_code;

  /* the find-method returns 0 if something is found */
  if h.find(key: bcode) ne 0 then code=acode;

  drop acode bcode;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 07 Jan 2022 10:00:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-matching-of-two-tables-with-different-variables/m-p/788816#M252315</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2022-01-07T10:00:35Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional matching of two tables with different variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-matching-of-two-tables-with-different-variables/m-p/789752#M252763</link>
      <description>&lt;P&gt;I like your solution. However, mea culpa, I framed the problem way too simply. Here's what I really want.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hospitalized patients can have a diagnosis coded in their medical record in one of two ways: as an APRDRG or an MSDRG.&amp;nbsp; These are stored in the tables “APR” and ‘MS”. &amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;* Tables apr and ms;

data apr; input @1 diagnosis $21. @25 aprdrg $3.;
cards;
Heart_Failure	        001
Myocardial_Infarction   002
Stroke	                103
Respiratory_Failure	
GI_Bleeding	            116
GI_Cancer	            167
GI_Obstruction          172
Influenza	            298
Head_Trauma	            300
;
run;


data ms; input @1 diagnosis $21. @25 msdrg $3.;
cards;
Heart_Failure           001
Myocardial_Infarction	005
Stroke	                098
Respiratory_Failure	    103
GI_Bleeding	            112
GI_Cancer	            
Asthma                  174
Influenza	            298
Head_Trauma	            300
Pelvic_Fractue          302
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The main patient data set is :&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data main; input @1 ptid $3.  @5 code $3. @9 drg_type $1.;
cards;
111	002	A
112	103	A
113	112	A
114	298	A
115	300	A
116	005	M
117	103	M
118	112	M
119	167	M
120	222	M
;
run;  &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The final table I'd like to get is:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data final; infile @1 PatientID $3.	@5 drg_type $1. @7 code $3 @11	Diagnosis $21.;
cards;
111	A 002  Myocardial_Infarction
112	A 103  Stroke
113	M 112  GI_Bleeding
114	A 298  Influenza
115	A 300  Head_Trauma
116	M 005  Myocardial_Infarction
117	M 103  Respiratory_Failure
118	M 112  GI_Bleeding
119	A 167  GI_Cancer
120	  222   
;	&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Any help is greatly appreciated!&lt;/P&gt;&lt;P&gt;Thanks.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jan 2022 15:56:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-matching-of-two-tables-with-different-variables/m-p/789752#M252763</guid>
      <dc:creator>DocMartin</dc:creator>
      <dc:date>2022-01-12T15:56:28Z</dc:date>
    </item>
  </channel>
</rss>

