<?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 Proc merge macro overwrites variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Proc-merge-macro-overwrites-variables/m-p/904790#M357444</link>
    <description>&lt;P&gt;When I run this macro, I get multiple overwritten notes for variables visit and parm in the SAS log. I am confused since these variables are dropped in the data step. How can I change the code so that visit and parm variables are not overwritten anymore?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
    merge test2(in=inA)
		
		  %macro merge(parm=,var=,and=);
             test3(keep=PtID Visit parm value0 
	                  where=(parm="&amp;amp;parm" and visit="Baseline" &amp;amp;and)
			          rename=(value0=&amp;amp;var._0 ))
	         test3(keep=PtID Visit parm value valueChg valueChgPct 
	                  where=(parm="&amp;amp;parm" and visit="48 Month Follow-Up" &amp;amp;and)
			          rename=(value=&amp;amp;var._48 valueChg=&amp;amp;var.Chg valueChgPct=&amp;amp;var.ChgPct))
          %mend;

		  %merge(parm=parm1,var=var1)
		  %merge(parm=parm2,var=var2)
		  %merge(parm=parm3,var=var3)
		  %merge(parm=parm4,var=var4)
		  %merge(parm=parm5,var=var5);
		  
	by Pt;
	if inA;
	drop visit parm;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Multiple Log notes like this:&lt;/P&gt;
&lt;P&gt;INFO: The variable visit on data set WORK.TEST3 will be overwritten by data set WORK.TEST3.&lt;/P&gt;
&lt;P&gt;INFO: The variable parm on data set WORK.TEST3 will be overwritten by data set WORK.TEST3.&lt;/P&gt;</description>
    <pubDate>Tue, 28 Nov 2023 06:00:55 GMT</pubDate>
    <dc:creator>Abishekaa</dc:creator>
    <dc:date>2023-11-28T06:00:55Z</dc:date>
    <item>
      <title>Proc merge macro overwrites variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-merge-macro-overwrites-variables/m-p/904790#M357444</link>
      <description>&lt;P&gt;When I run this macro, I get multiple overwritten notes for variables visit and parm in the SAS log. I am confused since these variables are dropped in the data step. How can I change the code so that visit and parm variables are not overwritten anymore?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
    merge test2(in=inA)
		
		  %macro merge(parm=,var=,and=);
             test3(keep=PtID Visit parm value0 
	                  where=(parm="&amp;amp;parm" and visit="Baseline" &amp;amp;and)
			          rename=(value0=&amp;amp;var._0 ))
	         test3(keep=PtID Visit parm value valueChg valueChgPct 
	                  where=(parm="&amp;amp;parm" and visit="48 Month Follow-Up" &amp;amp;and)
			          rename=(value=&amp;amp;var._48 valueChg=&amp;amp;var.Chg valueChgPct=&amp;amp;var.ChgPct))
          %mend;

		  %merge(parm=parm1,var=var1)
		  %merge(parm=parm2,var=var2)
		  %merge(parm=parm3,var=var3)
		  %merge(parm=parm4,var=var4)
		  %merge(parm=parm5,var=var5);
		  
	by Pt;
	if inA;
	drop visit parm;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Multiple Log notes like this:&lt;/P&gt;
&lt;P&gt;INFO: The variable visit on data set WORK.TEST3 will be overwritten by data set WORK.TEST3.&lt;/P&gt;
&lt;P&gt;INFO: The variable parm on data set WORK.TEST3 will be overwritten by data set WORK.TEST3.&lt;/P&gt;</description>
      <pubDate>Tue, 28 Nov 2023 06:00:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-merge-macro-overwrites-variables/m-p/904790#M357444</guid>
      <dc:creator>Abishekaa</dc:creator>
      <dc:date>2023-11-28T06:00:55Z</dc:date>
    </item>
    <item>
      <title>Re: Proc merge macro overwrites variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-merge-macro-overwrites-variables/m-p/904795#M357447</link>
      <description>&lt;P&gt;1) variables are dropped but at the "very end" of the process (just for clarification, moving the "drop" statement at the beginning of the data step code will &lt;EM&gt;not&lt;/EM&gt; change that).&lt;/P&gt;
&lt;P&gt;2) read this article by &lt;A title="https://www.lexjansen.com/nesug/nesug99/ad/ad155.pdf" href="https://www.lexjansen.com/nesug/nesug99/ad/ad155.pdf" target="_self"&gt;Bob Virgille:&amp;nbsp;How MERGE Really Works&lt;/A&gt; so you will understand what is happening under the hood.&lt;/P&gt;
&lt;P&gt;3)&amp;nbsp; good programming practice is to keep source code of your macro outside the data step, after all macros suppose to be (among others) "wrappers for reusable code", right?&lt;/P&gt;
&lt;P&gt;4) since this works:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
  set sashelp.class(
    rename=(age=abcdef)
    where=(abcdef=12)
  )
  ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;then this modification should too (you didn't provided any test data, so I can test it myself)&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro merge(parm=,var=,and=);
  %local f s;
  %let f=______A&amp;amp;sysindex.;
  %let s=______B&amp;amp;sysindex.;
  test3(keep=PtID Visit parm value0
  rename=(Visit=V&amp;amp;f. parm=P&amp;amp;f.) 
  where=(P&amp;amp;f.="&amp;amp;parm" and V&amp;amp;f.="Baseline" &amp;amp;and)
  rename=(value0=&amp;amp;var._0 ))
  test3(keep=PtID Visit parm value valueChg valueChgPct
  rename=(Visit=V&amp;amp;s. parm=P&amp;amp;s.)  
  where=(P&amp;amp;s.="&amp;amp;parm" and V&amp;amp;s.="48 Month Follow-Up" &amp;amp;and)
  rename=(value=&amp;amp;var._48 valueChg=&amp;amp;var.Chg valueChgPct=&amp;amp;var.ChgPct))
%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and at the end of the data step:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;drop V______: P______: ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;to drop unnecessary variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Tue, 28 Nov 2023 07:29:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-merge-macro-overwrites-variables/m-p/904795#M357447</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2023-11-28T07:29:20Z</dc:date>
    </item>
  </channel>
</rss>

