<?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 Using call execute and %macro to trim multiple variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Using-call-execute-and-macro-to-trim-multiple-variables/m-p/678290#M204699</link>
    <description>&lt;P&gt;Hi, SAS experts,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm trying to use call execute and %macro to trim multiple variables at the 99 percentile and 1st percentile. I found a very helpful post from this forum:&amp;nbsp;&lt;A href="https://communities.sas.com/t5/General-SAS-Programming/SAS-MACRO-Capping-Outliers-for-Dollar-amount-variables/td-p/335040#" target="_blank" rel="noopener"&gt;https://communities.sas.com/t5/General-SAS-Programming/SAS-MACRO-Capping-Outliers-for-Dollar-amount-variables/td-p/335040#&lt;/A&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I followed their steps and it did work. However, in their code, the new data set directly replaces the previous data set, and the created variables directly replace the original variables. I would like to tweak their code by creating a new data set and new variables. I'm using their code to demonstrate below:&lt;/P&gt;&lt;P&gt;However, I got error messages saying "ERROR: The keyword parameter DSET2 was not defined with the macro.&lt;BR /&gt;ERROR: The keyword parameter VAR2 was not defined with the macro."&lt;/P&gt;&lt;P&gt;What I can do to achieve the result I want?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you very much in advance!&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;&lt;LI-CODE lang="markup"&gt;*Calculate IQR and first/third quartiles;
proc means data=sashelp.class stackods n qrange p1 p99;
var weight height;
ods output summary=ranges;
run;

*create data with outliers to check;
data capped; 
	set sashelp.class;
	if name='Alfred' then weight=220;
	if name='Jane' then height=-30;
run;

*macro to cap outliers;

%macro cap(dset=,dset2=, var=,var2=, lower=, upper=);/* here I added dset2 and var2*/

data &amp;amp;dset2;/* here I tweaked their code, hoping to create a new data set with a new set of variables*/
	set &amp;amp;dset;
	if &amp;amp;var&amp;gt;&amp;amp;upper then &amp;amp;var2=&amp;amp;upper;
	if &amp;amp;var&amp;lt;&amp;amp;lower then &amp;amp;var2=&amp;amp;lower;
run;

%mend;


*create cutoffs and execute macro for each variable;
data cutoffs;
set ranges;
lower=p1;
upper=p99;
string = catt('%cap(dset=capped, dset2=new, var=, var2=', variable, ", lower=", lower, ", upper=", upper ,");");/* here I added dset2 and var2*/
call execute(string);
run;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 22 Aug 2020 23:08:36 GMT</pubDate>
    <dc:creator>sdaniels429</dc:creator>
    <dc:date>2020-08-22T23:08:36Z</dc:date>
    <item>
      <title>Using call execute and %macro to trim multiple variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-call-execute-and-macro-to-trim-multiple-variables/m-p/678290#M204699</link>
      <description>&lt;P&gt;Hi, SAS experts,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm trying to use call execute and %macro to trim multiple variables at the 99 percentile and 1st percentile. I found a very helpful post from this forum:&amp;nbsp;&lt;A href="https://communities.sas.com/t5/General-SAS-Programming/SAS-MACRO-Capping-Outliers-for-Dollar-amount-variables/td-p/335040#" target="_blank" rel="noopener"&gt;https://communities.sas.com/t5/General-SAS-Programming/SAS-MACRO-Capping-Outliers-for-Dollar-amount-variables/td-p/335040#&lt;/A&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I followed their steps and it did work. However, in their code, the new data set directly replaces the previous data set, and the created variables directly replace the original variables. I would like to tweak their code by creating a new data set and new variables. I'm using their code to demonstrate below:&lt;/P&gt;&lt;P&gt;However, I got error messages saying "ERROR: The keyword parameter DSET2 was not defined with the macro.&lt;BR /&gt;ERROR: The keyword parameter VAR2 was not defined with the macro."&lt;/P&gt;&lt;P&gt;What I can do to achieve the result I want?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you very much in advance!&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;&lt;LI-CODE lang="markup"&gt;*Calculate IQR and first/third quartiles;
