BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
statir
Fluorite | Level 6

Hi every one,

I am quite new in working with SAS.

 

I want to use the WRDS macro to winsorize my data set. I dont know how to run the macro. Could you please help me what should I do?

Do i have to copy and paste the codes in my sas program and run?

1 ACCEPTED SOLUTION

Accepted Solutions
Urban_Science
Quartz | Level 8

Hi Statir,

 

I found a link to a similar question someone asked that might help you.  https://communities.sas.com/t5/SAS-Procedures/how-to-winsorize-variable-using-SAS/td-p/94195

 

And yes, you would copy and paste the code into a SAS program and run.

 

Hope this help.

View solution in original post

8 REPLIES 8
Urban_Science
Quartz | Level 8

Hi Statir,

 

I found a link to a similar question someone asked that might help you.  https://communities.sas.com/t5/SAS-Procedures/how-to-winsorize-variable-using-SAS/td-p/94195

 

And yes, you would copy and paste the code into a SAS program and run.

 

Hope this help.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Hi,

 

If your new to SAS, and your unsure of how to run programs, you would be best served by going through the free videos, and looking at the documentation first, before attempting to start running macros.  A macro is bacially a code generation tool, so if you don't understand the requirements, the data, etc. then you will experience problems throught the exercise.

statir
Fluorite | Level 6

Thanks RW9  for your suggestion. I know the basics in sas but I have not worked with macros yet.

I copied the codes in winsorize macro and run the program, the log doesn't show any error but It did not create the output dataset for me. The log is just the repetation of macro codes. Could you please help me what should I do?

Tom
Super User Tom
Super User

 If your log is showing the macro definition then it looks like you have succeeded in defining the macro, now you need to add a line that actually calls the macro.  This is the point where you can provide values for the parameters that are defined in the macro header.

Let's look a a simple example macro:

%macro printit(dataset);
proc print data=&dataset ;
run;
%mend printit;

This macro has one parameter that I need to specify a value for. To call this I need a line like this:

%printit(sashelp.class)

Or if I want to specify the parameters by name I could call it like this:

%printit(dataset=sashelp.class)

 

statir
Fluorite | Level 6

Hi, Thanks for your reply. I think I specified the value. This is the first part of the code. I specified the input , output datsets and the variables that I want to winsorize.

 

Did I do something wrong?

 

/*****************************************
Trim or winsorize macro
* byvar = none for no byvar;
* type = delete/winsor (delete will trim, winsor will winsorize;
*dsetin = dataset to winsorize/trim;
*dsetout = dataset to output with winsorized/trimmed values;
*byvar = subsetting variables to winsorize/trim on;
****************************************/

%macro winsor(dsetin=clean.main11, dsetout=clean.win, byvar=none, vars=mdr bdr, type=winsor, pctl=1 99);

%if &dsetout = %then %let dsetout = &dsetin;

%let varL=;
%let varH=;
%let xn=1;

Tom
Super User Tom
Super User

I do not see a call to the macro in the code snippet you posted. That looks like the macro definition.  When you run the macro definition (%macro .... %mend) you have only DEFINED the macro.  To make the macro run you need to call it. A macro call just has the percent sign and then the macro name.  It does not include the %MACRO statement or the body of the macro.  If the macro is properly written there is no need to modify the macro definition to tell it to use different input files. That should be done by setting values for the parameters in the macro call.

statir
Fluorite | Level 6

Dear Tom,

I called the macro but it did not work. I am sure I did some thing wrong but I dont know where it is.

Could you please see the below codes and help me where is the problem?

 

?
/* ********************************************************************************* */
/* ******************** W R D S R E S E A R C H M A C R O S ******************** */
/* ********************************************************************************* */
/* WRDS Macro: WINSORIZE */
/* Summary : Winsorizes or Trims Outliers */
/* Date : April 14, 2009 */
/* Author : Rabih Moussawi, WRDS */
/* Variables : - INSET and OUTSET are input and output datasets */
/* - SORTVAR: sort variable used in ranking */
/* - VARS: variables to trim and winsorize */
/* - PERC1: trimming and winsorization percent, each tail (default=1%) */
/* - TRIM: trimming=1/winsorization=0, default=0 */
/* ********************************************************************************* */

%MACRO WINSORIZE (INSET=,OUTSET=,SORTVAR=,VARS=,PERC1=1,TRIM=0);

/* List of all variables */
%let vars = %sysfunc(compbl(&vars));
%let nvars = %nwords(&vars);

/* Display Output */
%put ### START.;

/* Trimming / Winsorization Options */
%if &trim=0 %then %put ### Winsorization; %else %put ### Trimming;
%put ### Number of Variables: &nvars;
%put ### List of Variables: &vars;
options nonotes;

/* Ranking within &sortvar levels */
%put ### Sorting... ;
proc sort data=&inset; by &sortvar; run;

/* 2-tail winsorization/trimming */
%let perc2 = %eval(100-&perc1);

%let var2 = %sysfunc(tranwrd(&vars,%str( ),%str(__ )))__;
%let var_p1 = %sysfunc(tranwrd(&vars,%str( ),%str(__&perc1 )))__&perc1 ;
%let var_p2 = %sysfunc(tranwrd(&vars,%str( ),%str(__&perc2 )))__&perc2 ;

/* Calculate upper and lower percentiles */
proc univariate data=&inset noprint;
by &sortvar;
var &vars;
output out=_perc pctlpts=&perc1 &perc2 pctlpre=&var2;
run;

%if &trim=1 %then
%let condition = %str(if myvars(i)>=perct2(i) or myvars(i)<=perct1(i) then myvars(i)=. );
%else %let condition = %str(myvars(i)=min(perct2(i),max(perct1(i),myvars(i))) );

%if &trim=0 %then %put ### Winsorizing at &perc1.%... ;
%else %put ### Trimming at &perc1.%... ;

/* Save output with trimmed/winsorized variables */
data &outset;
merge &inset (in=a) _perc;
by &sortvar;
if a;
array myvars {&nvars} &vars;
array perct1 {&nvars} &var_p1;
array perct2 {&nvars} &var_p2;
do i = 1 to &nvars;
if not missing(myvars(i)) then
do;
&condition;
end;
end;
drop i &var_p1 &var_p2;
run;

/* House Cleaning */
proc sql; drop table _perc; quit;
options notes;

%put ### DONE . ; %put ;

%MEND WINSORIZE;

/* ********************************************************************************* */
/* ************* Material Copyright Wharton Research Data Services *************** */
/* ****************************** All Rights Reserved ****************************** */
/* ********************************************************************************* */
%MACRO WINSORIZE (INSET=clean.main11 ,OUTSET=clean.winsorized ,SORTVAR=none ,VARS=mdr, bdr, ebit_ta, fa_ta ,PERC1=1,TRIM=0);

Tom
Super User Tom
Super User

I don't see where you are calling the macro.  Most of the file is the macro definition and then at the bottom you are starting to define the macro again, but there is no body to the new defintion or any %MEND statement to end the new definition.

 

You need to call the macro and not use another %MACRO statement to begin defining it again.

 

%WINSORIZE 
(INSET=clean.main11 
,OUTSET=clean.winsorized 
,SORTVAR=none 
,VARS=mdr bdr ebit_ta fa_ta 
,PERC1=1
,TRIM=0
);

 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to choose a machine learning algorithm

Use this tutorial as a handy guide to weigh the pros and cons of these commonly used machine learning algorithms.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 8 replies
  • 11189 views
  • 5 likes
  • 4 in conversation