<?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: Merging: How di I keep by-variables from both files in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Merging-How-di-I-keep-by-variables-from-both-files/m-p/929473#M365720</link>
    <description>&lt;P&gt;Your "want" dataset is impossible, as a dataset can not have two variables with the same name.&lt;/P&gt;
&lt;P&gt;You probably want&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
  select
    t1.id,
    t1.day as day_1,
    t2.day as day_2
  from have1 t1
  left join have2 t2
  on t1.id = t2.id and t1.day = t2.day
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 23 May 2024 19:11:58 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2024-05-23T19:11:58Z</dc:date>
    <item>
      <title>Merging: How di I keep by-variables from both files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-How-di-I-keep-by-variables-from-both-files/m-p/929465#M365718</link>
      <description>&lt;P&gt;I have two datasets, both including ID and day. I would like to merge them by ID and day but include the day variables from both files in the data I want, i.e. include them also when there is no match on the day variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have;&lt;/P&gt;
&lt;P&gt;input ID Day;&lt;/P&gt;
&lt;P&gt;datalines;&lt;/P&gt;
&lt;P&gt;1 1&lt;/P&gt;
&lt;P&gt;1 2&lt;/P&gt;
&lt;P&gt;1 3&lt;/P&gt;
&lt;P&gt;;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Data have2;&lt;/P&gt;
&lt;P&gt;Input ID Day;&lt;/P&gt;
&lt;P&gt;1 .&lt;/P&gt;
&lt;P&gt;1 2&lt;/P&gt;
&lt;P&gt;1 3&lt;/P&gt;
&lt;P&gt;;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Data want;&lt;/P&gt;
&lt;P&gt;ID Day Day&lt;/P&gt;
&lt;P&gt;1 1 &amp;nbsp;.&lt;/P&gt;
&lt;P&gt;1 2 2&lt;/P&gt;
&lt;P&gt;1 3 3&lt;/P&gt;</description>
      <pubDate>Thu, 23 May 2024 18:53:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-How-di-I-keep-by-variables-from-both-files/m-p/929465#M365718</guid>
      <dc:creator>terjeph</dc:creator>
      <dc:date>2024-05-23T18:53:11Z</dc:date>
    </item>
    <item>
      <title>Re: Merging: How di I keep by-variables from both files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-How-di-I-keep-by-variables-from-both-files/m-p/929473#M365720</link>
      <description>&lt;P&gt;Your "want" dataset is impossible, as a dataset can not have two variables with the same name.&lt;/P&gt;
&lt;P&gt;You probably want&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
  select
    t1.id,
    t1.day as day_1,
    t2.day as day_2
  from have1 t1
  left join have2 t2
  on t1.id = t2.id and t1.day = t2.day
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 23 May 2024 19:11:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-How-di-I-keep-by-variables-from-both-files/m-p/929473#M365720</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2024-05-23T19:11:58Z</dc:date>
    </item>
    <item>
      <title>Re: Merging: How di I keep by-variables from both files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-How-di-I-keep-by-variables-from-both-files/m-p/929484#M365723</link>
      <description>&lt;P&gt;Thanks! SQL worked well.&lt;/P&gt;</description>
      <pubDate>Thu, 23 May 2024 20:13:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-How-di-I-keep-by-variables-from-both-files/m-p/929484#M365723</guid>
      <dc:creator>terjeph</dc:creator>
      <dc:date>2024-05-23T20:13:51Z</dc:date>
    </item>
    <item>
      <title>Re: Merging: How di I keep by-variables from both files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-How-di-I-keep-by-variables-from-both-files/m-p/929486#M365725</link>
      <description>&lt;P&gt;What happened to the first observation from the second dataset?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;STRONG&gt;You did not include it in your expected output.&amp;nbsp; Why not?&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First let's clean up your example datasets so they actually can run:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input ID Day;
datalines;
1 1
1 2
1 3
;
 
Data have2;
  input ID Day;
datalines;
1 .
1 2
1 3
;

data expect; 
  input ID Day1 Day2
datalines;
1 1  .
1 2 2
1 3 3
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;To tell if the a given dataset is contributing to the merge you can use the IN= dataset option.&amp;nbsp; Those variables are temporary so you can assign them to new variables if you want to remember them.&lt;/P&gt;
&lt;P&gt;So perhaps something like this?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  merge have(in=in1) have2(in=in2);
  by id day;
  day1=in1;
  day2=in2;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;Obs    ID    Day    day1    day2

 1      1     .       0       1
 2      1     1       1       0
 3      1     2       1       1
 4      1     3       1       1
&lt;/PRE&gt;
&lt;P&gt;Or to get closer to want you said you wanted you could instead put the DAY value into the DAY1 and DAY2 variables.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  merge have(in=in1) have2(in=in2);
  by id day;
  if in1 then day1=day;
  if in2 then day2=day;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;PRE&gt;Obs    ID    Day    day1    day2

 1      1     .       .       .
 2      1     1       1       .
 3      1     2       2       2
 4      1     3       3       3
&lt;/PRE&gt;</description>
      <pubDate>Thu, 23 May 2024 21:03:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-How-di-I-keep-by-variables-from-both-files/m-p/929486#M365725</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-05-23T21:03:22Z</dc:date>
    </item>
    <item>
      <title>Re: Merging: How di I keep by-variables from both files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-How-di-I-keep-by-variables-from-both-files/m-p/929520#M365743</link>
      <description>&lt;P&gt;The first obs. in the second dataset is a missing. I'd like to keep the missing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It seems like this solution is working as well. Thanks.&lt;/P&gt;</description>
      <pubDate>Fri, 24 May 2024 05:48:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-How-di-I-keep-by-variables-from-both-files/m-p/929520#M365743</guid>
      <dc:creator>terjeph</dc:creator>
      <dc:date>2024-05-24T05:48:05Z</dc:date>
    </item>
  </channel>
</rss>

