<?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: How to join a Table to another Table with two conditions? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-join-a-Table-to-another-Table-with-two-conditions/m-p/719871#M222941</link>
    <description>&lt;P&gt;Please explain the logic that allows you to go from the input data set to the output data set.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Could you also please provide the data as DATA step code instead of columns of data?&lt;/P&gt;</description>
    <pubDate>Wed, 17 Feb 2021 11:42:07 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2021-02-17T11:42:07Z</dc:date>
    <item>
      <title>How to join a Table to another Table with two conditions?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-join-a-Table-to-another-Table-with-two-conditions/m-p/719869#M222940</link>
      <description>&lt;P&gt;Hi all SAS Users,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;From my understanding, we join or match two datasets mainly by using the proc SQL (inner, right, and left-join).&lt;/P&gt;
&lt;P&gt;However, my case below does not work on that way.&lt;/P&gt;
&lt;P&gt;I have two dataset called: &lt;STRONG&gt;winsorize&lt;/STRONG&gt; and&amp;nbsp;&lt;STRONG&gt;industry_return. &lt;/STRONG&gt;The simplified versions of these two datasets are as below:&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;winsorize&lt;/STRONG&gt;&lt;CODE class=" language-sas"&gt;
&lt;/CODE&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Type    INDC3       Year   var1    var2
AXX     CNSTM       1994   0.01    0.08
MAS     CNSTM       1996   0.05    .
RAF     CNSTM       1994   0.07    0.08
AGG     CNSTM       2004   0.07    .
CCC     CNSTM       1996   0.02    .
RBB     ENEGY       1998   0.05    0.88
RFB     ENEGY       1999   0.06    0.89
TYB     ENEGY       2018   0.05    0.48&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;industry_return&lt;/STRONG&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;INDC3     Year    var3
CNSTM     1988&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;1.2
CNTSM&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;1989&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;1.3
CNTSM&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;1990&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;1.5
CNTSM&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;1994&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;1.1
CNTSM&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;1996&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;1.7
CNTSM&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;2004&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;1.9
CNTSM&amp;nbsp;    2018    2.1
ENEGY &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;1988&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;3.3
ENEGY &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;1996&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;3.5
ENEGY     1998&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;3.9
ENEGY     1999&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;4.2
ENEGY     2018&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;4.4&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want a results called "&lt;STRONG&gt;matching&lt;/STRONG&gt;"&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Type    INDC3          Year   var1    var2       var3
AXX     CNSTM          1994   0.01    0.08        1.1
MAS     CNSTM          1996   0.05    .           1.7
RAF     CNSTM          1994   0.07    0.08        1.1
AGG     CNSTM          2004   0.07    .           1.9
CCC     CNSTM          1996   0.02    .           1.7
RBB     ENEGY          1998   0.05    0.88        3.9
RFB     ENEGY          1999   0.06    0.89        4.2
TYB     ENEGY          2018   0.05    0.48&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;4.4&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And I see that this match seems not to be able to be done by using three join styles of proc sql.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Could you please suggest to me how to sort it out?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Warmest regards.&lt;/P&gt;</description>
      <pubDate>Wed, 17 Feb 2021 11:35:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-join-a-Table-to-another-Table-with-two-conditions/m-p/719869#M222940</guid>
      <dc:creator>Phil_NZ</dc:creator>
      <dc:date>2021-02-17T11:35:01Z</dc:date>
    </item>
    <item>
      <title>Re: How to join a Table to another Table with two conditions?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-join-a-Table-to-another-Table-with-two-conditions/m-p/719871#M222941</link>
      <description>&lt;P&gt;Please explain the logic that allows you to go from the input data set to the output data set.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Could you also please provide the data as DATA step code instead of columns of data?&lt;/P&gt;</description>
      <pubDate>Wed, 17 Feb 2021 11:42:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-join-a-Table-to-another-Table-with-two-conditions/m-p/719871#M222941</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-02-17T11:42:07Z</dc:date>
    </item>
    <item>
      <title>Re: How to join a Table to another Table with two conditions?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-join-a-Table-to-another-Table-with-two-conditions/m-p/719876#M222944</link>
      <description>&lt;P&gt;Perhaps I'm missing something when skimming through your data, but this should work with a single join, using two columns in the join condition (INDC3 and Year)?&lt;/P&gt;</description>
      <pubDate>Wed, 17 Feb 2021 11:57:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-join-a-Table-to-another-Table-with-two-conditions/m-p/719876#M222944</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2021-02-17T11:57:35Z</dc:date>
    </item>
    <item>
      <title>Re: How to join a Table to another Table with two conditions?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-join-a-Table-to-another-Table-with-two-conditions/m-p/719879#M222946</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/212695"&gt;@Phil_NZ&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I agree with &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13674"&gt;@LinusH&lt;/a&gt;. Only if the sort order of &lt;FONT face="courier new,courier"&gt;matching&lt;/FONT&gt; must be the same as that of &lt;FONT face="courier new,courier"&gt;winsorize&lt;/FONT&gt;&amp;nbsp;and the real data don't contain a suitable sort key, you may need to add a sequence number in a preliminary DATA step as shown below.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data winsorize;
