<?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: Trap when merging, then redefining variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Trap-when-merging-then-redefining-variable/m-p/957701#M373836</link>
    <description>&lt;P&gt;Here are a couple other resources that explain what happens during merges.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://support.sas.com/kb/48/705.html" target="_self"&gt;Usage Note 48705: A one-to-many merge with common variables that are not the BY variables will have values from the many data set after the first observation&lt;/A&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://go.documentation.sas.com/doc/en/lrcon/9.4/p1topuaeb1ikf0n11f6ibw5ftral.htm" target="_self"&gt;Data Step Concepts&lt;/A&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hope that helps,&lt;/P&gt;
&lt;P&gt;Grace&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 30 Jan 2025 18:07:32 GMT</pubDate>
    <dc:creator>grace_sas</dc:creator>
    <dc:date>2025-01-30T18:07:32Z</dc:date>
    <item>
      <title>Trap when merging, then redefining variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trap-when-merging-then-redefining-variable/m-p/957557#M373789</link>
      <description>&lt;P&gt;I've been using SAS for many years, but never came across this trap before. Surely it deserves a warning message in the log!&lt;/P&gt;
&lt;P&gt;I can see now how the problem arises when I think through how SAS does merges (by retaining variables on the 1 side of a 1:n merge). The solution is to never redefine variables in your data step when merging (if the variable comes from the 1: side of the merge) - even if it might be convenient for doing things like converting cents to $.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data file1;
input id var;
cards;
1 10
2 20
;
data file2;
input id;
cards;
1 
1
2
2
2
;
data out1;
merge file1 file2;
by id;
var = var/100;  /* this gets calculated as if var were retained for each by value */
run;
proc print;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 29 Jan 2025 11:40:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trap-when-merging-then-redefining-variable/m-p/957557#M373789</guid>
      <dc:creator>BruceBrad</dc:creator>
      <dc:date>2025-01-29T11:40:55Z</dc:date>
    </item>
    <item>
      <title>Re: Trap when merging, then redefining variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trap-when-merging-then-redefining-variable/m-p/957564#M373790</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The value of a variable is only reset to missing in the&amp;nbsp;data step's PDV at the start of a data step if&lt;/P&gt;
&lt;P&gt;- it is not a &lt;U&gt;data set variable&lt;/U&gt;, and&lt;/P&gt;
&lt;P&gt;- it is not a &lt;U&gt;retained variable&lt;/U&gt; (RETAIN statement or increment syntax).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The value of a &lt;U&gt;data set variable&lt;/U&gt; is refreshed in the PDV when a value is read from the data set.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When none of these condition are true (data set variable, but no value read for the data set), the value in the PDV remains unchanged.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;People who don't know that have not been trained properly. Not blaming them, it's very common unfortunately.&lt;/P&gt;
&lt;P&gt;The PDV logic also explains why a Cartesian product does not take place when merging data sets in a data step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'll take the opportunity to complain again that the message&lt;/P&gt;
&lt;PRE&gt; NOTE: MERGE statement has more than one data set with repeats of BY values. &lt;/PRE&gt;
&lt;P&gt;should never have been a note, and should now be customisable to a WARNING or an ERROR, with an option similar to these:&lt;/P&gt;
&lt;PRE&gt;  DKRICOND=ERROR    Specifies the error level to report when a variable is missing from an input data set during the processing of a 
                    DROP=, KEEP=, or RENAME= data set option.
  DKROCOND=WARN     Specifies the error level to report when a variable is missing from an output data set during the processing of 
                    a DROP=, KEEP=, or RENAME= data set option.
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 01 Feb 2025 02:09:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trap-when-merging-then-redefining-variable/m-p/957564#M373790</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2025-02-01T02:09:28Z</dc:date>
    </item>
    <item>
      <title>Re: Trap when merging, then redefining variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trap-when-merging-then-redefining-variable/m-p/957578#M373791</link>
      <description>&lt;P&gt;That is why 1 to many merges actually work.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Just do you calculations on the first observation of the by group.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data out1;
  merge file1 file2;
  by id;
  if first.id then var = var/100;  
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 29 Jan 2025 13:55:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trap-when-merging-then-redefining-variable/m-p/957578#M373791</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-01-29T13:55:43Z</dc:date>
    </item>
    <item>
      <title>Re: Trap when merging, then redefining variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trap-when-merging-then-redefining-variable/m-p/957582#M373792</link>
      <description>&lt;P&gt;Here are some ancient papers that discuss this and similar issues.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's an ancient paper that discusses your original post plus more:&lt;/P&gt;
