<?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: Join two datasets with different structure (similar to vlookup) in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Join-two-datasets-with-different-structure-similar-to-vlookup/m-p/936268#M368040</link>
    <description>&lt;P&gt;Normalize the "lookup" table.&amp;nbsp; Then it is much easier to deal with.&amp;nbsp;No need to keep the empty cells.&amp;nbsp; Example using the same character variables.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data lookup
  length row col $8 ;
  input row @ ;
  do col='1','2','3','4';
    input type $ @;
    if not missing(type) then output;
  end;
datalines;
1 a . b2 a .
2 . b1 b3 .
3 . . b4 b2
4 . . . a
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now you can use SQL or data step MERGE to combine the two.&lt;/P&gt;
&lt;P&gt;Or even load it into a hash object and use the FIND() hash object method to retrieve the TYPE.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But it probably makes more sense if you are trying to use VLOOKUP type capabilities if the index variables are numeric.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data lookup
  row+1;
  do col=1 to 4;
    input type $ @;
    if not missing(type) then output;
  end;
datalines;
a . b2 a .
. b1 b3 .
. . b4 b2
. . . a
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 19 Jul 2024 13:42:42 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2024-07-19T13:42:42Z</dc:date>
    <item>
      <title>Join two datasets with different structure (similar to vlookup)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Join-two-datasets-with-different-structure-similar-to-vlookup/m-p/936264#M368038</link>
      <description>&lt;P&gt;Hi everyone,&lt;/P&gt;&lt;P&gt;I have two tables A and B and need to join them based on two variables from A which correspond to differently structured variables from B.&lt;/P&gt;&lt;PRE&gt;data a;
  input id $ var1 $ var2 $;
datalines;
1 1 1&lt;BR /&gt;1 3 2&lt;BR /&gt;2 2 4&lt;BR /&gt;2 4 3
run;&lt;/PRE&gt;&lt;PRE&gt;data b;
  input var2 $ var1_1 $ var1_2 $ var1_3 $ var1_4 $;
datalines;
1 a . b2 a .&lt;BR /&gt;2 . b1 b3 .&lt;BR /&gt;3 . . b4 b2&lt;BR /&gt;4 . . . a
run;&lt;/PRE&gt;&lt;PRE&gt;data want;
  input id $ var1 $ var2 $ type $;
datalines;
1 1 1 a&lt;BR /&gt;1 3 2 .&lt;BR /&gt;2 2 4 .&lt;BR /&gt;2 4 3 b2
;&lt;/PRE&gt;&lt;P&gt;Thereby, the dots represent empty cells.&lt;/P&gt;&lt;P&gt;This left join would also be possible with Excel vlookup and two criteria, however Excel's performance is too weak for the actual datasets and I'm sure there is an elegant way to do this also with SAS in one step. Any suggestions of how to do this are appreciated.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 18 Jul 2024 18:50:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Join-two-datasets-with-different-structure-similar-to-vlookup/m-p/936264#M368038</guid>
      <dc:creator>nemez</dc:creator>
      <dc:date>2024-07-18T18:50:04Z</dc:date>
    </item>
    <item>
      <title>Re: Join two datasets with different structure (similar to vlookup)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Join-two-datasets-with-different-structure-similar-to-vlookup/m-p/936268#M368040</link>
      <description>&lt;P&gt;Normalize the "lookup" table.&amp;nbsp; Then it is much easier to deal with.&amp;nbsp;No need to keep the empty cells.&amp;nbsp; Example using the same character variables.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data lookup
  length row col $8 ;
  input row @ ;
  do col='1','2','3','4';
    input type $ @;
    if not missing(type) then output;
  end;
datalines;
1 a . b2 a .
2 . b1 b3 .
3 . . b4 b2
4 . . . a
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now you can use SQL or data step MERGE to combine the two.&lt;/P&gt;
&lt;P&gt;Or even load it into a hash object and use the FIND() hash object method to retrieve the TYPE.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But it probably makes more sense if you are trying to use VLOOKUP type capabilities if the index variables are numeric.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data lookup
  row+1;
  do col=1 to 4;
    input type $ @;
    if not missing(type) then output;
  end;