proc means data=sashelp.class stackods n qrange p1 p99;
var weight height;
ods output summary=ranges;
run;

*create data with outliers to check;
data capped; 
	set sashelp.class;
	if name='Alfred' then weight=220;
	if name='Jane' then height=-30;
run;

*macro to cap outliers;

%macro cap(dset=,dset2=, var=,var2=, lower=, upper=);/* here I added dset2 and var2*/

data &amp;amp;dset2;/* here I tweaked their code, hoping to create a new data set with a new set of variables*/
	set &amp;amp;dset;
	if &amp;amp;var&amp;gt;&amp;amp;upper then &amp;amp;var2=&amp;amp;upper;
	if &amp;amp;var&amp;lt;&amp;amp;lower then &amp;amp;var2=&amp;amp;lower;
run;

%mend;


*create cutoffs and execute macro for each variable;
data cutoffs;
set ranges;
lower=p1;
upper=p99;
string = catt('%cap(dset=capped, dset2=new, var=, var2=', variable, ", lower=", lower, ", upper=", upper ,");");/* here I added dset2 and var2*/
call execute(string);
run;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 22 Aug 2020 23:08:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-call-execute-and-macro-to-trim-multiple-variables/m-p/678290#M204699</guid>
      <dc:creator>sdaniels429</dc:creator>
      <dc:date>2020-08-22T23:08:36Z</dc:date>
    </item>
    <item>
      <title>Re: Using call execute and %macro to trim multiple variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-call-execute-and-macro-to-trim-multiple-variables/m-p/678297#M204702</link>
      <description>&lt;P&gt;The code you posted looks fine.&amp;nbsp; This means that the chief suspect here is differences in the code you posted vs. the code you actually ran.&amp;nbsp; Somehow, the code you actually ran is using the original version of %cap instead of the version that you posted here.&amp;nbsp; I'm forced to leave the detective work in your hands.&lt;/P&gt;</description>
      <pubDate>Fri, 21 Aug 2020 01:32:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-call-execute-and-macro-to-trim-multiple-variables/m-p/678297#M204702</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2020-08-21T01:32:52Z</dc:date>
    </item>
    <item>
      <title>Re: Using call execute and %macro to trim multiple variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-call-execute-and-macro-to-trim-multiple-variables/m-p/678419#M204749</link>
      <description>&lt;P&gt;Thank you for your response!&amp;nbsp;&lt;/P&gt;&lt;P&gt;Even with the code I posted, I ran it with sashelp.class data, and got error message saying "ERROR: All positional parameters must precede keyword parameters." when I was trying to do call execute.&lt;/P&gt;&lt;P&gt;I'm new to macro, so I think there is something wrong with the code itself?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 21 Aug 2020 12:10:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-call-execute-and-macro-to-trim-multiple-variables/m-p/678419#M204749</guid>
      <dc:creator>sdaniels429</dc:creator>
      <dc:date>2020-08-21T12:10:24Z</dc:date>
    </item>
    <item>
      <title>Re: Using call execute and %macro to trim multiple variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-call-execute-and-macro-to-trim-multiple-variables/m-p/678424#M204752</link>
      <description>&lt;P&gt;First, you have to appreciate that this is a different error message.&amp;nbsp; The cause would be something different than what happened the first time.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Second, if you ran this on sashelp.class, that confirms that the code you ran is different than what you posted.&amp;nbsp; I would check for other differences, especially a missing equal sign.&amp;nbsp; It could be on a missing equal sign when defining the macro (on the %macro cap statement), or it could be a missing equal sign when calling the macro (in the construction of the variable STRING).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 21 Aug 2020 12:31:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-call-execute-and-macro-to-trim-multiple-variables/m-p/678424#M204752</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2020-08-21T12:31:27Z</dc:date>
    </item>
  </channel>
</rss>

