<?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 How do I do a conditional merge with a left join to include all the rows from the left data set? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-do-a-conditional-merge-with-a-left-join-to-include-all/m-p/855462#M338094</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;This is a data manipulation exercise.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How do I do merge every row in the right data set (have2) with only the rows that have the yesno variable="Y"? I want to include all the rows in the left data set that have either yesno="Y" and yesno="N".&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have1; 
infile datalines dsd dlm=",";
	input id $ visitnum yesno$;
datalines;
001, 1, N
001, 2, Y
002, 1, N
002, 2, Y
003, 1, Y
003, 2, Y
;
run;
proc sort; by visitnum; run;


data have2; 
infile datalines dsd dlm=",";
	input id $ visitnum howmany seq;
datalines;
001, 2, 10, 1
001, 2, 15, 2
002, 2, 10, 1
002, 2, 15, 2
003, 1, 10, 1
003, 2, 15, 1
003, 2, 15, 2
;
run;
proc sort; by id visitnum; run;

proc sql; /*incorrect code attempt*/
	create table want as
	select a.*, b.howmany, b.seq
	from have1 a
	left join have2 b on a.id=b.id and b.visitnum=b.visitnum
	where yesno="Y";
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;desired output:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Hello_there_0-1674592052674.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/79751i1240714CCA0150D6/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Hello_there_0-1674592052674.png" alt="Hello_there_0-1674592052674.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 24 Jan 2023 20:40:38 GMT</pubDate>
    <dc:creator>Hello_there</dc:creator>
    <dc:date>2023-01-24T20:40:38Z</dc:date>
    <item>
      <title>How do I do a conditional merge with a left join to include all the rows from the left data set?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-do-a-conditional-merge-with-a-left-join-to-include-all/m-p/855462#M338094</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;This is a data manipulation exercise.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How do I do merge every row in the right data set (have2) with only the rows that have the yesno variable="Y"? I want to include all the rows in the left data set that have either yesno="Y" and yesno="N".&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have1; 
infile datalines dsd dlm=",";
	input id $ visitnum yesno$;
datalines;
001, 1, N
001, 2, Y
002, 1, N
002, 2, Y
003, 1, Y
003, 2, Y
;
run;
proc sort; by visitnum; run;


data have2; 
infile datalines dsd dlm=",";
	input id $ visitnum howmany seq;
datalines;
001, 2, 10, 1
001, 2, 15, 2
002, 2, 10, 1
002, 2, 15, 2
003, 1, 10, 1
003, 2, 15, 1
003, 2, 15, 2
;
run;
proc sort; by id visitnum; run;

proc sql; /*incorrect code attempt*/
	create table want as
	select a.*, b.howmany, b.seq
	from have1 a
	left join have2 b on a.id=b.id and b.visitnum=b.visitnum
	where yesno="Y";
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;desired output:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Hello_there_0-1674592052674.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/79751i1240714CCA0150D6/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Hello_there_0-1674592052674.png" alt="Hello_there_0-1674592052674.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 24 Jan 2023 20:40:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-do-a-conditional-merge-with-a-left-join-to-include-all/m-p/855462#M338094</guid>
      <dc:creator>Hello_there</dc:creator>
      <dc:date>2023-01-24T20:40:38Z</dc:date>
    </item>
    <item>
      <title>Re: How do I do a conditional merge with a left join to include all the rows from the left data set?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-do-a-conditional-merge-with-a-left-join-to-include-all/m-p/855465#M338095</link>
      <description>&lt;P&gt;I don't understand the goal:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;How do I do merge every row in the right data set (have2) with only the rows that have the yesno variable="Y"? I want to include all the rows in the left data set that have either yesno="Y" and yesno="N".&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;It seems the first sentence (where you want yesno="Y") is contradicted by the second sentence (where yesno can be either "Y" or "N").&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 24 Jan 2023 20:31:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-do-a-conditional-merge-with-a-left-join-to-include-all/m-p/855465#M338095</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-01-24T20:31:36Z</dc:date>
    </item>
    <item>
      <title>Re: How do I do a conditional merge with a left join to include all the rows from the left data set?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-do-a-conditional-merge-with-a-left-join-to-include-all/m-p/855466#M338096</link>
      <description>&lt;P&gt;I don't know if it's possible. But i would like to merge the values from have2 with only the values from have1 where it meets the condition that yesno="Y", else if yesno is not "Yes", then don't merge. &lt;BR /&gt;The final table i would like to have both the values of yesno (either yesno="Y" or yesno="N" while the yesno="N" values have missing values where the merge would have been)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The desired output at the bottom of the OP will probably show more than i can explain.&lt;/P&gt;</description>
      <pubDate>Tue, 24 Jan 2023 20:35:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-do-a-conditional-merge-with-a-left-join-to-include-all/m-p/855466#M338096</guid>
      <dc:creator>Hello_there</dc:creator>
      <dc:date>2023-01-24T20:35:58Z</dc:date>
    </item>
    <item>
      <title>Re: How do I do a conditional merge with a left join to include all the rows from the left data set?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-do-a-conditional-merge-with-a-left-join-to-include-all/m-p/855473#M338098</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
    create table want as select
        a.*,
        case when a.yesno='Y' then b.howmany else . end as howmany,
        case when a.yesno='Y' then b.seq else . end as seq

    from have1 as a left join have2 as b
    on a.id=b.id and a.visitnum=b.visitnum;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 24 Jan 2023 20:50:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-do-a-conditional-merge-with-a-left-join-to-include-all/m-p/855473#M338098</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-01-24T20:50:39Z</dc:date>
    </item>
    <item>
      <title>Re: How do I do a conditional merge with a left join to include all the rows from the left data set?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-do-a-conditional-merge-with-a-left-join-to-include-all/m-p/855474#M338099</link>
      <description>Thanks, PaigeMiller!</description>
      <pubDate>Tue, 24 Jan 2023 20:52:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-do-a-conditional-merge-with-a-left-join-to-include-all/m-p/855474#M338099</guid>
      <dc:creator>Hello_there</dc:creator>
      <dc:date>2023-01-24T20:52:52Z</dc:date>
    </item>
  </channel>
</rss>