&lt;P&gt;How MERGE Really Works&lt;/P&gt;
&lt;P&gt;&lt;A href="https://www.lexjansen.com/nesug/nesug99/ad/ad155.pdf" target="_blank"&gt;https://www.lexjansen.com/nesug/nesug99/ad/ad155.pdf&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And here's a slightly less ancient paper that discusses other bizarre results with MERGE:&lt;/P&gt;
&lt;P&gt;Danger:&amp;nbsp; MERGE Ahead!&amp;nbsp; Warning:&amp;nbsp; BY Variable with Multiple Lengths&lt;/P&gt;
&lt;P&gt;&lt;A href="https://support.sas.com/resources/papers/proceedings/proceedings/sugi28/098-28.pdf" target="_blank"&gt;https://support.sas.com/resources/papers/proceedings/proceedings/sugi28/098-28.pdf&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 29 Jan 2025 14:33:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trap-when-merging-then-redefining-variable/m-p/957582#M373792</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2025-01-29T14:33:05Z</dc:date>
    </item>
    <item>
      <title>Re: Trap when merging, then redefining variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trap-when-merging-then-redefining-variable/m-p/957594#M373795</link>
      <description>&lt;P&gt;Both very good papers.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One thing I noticed in the first one that I would change is this paragraph.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;The role of the PDV clears up confusing combinations&lt;BR /&gt;of KEEPs, DROPs, and RENAMEs. All KEEPs,&lt;BR /&gt;DROPs, and RENAMEs on a DATA statement refer to&lt;BR /&gt;variable names in the PDV. All KEEPs, DROPs, and&lt;BR /&gt;RENAMEs on a SET, MERGE, or UPDATE statement&lt;BR /&gt;refer to variable names in the source data set. So&lt;BR /&gt;when will the following program work?&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I like to include the required = when referring to dataset options to make it clearer to the reader that the KEEP= dataset option is a different thing than the KEEP data step statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And in the second one it is working to hard to add unneeded commas between the values when generating the list of values for the IN operator.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;memname in&lt;BR /&gt;("MALES", "FEMALES", "ANDROIDS")&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;SAS is just as happy with spaces between the values:&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;memname in&amp;nbsp;("MALES" "FEMALES" "ANDROIDS")
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;which is a lot easier to generate in macro code.&lt;/SPAN&gt;&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;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 29 Jan 2025 15:47:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trap-when-merging-then-redefining-variable/m-p/957594#M373795</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-01-29T15:47:34Z</dc:date>
    </item>
    <item>
      <title>Re: Trap when merging, then redefining variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trap-when-merging-then-redefining-variable/m-p/957654#M373814</link>
      <description>&lt;P&gt;1)&lt;/P&gt;
&lt;P&gt;Yeah. That is an interesting problem.&lt;BR /&gt;As you said ,the problem is from VAR is retained (since it is from a dataset).&lt;BR /&gt;first var=var/100 ==&amp;gt;10/100= 0.1&lt;BR /&gt;second var=var/100 ==&amp;gt; 0.1/100=0.001&lt;BR /&gt;......and so on.&lt;BR /&gt;&lt;BR /&gt;That violated our intuition inside. I suggest to use TWO dataset to avoid this problem:&lt;/P&gt;
&lt;PRE&gt;data file1;
input id var;
cards;
1 10
2 20
;
data file2;
input id;
cards;
1
1
2
2
2
;
data out1;
merge file1 file2;
by id;
run;
data out1;
set out1;
var = var/100;
run;
proc print;run;&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;2)&lt;/P&gt;
&lt;P&gt;Another problem I am running into when I use MERGE and two datasets have a variable in common :&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data file1;
input id var;
cards;
1 10
2 20
;
data file2;
input id var;
cards;
1 9
1 9
2 99
2 99
2 99
;
data out1;
merge file2 file1;
by id;
run;

proc print;run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Ksharp_1-1738206818971.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/104136iE46B5315CD16DC11/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Ksharp_1-1738206818971.png" alt="Ksharp_1-1738206818971.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 30 Jan 2025 03:16:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trap-when-merging-then-redefining-variable/m-p/957654#M373814</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2025-01-30T03:16:49Z</dc:date>
    </item>
    <item>
      <title>Re: Trap when merging, then redefining variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trap-when-merging-then-redefining-variable/m-p/957701#M373836</link>
      <description>&lt;P&gt;Here are a couple other resources that explain what happens during merges.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://support.sas.com/kb/48/705.html" target="_self"&gt;Usage Note 48705: A one-to-many merge with common variables that are not the BY variables will have values from the many data set after the first observation&lt;/A&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://go.documentation.sas.com/doc/en/lrcon/9.4/p1topuaeb1ikf0n11f6ibw5ftral.htm" target="_self"&gt;Data Step Concepts&lt;/A&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hope that helps,&lt;/P&gt;
&lt;P&gt;Grace&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 30 Jan 2025 18:07:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trap-when-merging-then-redefining-variable/m-p/957701#M373836</guid>
      <dc:creator>grace_sas</dc:creator>
      <dc:date>2025-01-30T18:07:32Z</dc:date>
    </item>
  </channel>
</rss>