input Type $ INDC3 $ Year var1 var2;
cards;
AXX CNSTM 1994 0.01 0.08
MAS CNSTM 1996 0.05 .
RAF CNSTM 1994 0.07 0.08
AGG CNSTM 2004 0.07 .
CCC CNSTM 1996 0.02 .
RBB ENEGY 1998 0.05 0.88
RFB ENEGY 1999 0.06 0.89
TYB ENEGY 2018 0.05 0.48
;

data industry_return;
input INDC3 $ Year var3;
cards;
CNSTM 1988 1.2
CNTSM 1989 1.3
CNTSM 1990 1.5
CNTSM 1994 1.1
CNTSM 1996 1.7
CNTSM 2004 1.9
CNTSM 2018 2.1
ENEGY 1988 3.3
ENEGY 1996 3.5
ENEGY 1998 3.9
ENEGY 1999 4.2
ENEGY 2018 4.4
;

data _wins / view=_wins;
set winsorize;
_seqno=_n_;
run;

proc sql;
create table matching(drop=_seqno) as
select a.*, var3
from _wins a left join industry_return b
on spedis(a.indc3, b.indc3)&amp;lt;=10 &amp;amp; a.year=b.year
order by _seqno;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;(Increase the "tolerance" 10 in the ON condition if there are more severe typos (?) in INDC3 than just "&lt;FONT face="courier new,courier"&gt;CN&lt;STRONG&gt;ST&lt;/STRONG&gt;M&lt;/FONT&gt;" vs. "&lt;FONT face="courier new,courier"&gt;CN&lt;STRONG&gt;TS&lt;/STRONG&gt;M&lt;/FONT&gt;", but make sure that no actual mismatches are tolerated.)&lt;/P&gt;</description>
      <pubDate>Wed, 17 Feb 2021 12:41:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-join-a-Table-to-another-Table-with-two-conditions/m-p/719879#M222946</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2021-02-17T12:41:06Z</dc:date>
    </item>
    <item>
      <title>Re: How to join a Table to another Table with two conditions?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-join-a-Table-to-another-Table-with-two-conditions/m-p/719911#M222958</link>
      <description>&lt;P&gt;You have typos in your industry_return dataset (CNTSM instead of CNSTM).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Otherwise, this is a classic example for doing a hash lookup:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data winsorize;
input Type $ INDC3 $ Year var1 var2;
datalines;
AXX     CNSTM       1994   0.01    0.08
MAS     CNSTM       1996   0.05    .
RAF     CNSTM       1994   0.07    0.08
AGG     CNSTM       2004   0.07    .
CCC     CNSTM       1996   0.02    .
RBB     ENEGY       1998   0.05    0.88
RFB     ENEGY       1999   0.06    0.89
TYB     ENEGY       2018   0.05    0.48
;

