<?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 merge 2 datasets but get this specific output I need? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-merge-2-datasets-but-get-this-specific-output-I-need/m-p/897520#M354667</link>
    <description>&lt;P&gt;I am learning SAS and I am supposed to get this output by sorting and merging my 2 raw datasets.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Colleen9400_0-1696562955260.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/88681i7E72254C42E17121/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Colleen9400_0-1696562955260.png" alt="Colleen9400_0-1696562955260.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;The problem I keep running into is that I get close, but only the first 3 obs are correct.&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is my code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;filename f1 "C:\Users\Colle\Documents\Grad School\A BINF 5210 Health Data Analytics with SAS\File1-1-1.txt";
filename f2 "C:\Users\Colle\Documents\Grad School\A BINF 5210 Health Data Analytics with SAS\File2-1-1.txt";
run;
data temp;
    infile f1 DSD dlm='09'x;
    input Salary Location $ ID $ Desc $;
run;
proc sort data=temp;
    by ID;
run;
proc print data=temp;
run;

data temp1;
    infile f2 DSD dlm='09'x;
    input ID $ Gender $ Age Mar_Status $ Weight;
run;
proc sort data=temp1;
    by ID;
run;
proc print data=temp1;
run;

data merged_data;
    merge temp(in=a) temp1(in=b);
    by ID;
	if a=1 or b=0;
    run;
proc print data=merged_data;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;This is my output which obviously doesn't match the one I'm supposed to get. I tried all left and right merge combinations.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Colleen9400_1-1696563164626.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/88682i5C49E73B434CF3ED/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Colleen9400_1-1696563164626.png" alt="Colleen9400_1-1696563164626.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;What am I doing wrong? Obs 1-3 are what I need but then obs 4-5 are wrong.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I attached my raw data below.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Again I am a beginner so I am not supposed to use anything "fancy" or more advanced than this level of sorting/merging/aliases&amp;nbsp; &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you!!&lt;/P&gt;</description>
    <pubDate>Fri, 06 Oct 2023 03:41:00 GMT</pubDate>
    <dc:creator>Colleen9400</dc:creator>
    <dc:date>2023-10-06T03:41:00Z</dc:date>
    <item>
      <title>How do I merge 2 datasets but get this specific output I need?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-merge-2-datasets-but-get-this-specific-output-I-need/m-p/897520#M354667</link>
      <description>&lt;P&gt;I am learning SAS and I am supposed to get this output by sorting and merging my 2 raw datasets.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Colleen9400_0-1696562955260.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/88681i7E72254C42E17121/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Colleen9400_0-1696562955260.png" alt="Colleen9400_0-1696562955260.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;The problem I keep running into is that I get close, but only the first 3 obs are correct.&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is my code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;filename f1 "C:\Users\Colle\Documents\Grad School\A BINF 5210 Health Data Analytics with SAS\File1-1-1.txt";
filename f2 "C:\Users\Colle\Documents\Grad School\A BINF 5210 Health Data Analytics with SAS\File2-1-1.txt";
run;
data temp;
    infile f1 DSD dlm='09'x;
    input Salary Location $ ID $ Desc $;
run;
proc sort data=temp;
    by ID;
run;
proc print data=temp;
run;

data temp1;
    infile f2 DSD dlm='09'x;
    input ID $ Gender $ Age Mar_Status $ Weight;
run;
proc sort data=temp1;
    by ID;
run;
proc print data=temp1;
run;

data merged_data;
    merge temp(in=a) temp1(in=b);
    by ID;
	if a=1 or b=0;
    run;