datalines;
a . b2 a .
. b1 b3 .
. . b4 b2
. . . a
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 19 Jul 2024 13:42:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Join-two-datasets-with-different-structure-similar-to-vlookup/m-p/936268#M368040</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-07-19T13:42:42Z</dc:date>
    </item>
    <item>
      <title>Re: Join two datasets with different structure (similar to vlookup)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Join-two-datasets-with-different-structure-similar-to-vlookup/m-p/936299#M368054</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a(index=(var2));
  input id $ var1 $ var2 $;
datalines;
1 1 1
1 3 2
2 2 4
2 4 3
;
data b(index=(var2));
  input var2 $ var1_1 $ var1_2 $ var1_3 $ var1_4 $;
datalines;
1 a . b2 a .
2 . b1 b3 .
3 . . b4 b2
4 . . . a
;

data want;
 merge a b;
 by var2;
 want=vvaluex(cats('var1_',var1));
 drop var1_:;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 19 Jul 2024 01:10:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Join-two-datasets-with-different-structure-similar-to-vlookup/m-p/936299#M368054</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2024-07-19T01:10:21Z</dc:date>
    </item>
    <item>
      <title>Re: Join two datasets with different structure (similar to vlookup)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Join-two-datasets-with-different-structure-similar-to-vlookup/m-p/936323#M368060</link>
      <description>&lt;P&gt;Assuming the table B is already sorted by VAR2, I would use a solution like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=a;
  by var2;
run;

data want;
 merge a b;
 by var2;
 array var1_ (*) var1_1-var1_4;
 type=var1_(var1);
 drop var1_:;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You may then want to sort back by ID and VAR1, to get data in the same order as your example.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But I do not understand the second observation in your WANT example: should the TYPE variable not be "b3" here (VAR1_3 from the row in B with VAR2=2)?&lt;/P&gt;</description>
      <pubDate>Fri, 19 Jul 2024 10:44:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Join-two-datasets-with-different-structure-similar-to-vlookup/m-p/936323#M368060</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2024-07-19T10:44:30Z</dc:date>
    </item>
    <item>
      <title>Re: Join two datasets with different structure (similar to vlookup)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Join-two-datasets-with-different-structure-similar-to-vlookup/m-p/936400#M368071</link>
      <description>&lt;P&gt;Hello Ksharp,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;thank you very much, your solution works fine.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Greetings&lt;/P&gt;</description>
      <pubDate>Fri, 19 Jul 2024 16:38:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Join-two-datasets-with-different-structure-similar-to-vlookup/m-p/936400#M368071</guid>
      <dc:creator>nemez</dc:creator>
      <dc:date>2024-07-19T16:38:07Z</dc:date>
    </item>
    <item>
      <title>Re: Join two datasets with different structure (similar to vlookup)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Join-two-datasets-with-different-structure-similar-to-vlookup/m-p/936402#M368073</link>
      <description>Thank you for your suggestion and you are right about my mistake in the want example. However, I have not yet tested your approach.</description>
      <pubDate>Fri, 19 Jul 2024 16:42:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Join-two-datasets-with-different-structure-similar-to-vlookup/m-p/936402#M368073</guid>
      <dc:creator>nemez</dc:creator>
      <dc:date>2024-07-19T16:42:48Z</dc:date>
    </item>
    <item>
      <title>Re: Join two datasets with different structure (similar to vlookup)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Join-two-datasets-with-different-structure-similar-to-vlookup/m-p/936403#M368074</link>
      <description>&lt;P&gt;Thank you very much for all the contributions!&lt;/P&gt;</description>
      <pubDate>Fri, 19 Jul 2024 16:44:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Join-two-datasets-with-different-structure-similar-to-vlookup/m-p/936403#M368074</guid>
      <dc:creator>nemez</dc:creator>
      <dc:date>2024-07-19T16:44:45Z</dc:date>
    </item>
  </channel>
</rss>