data industry_return;
input INDC3 $  Year var3;
datalines;
CNSTM     1988    1.2
CNSTM     1989    1.3
CNSTM     1990    1.5
CNSTM     1994    1.1
CNSTM     1996    1.7
CNSTM     2004    1.9
CNSTM     2018    2.1
ENEGY     1988    3.3
ENEGY     1996    3.5
ENEGY     1998    3.9
ENEGY     1999    4.2
ENEGY     2018    4.4
;

data want;
set winsorize;
if _n_ = 1 then do;
  length var3 8;
  declare hash i (dataset:"industry_return");
  i.definekey("INDC3","year");
  i.definedata("var3");
  i.definedone();
end;
if i.find() ne 0 then var3 = .;
run;

proc print data=want noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;Type	INDC3	Year	var1	var2	var3
AXX	CNSTM	1994	0.01	0.08	1.1
MAS	CNSTM	1996	0.05	.	1.7
RAF	CNSTM	1994	0.07	0.08	1.1
AGG	CNSTM	2004	0.07	.	1.9
CCC	CNSTM	1996	0.02	.	1.7
RBB	ENEGY	1998	0.05	0.88	3.9
RFB	ENEGY	1999	0.06	0.89	4.2
TYB	ENEGY	2018	0.05	0.48	4.4
&lt;/PRE&gt;</description>
      <pubDate>Wed, 17 Feb 2021 14:35:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-join-a-Table-to-another-Table-with-two-conditions/m-p/719911#M222958</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-02-17T14:35:52Z</dc:date>
    </item>
    <item>
      <title>Re: How to join a Table to another Table with two conditions?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-join-a-Table-to-another-Table-with-two-conditions/m-p/720027#M223009</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;&amp;nbsp;,&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;,&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13674"&gt;@LinusH&lt;/a&gt;&amp;nbsp;, and&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Sorry it is totally my typos, so when I edit my data consistently, whether the changed line in the code below is correct, I just wanna cross-check:&lt;/P&gt;
&lt;P&gt;Simplistically speaking, yes, I match based on &lt;STRONG&gt;INDC3&lt;/STRONG&gt; and &lt;STRONG&gt;Year&lt;/STRONG&gt;.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data winsorize;
input Type $ INDC3 $ Year var1 var2;
cards;
AXX CNSTM 1994 0.01 0.08
MAS CNSTM 1996 0.05 .
RAF CNSTM 1994 0.07 0.08
AGG CNSTM 2004 0.07 .
CCC CNSTM 1996 0.02 .
RBB ENEGY 1998 0.05 0.88
RFB ENEGY 1999 0.06 0.89
TYB ENEGY 2018 0.05 0.48
;

data industry_return;
input INDC3 $ Year var3;
cards;
CNSTM 1988 1.2
CNSTM 1989 1.3
CNSTM 1990 1.5
CNSTM 1994 1.1
CNSTM 1996 1.7
CNSTM 2004 1.9
CNSTM 2018 2.1
ENEGY 1988 3.3
ENEGY 1996 3.5
ENEGY 1998 3.9
ENEGY 1999 4.2
ENEGY 2018 4.4
;

data _wins / view=_wins;
set winsorize;
_seqno=_n_;
run;

