Hi, SAS experts,
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: https://communities.sas.com/t5/General-SAS-Programming/SAS-MACRO-Capping-Outliers-for-Dollar-amount-...
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:
However, I got error messages saying "ERROR: The keyword parameter DSET2 was not defined with the macro.
ERROR: The keyword parameter VAR2 was not defined with the macro."
What I can do to achieve the result I want?
Thank you very much in advance!
*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 &dset2;/* here I tweaked their code, hoping to create a new data set with a new set of variables*/
set &dset;
if &var>&upper then &var2=&upper;
if &var<&lower then &var2=&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;
The code you posted looks fine. This means that the chief suspect here is differences in the code you posted vs. the code you actually ran. Somehow, the code you actually ran is using the original version of %cap instead of the version that you posted here. I'm forced to leave the detective work in your hands.
The code you posted looks fine. This means that the chief suspect here is differences in the code you posted vs. the code you actually ran. Somehow, the code you actually ran is using the original version of %cap instead of the version that you posted here. I'm forced to leave the detective work in your hands.
Thank you for your response!
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.
I'm new to macro, so I think there is something wrong with the code itself?
First, you have to appreciate that this is a different error message. The cause would be something different than what happened the first time.
Second, if you ran this on sashelp.class, that confirms that the code you ran is different than what you posted. I would check for other differences, especially a missing equal sign. 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).
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.