<?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: sas datasets won't merge after being sorted. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/sas-datasets-won-t-merge-after-being-sorted/m-p/967174#M376292</link>
    <description>&lt;P&gt;Please take the time to show a couple of examples of the values that "stack" (or perhaps interleave) instead of merging.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example demonstrating the effect of leading spaces on character values.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a;
  input subject $char5.;
  sourceA='FROM A';
cards;
  201
 202
203
;

data b;
  input subject $char5.;
  sourceB='FROM B';
cards;
201
202
203
;

data want;
 merge a b;
 by subject;
run;

proc print;
  format subject $quote.;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1747861288775.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/107228i31B3DBD1FEB8B2F9/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Tom_0-1747861288775.png" alt="Tom_0-1747861288775.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 21 May 2025 21:01:42 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2025-05-21T21:01:42Z</dc:date>
    <item>
      <title>sas datasets won't merge after being sorted.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sas-datasets-won-t-merge-after-being-sorted/m-p/967136#M376287</link>
      <description>&lt;P&gt;The sort key is the same on both, and is formatted identically on both as a character with idential width. The merged dataset simply stacks one on top of the other.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Code:&lt;/P&gt;&lt;DIV&gt;PROC SORT DATA=AE.LOG_AE;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;BY Subject;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;RUN;&lt;/DIV&gt;&lt;DIV&gt;PROC SORT DATA=AE.Treatment;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; &amp;nbsp; BY Subject;&lt;/DIV&gt;&lt;DIV&gt;RUN;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;DATA AEListings;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;MERGE AE.Log_AE AE.Treatment;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;BY Subject;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;RUN;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Any and all help is welcome. Thanks.&lt;/DIV&gt;</description>
      <pubDate>Wed, 21 May 2025 17:22:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sas-datasets-won-t-merge-after-being-sorted/m-p/967136#M376287</guid>
      <dc:creator>Newtrix</dc:creator>
      <dc:date>2025-05-21T17:22:18Z</dc:date>
    </item>
    <item>
      <title>Re: sas datasets won't merge after being sorted.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sas-datasets-won-t-merge-after-being-sorted/m-p/967153#M376288</link>
      <description>&lt;P&gt;So something is different.&lt;/P&gt;
&lt;P&gt;First make sure the sort actually worked (check the log).&lt;/P&gt;
&lt;P&gt;Now look at examples values of SUBJECT from the two different datasets that you THINK are the same to see why SAS does not consider them the same.&amp;nbsp; Obvious things to look for:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Numeric variables.&amp;nbsp; The values differ by such a small amount that it is not visible when printed.&amp;nbsp; Print the values using HEX16. format to see the exact bit pattern used to represent the values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Character variables.&amp;nbsp; Make sure the case of letters is the same.&amp;nbsp; Make sure that there are not leading spaces, Print the values using the $QUOTE. format.&amp;nbsp; &amp;nbsp;Make sure that there are not hidden characters (such as tab, non-breaking space, carraige return) etc.&amp;nbsp; Print the values with the $HEX. format.&amp;nbsp; Make sure that you have not attached a format with too small a width so that displayed values are truncated, hiding difference in the full value.&amp;nbsp; Print the value without any format attached. Make sure the two datasets are using the same setting of the ENCODING= option.&lt;/P&gt;</description>
      <pubDate>Wed, 21 May 2025 18:05:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sas-datasets-won-t-merge-after-being-sorted/m-p/967153#M376288</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-05-21T18:05:19Z</dc:date>
    </item>
    <item>
      <title>Re: sas datasets won't merge after being sorted.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sas-datasets-won-t-merge-after-being-sorted/m-p/967170#M376289</link>
      <description>&lt;P&gt;The variables in each dataset are 3-digit numbers ranging from 201-208 but are classified as characters, formatted $30. There are no leading or trailing spaces, no carriage returns, etc. Both were uploaded from TXT files with no ENCODING commands in the uploading Macro.&lt;/P&gt;</description>
      <pubDate>Wed, 21 May 2025 20:21:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sas-datasets-won-t-merge-after-being-sorted/m-p/967170#M376289</guid>
      <dc:creator>Newtrix</dc:creator>
      <dc:date>2025-05-21T20:21:55Z</dc:date>
    </item>
    <item>
      <title>Re: sas datasets won't merge after being sorted.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sas-datasets-won-t-merge-after-being-sorted/m-p/967171#M376290</link>
      <description>&lt;P&gt;What do you mean by "&lt;SPAN&gt;formatted $30"?&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Did you attach the $30. format specification to the variable?&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;What LENGTH did you define for the variable?&amp;nbsp; If you did not explicitly define its length and instead first referenced the variable in a format statement that attached the $30. format to it then it will have a length of 30 instead of the 3 you need to store your values.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;If the length is 30 then a three character string can by stored in 28 different ways inside of the 30 character value.&amp;nbsp; (With from 0 to 27 leading spaces). And i&lt;/SPAN&gt;&lt;SPAN&gt;f you only ever PRINT the values using ODS output (the "pretty" output) then the leading spaces will disappear and the value "201" will look exactly the same as the value "&amp;nbsp; &amp;nbsp; &amp;nbsp;201".&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Try removing the leading spaces by using the LEFT() function.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Note: If you assign the numeric value 201 to character variable of length 30 it will have 27 leading spaces.&amp;nbsp; Also if you read in the values using the $CHAR informat instead of the $ informat then leading spaces will be preserved.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 21 May 2025 20:41:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sas-datasets-won-t-merge-after-being-sorted/m-p/967171#M376290</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-05-21T20:41:40Z</dc:date>
    </item>
    <item>
      <title>Re: sas datasets won't merge after being sorted.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sas-datasets-won-t-merge-after-being-sorted/m-p/967172#M376291</link>
      <description>&lt;P&gt;I have since created a numeric variable for each set with the identical info, INFORMAT 5.&lt;/P&gt;&lt;P&gt;Sorted by the new variable, the merge still doesn't work. One gets stacked on the other.&lt;/P&gt;</description>
      <pubDate>Wed, 21 May 2025 20:46:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sas-datasets-won-t-merge-after-being-sorted/m-p/967172#M376291</guid>
      <dc:creator>Newtrix</dc:creator>
      <dc:date>2025-05-21T20:46:34Z</dc:date>
    </item>
    <item>
      <title>Re: sas datasets won't merge after being sorted.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sas-datasets-won-t-merge-after-being-sorted/m-p/967174#M376292</link>
      <description>&lt;P&gt;Please take the time to show a couple of examples of the values that "stack" (or perhaps interleave) instead of merging.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example demonstrating the effect of leading spaces on character values.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a;
  input subject $char5.;
  sourceA='FROM A';