proc sql;
create table matching(drop=_seqno) as
select a.*, var3
from _wins a left join industry_return b
on a.indc3 = b.indc3 &amp;amp; a.year=b.year /*changed line*/
order by _seqno;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Apart from that, I do not know what is the role and purpose of these lines of code in my case???&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _wins / view=_wins;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;_seqno=_n_;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Warm regards.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 17 Feb 2021 20:31:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-join-a-Table-to-another-Table-with-two-conditions/m-p/720027#M223009</guid>
      <dc:creator>Phil_NZ</dc:creator>
      <dc:date>2021-02-17T20:31:08Z</dc:date>
    </item>
    <item>
      <title>Re: How to join a Table to another Table with two conditions?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-join-a-Table-to-another-Table-with-two-conditions/m-p/720040#M223013</link>
      <description>&lt;P&gt;Yes, with consistent data, of course, you don't need functions (like SPEDIS) supporting "fuzzy" comparisons and the criterion regarding INDC3 simplifies to &lt;FONT face="courier new,courier"&gt;a.indc3=b.indc3&lt;/FONT&gt;. I introduced the sequence number &lt;FONT face="courier new,courier"&gt;_seqno&lt;/FONT&gt; just to ensure that datasets&amp;nbsp;&lt;FONT face="courier new,courier"&gt;winsorize&lt;/FONT&gt; and&amp;nbsp;&lt;FONT face="courier new,courier"&gt;matching&lt;/FONT&gt;&amp;nbsp;have the same sort order.&amp;nbsp;If sort order doesn't matter, you should omit the DATA step creating the view &lt;FONT face="courier new,courier"&gt;_wins&lt;/FONT&gt; and use dataset&amp;nbsp;&lt;FONT face="courier new,courier"&gt;winsorize&lt;/FONT&gt;&amp;nbsp;instead in the PROC SQL step (as this will improve performance and simplify the code):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table matching as
select a.*, var3
from winsorize a left join industry_return b
on a.indc3=b.indc3 &amp;amp; a.year=b.year;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I used a &lt;EM&gt;view&lt;/EM&gt; rather than a dataset to create the temporary sort key&amp;nbsp;&lt;FONT face="courier new,courier"&gt;_seqno&lt;/FONT&gt; in order to save disk space (of about the size occupied by dataset &lt;FONT face="courier new,courier"&gt;winsorize&lt;/FONT&gt;).&lt;/P&gt;</description>
      <pubDate>Wed, 17 Feb 2021 21:19:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-join-a-Table-to-another-Table-with-two-conditions/m-p/720040#M223013</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2021-02-17T21:19:09Z</dc:date>
    </item>
    <item>
      <title>Re: How to join a Table to another Table with two conditions?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-join-a-Table-to-another-Table-with-two-conditions/m-p/720042#M223015</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you very much for your explanation.&lt;/P&gt;
&lt;P&gt;Can I ask, so "&lt;STRONG&gt;same sort of order&lt;/STRONG&gt;" means &lt;STRONG&gt;similar in "ascending or descending" order&lt;/STRONG&gt; , is not it?&lt;/P&gt;
&lt;P&gt;I could not get the idea "&lt;SPAN&gt;&amp;nbsp;sort order doesn't matter", because I think SAS will automatically match from Table b with b based on the match in "INDC3" and "year". And in PROC SQL, we do not need to care about the sort or else?&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Please let me know if I fall into any fallacy.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Cheers.&lt;/P&gt;
&lt;P&gt;P/S: I also have some "trials and errors"test, when applying your code with&lt;STRONG&gt; _seqno&lt;/STRONG&gt;, the order of observations in table matching following that of&lt;STRONG&gt;&amp;nbsp;winsorize&lt;/STRONG&gt;. Without applying &lt;STRONG&gt;_seqno&lt;/STRONG&gt;, the order of dataset matching similar to that of &lt;STRONG&gt;industry_return&lt;/STRONG&gt;. Interesting to me.&lt;/P&gt;</description>
      <pubDate>Wed, 17 Feb 2021 21:42:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-join-a-Table-to-another-Table-with-two-conditions/m-p/720042#M223015</guid>
      <dc:creator>Phil_NZ</dc:creator>
      <dc:date>2021-02-17T21:42:03Z</dc:date>
    </item>
    <item>
      <title>Re: How to join a Table to another Table with two conditions?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-join-a-Table-to-another-Table-with-two-conditions/m-p/720059#M223021</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/212695"&gt;@Phil_NZ&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Let me quote your initial post:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/212695"&gt;@Phil_NZ&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I want a results called "&lt;STRONG&gt;matching&lt;/STRONG&gt;"&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Type    INDC3          Year   var1    var2       var3