proc print data=merged_data;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;This is my output which obviously doesn't match the one I'm supposed to get. I tried all left and right merge combinations.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Colleen9400_1-1696563164626.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/88682i5C49E73B434CF3ED/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Colleen9400_1-1696563164626.png" alt="Colleen9400_1-1696563164626.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;What am I doing wrong? Obs 1-3 are what I need but then obs 4-5 are wrong.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I attached my raw data below.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Again I am a beginner so I am not supposed to use anything "fancy" or more advanced than this level of sorting/merging/aliases&amp;nbsp; &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you!!&lt;/P&gt;</description>
      <pubDate>Fri, 06 Oct 2023 03:41:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-merge-2-datasets-but-get-this-specific-output-I-need/m-p/897520#M354667</guid>
      <dc:creator>Colleen9400</dc:creator>
      <dc:date>2023-10-06T03:41:00Z</dc:date>
    </item>
    <item>
      <title>Re: How do I merge 2 datasets but get this specific output I need?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-merge-2-datasets-but-get-this-specific-output-I-need/m-p/897524#M354668</link>
      <description>&lt;P&gt;If you look into your SAS log you will find:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1696565205306.png" style="width: 513px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/88685iB9718A9C1819DAB1/image-dimensions/513x227?v=v2" width="513" height="227" role="button" title="Patrick_0-1696565205306.png" alt="Patrick_0-1696565205306.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Your source file has 6 lines but you "lose" the last one.&amp;nbsp;This is caused by your source file not having strings for all positions.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_1-1696564687194.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/88684iF89BF9F142DA6804/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_1-1696564687194.png" alt="Patrick_1-1696564687194.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Add infile option &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lestmtsref/n1rill4udj0tfun1fvce3j401plo.htm" target="_self"&gt;TRUNCOVER&lt;/A&gt; to read the data correctly. As a rule of thumb: Always use truncover unless there is a good reason not to.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data temp;
  infile f1 DSD dlm='09'x truncover;
  input Salary Location $ ID $ Desc $;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Once you've got correct source data you also need to amend your merge step to only keep rows where the ID's match.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data merged_data;
  merge temp(in=a) temp1(in=b);
  by ID;
  if a=1 and b=1;
run;&lt;/CODE&gt;&lt;/PRE&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>Fri, 06 Oct 2023 04:07:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-merge-2-datasets-but-get-this-specific-output-I-need/m-p/897524#M354668</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-10-06T04:07:02Z</dc:date>
    </item>
    <item>
      <title>Re: How do I merge 2 datasets but get this specific output I need?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-merge-2-datasets-but-get-this-specific-output-I-need/m-p/897577#M354697</link>
      <description>&lt;P&gt;Ah thank you so much!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I didn't use truncover because my professor didn't teach us that, so I wonder what she was expecting us to use?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This works though so I am using your solution &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 06 Oct 2023 13:12:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-merge-2-datasets-but-get-this-specific-output-I-need/m-p/897577#M354697</guid>
      <dc:creator>Colleen9400</dc:creator>
      <dc:date>2023-10-06T13:12:47Z</dc:date>
    </item>
    <item>
      <title>Re: How do I merge 2 datasets but get this specific output I need?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-merge-2-datasets-but-get-this-specific-output-I-need/m-p/897578#M354698</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/455538"&gt;@Colleen9400&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Ah thank you so much!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I didn't use truncover because my professor didn't teach us that, so I wonder what she was expecting us to use?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This works though so I am using your solution &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;If you are using LIST MODE input and some values are missing then place a period in the source text file to indicate where the missing value.&amp;nbsp; You can use period for either numeric or character variables as the normal character informat will convert a string consisting of a single period to an empty (really all blank) string.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  input name $ age sex $ ;
cards;
Alice 12 F
Joe . M
Sam 13 .
;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 06 Oct 2023 13:18:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-merge-2-datasets-but-get-this-specific-output-I-need/m-p/897578#M354698</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-10-06T13:18:03Z</dc:date>
    </item>
    <item>
      <title>Re: How do I merge 2 datasets but get this specific output I need?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-merge-2-datasets-but-get-this-specific-output-I-need/m-p/898035#M354943</link>
      <description>Thank you again!</description>
      <pubDate>Tue, 10 Oct 2023 21:44:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-merge-2-datasets-but-get-this-specific-output-I-need/m-p/898035#M354943</guid>
      <dc:creator>Colleen9400</dc:creator>
      <dc:date>2023-10-10T21:44:47Z</dc:date>
    </item>
  </channel>
</rss>