cards;
  201
 202
203
;

data b;
  input subject $char5.;
  sourceB='FROM B';
cards;
201
202
203
;

data want;
 merge a b;
 by subject;
run;

proc print;
  format subject $quote.;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1747861288775.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/107228i31B3DBD1FEB8B2F9/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Tom_0-1747861288775.png" alt="Tom_0-1747861288775.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 21 May 2025 21:01:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sas-datasets-won-t-merge-after-being-sorted/m-p/967174#M376292</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-05-21T21:01:42Z</dc:date>
    </item>
    <item>
      <title>Re: sas datasets won't merge after being sorted.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sas-datasets-won-t-merge-after-being-sorted/m-p/967175#M376293</link>
      <description>&lt;P&gt;Thanks for your time on this. I did try changing to numeric (see post just above) with the same result. There are no leading or trailing characters - 3 digits, that's all. This is the first time I've ever had such issues with a merge.&lt;/P&gt;</description>
      <pubDate>Wed, 21 May 2025 21:10:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sas-datasets-won-t-merge-after-being-sorted/m-p/967175#M376293</guid>
      <dc:creator>Newtrix</dc:creator>
      <dc:date>2025-05-21T21:10:54Z</dc:date>
    </item>
    <item>
      <title>Re: sas datasets won't merge after being sorted.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sas-datasets-won-t-merge-after-being-sorted/m-p/967176#M376294</link>
      <description>&lt;P&gt;I meant "spaces" just now, not characters.&lt;/P&gt;</description>
      <pubDate>Wed, 21 May 2025 21:11:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sas-datasets-won-t-merge-after-being-sorted/m-p/967176#M376294</guid>
      <dc:creator>Newtrix</dc:creator>
      <dc:date>2025-05-21T21:11:46Z</dc:date>
    </item>
    <item>
      <title>Re: sas datasets won't merge after being sorted.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sas-datasets-won-t-merge-after-being-sorted/m-p/967177#M376295</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/299138"&gt;@Newtrix&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thanks for your time on this. I did try changing to numeric (see post just above) with the same result. There are no leading or trailing characters - 3 digits, that's all. This is the first time I've ever had such issues with a merge.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Do the obvious checks again.&amp;nbsp; Make sure you are using MERGE and not SET. Make sure each step finished without errors or unexpected notes.&amp;nbsp; Make sure you are checking the datasets that are actually mentioned in the LOG.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then pick a value that you think should be in both and try to find the observations with that value for each source.&amp;nbsp; If you cannot find them in one of the sources then check what value is actually there for that observation (perhaps you can select it by some other variable to avoid looking at all values).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For numeric values check the actual value by using the HEX16. format so you can see what it actually is.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a;
  do subject=200,300,400; output; end;
run;
data b;
  set a;
  subject+1E-10;
run;
data both;
  merge a(in=in1) b(in=in2);
  by subject;
  ina=in1; inb=in2;
run;

proc print;
run;
proc print;
 format subject hex16.;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;PRE&gt;Obs    subject    ina    inb

 1       200       1      0
 2       200       0      1
 3       300       1      0
 4       300       0      1
 5       400       1      0
 6       400       0      1
&amp;#12;
Obs             subject    ina    inb

 1     4069000000000000     1      0
 2     4069000000000DBE     0      1
 3     4072C00000000000     1      0
 4     4072C000000006DF     0      1
 5     4079000000000000     1      0
 6     40790000000006DF     0      1
&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;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 21 May 2025 21:43:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sas-datasets-won-t-merge-after-being-sorted/m-p/967177#M376295</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-05-21T21:43:38Z</dc:date>
    </item>
    <item>
      <title>Re: sas datasets won't merge after being sorted.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sas-datasets-won-t-merge-after-being-sorted/m-p/967179#M376296</link>
      <description>&lt;P&gt;Pick a subject value that you know is in both datasets, and check that PROC PRINT can find that value.&amp;nbsp; So if the subject ID is 333, then try:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc print data=AE.Log_AE;
  where subject='333';
run;

proc print data=AE.Treatment;
  where subject='333';
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Does that confirm that the subject exists in both datasets?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If so, please post the log from running:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  merge AE.Log_AE  (where=(subject='333') in=a)
             AE.Treatment (where=(subject='333') in=b)
  ;
  by subject;
  put (subject a b)(=) ;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 21 May 2025 21:46:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sas-datasets-won-t-merge-after-being-sorted/m-p/967179#M376296</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2025-05-21T21:46:19Z</dc:date>
    </item>
  </channel>
</rss>