AXX     CNSTM          1994   0.01    0.08        1.1
MAS     CNSTM          1996   0.05    .           1.7
RAF     CNSTM          1994   0.07    0.08        1.1
AGG     CNSTM          2004   0.07    .           1.9
CCC     CNSTM          1996   0.02    .           1.7
RBB     ENEGY          1998   0.05    0.88        3.9
RFB     ENEGY          1999   0.06    0.89        4.2
TYB     ENEGY          2018   0.05    0.48&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;4.4&lt;/CODE&gt;&lt;/PRE&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;When I used dataset &lt;FONT face="courier new,courier"&gt;winsorize&lt;/FONT&gt; instead of view &lt;FONT face="courier new,courier"&gt;_wins&lt;/FONT&gt; in the PROC SQL step (and no ORDER BY clause and no reference to variable &lt;FONT face="courier new,courier"&gt;_seqno&lt;/FONT&gt;) I obtained:&lt;/P&gt;
&lt;PRE&gt;Type    INDC3    Year    var1    var2    var3

RAF     CNSTM    1994    0.07    0.08     1.1
AXX     CNSTM    1994    0.01    0.08     1.1
CCC     CNSTM    1996    0.02     .       1.7
MAS     CNSTM    1996    0.05     .       1.7
RBB     ENEGY    1998    0.05    0.88     3.9
RFB     ENEGY    1999    0.06    0.89     4.2
AGG     CNSTM    2004    0.07     .       1.9
TYB     ENEGY    2018    0.05    0.48     4.4&lt;/PRE&gt;
&lt;P&gt;As you see, the order of observations is different from the result you wanted (which is typical for PROC SQL and SQL in general when you don't use an ORDER BY clause or certain other language elements), making it harder to check if the result meets your requirements. But if that's not a problem, i.e., "sort order [&lt;EM&gt;of the result&lt;/EM&gt;] doesn't matter," then forget about &lt;FONT face="courier new,courier"&gt;_wins&lt;/FONT&gt; and &lt;FONT face="courier new,courier"&gt;_seqno&lt;/FONT&gt;. For the join PROC SQL doesn't need any specific sort order.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that table "&lt;FONT color="#808080"&gt;&lt;STRONG&gt;matching&lt;/STRONG&gt;&lt;/FONT&gt;" is &lt;EM&gt;not&lt;/EM&gt; sorted by any obvious &lt;EM&gt;unique&lt;/EM&gt; combination of available sort keys (e.g.,&amp;nbsp;&lt;FONT face="courier new,courier"&gt;INDC3, Type&lt;/FONT&gt; or &lt;FONT face="courier new,courier"&gt;INDC3, Year, var1&lt;/FONT&gt; or &lt;FONT face="courier new,courier"&gt;descending &lt;EM&gt;anything&lt;/EM&gt;&lt;/FONT&gt;). So, to obtain the exact table&amp;nbsp;"&lt;FONT color="#808080"&gt;&lt;STRONG&gt;matching&lt;/STRONG&gt;&lt;/FONT&gt;" from PROC SQL there's almost no other option than to create a new sort key such as &lt;FONT face="courier new,courier"&gt;_seqno&lt;/FONT&gt;, based on dataset &lt;FONT face="courier new,courier"&gt;winsorize&lt;/FONT&gt;.&lt;/P&gt;</description>
      <pubDate>Wed, 17 Feb 2021 22:34:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-join-a-Table-to-another-Table-with-two-conditions/m-p/720059#M223021</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2021-02-17T22:34:18Z</dc:date>
    </item>
  </channel>
</rss>

