BookmarkSubscribeRSS Feed
xyxu
Quartz | Level 8

I am trying to winsorize a variable using the winsorization macro provided by WRDS, which can be seen here: https://gist.github.com/mgao6767/736968d5b3a6b9c11df76624c98e5d50

 

I call the macro with

%include "&dir\Code\winsorize.sas";
%winsorize(inset = have, outset = want, sortvar = region, vars = number, perc1 = 2.5, trim = 0);

and get error messages:


WARNING: Apparent invocation of macro NWORDS not resolved.
### START.
### Winsorization
WARNING: Apparent invocation of macro NWORDS not resolved.
### Number of Variables: %nwords(flow)
### List of Variables: flow
### Sorting...
ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The
condition was: 100-2.5
ERROR: The macro WINSORIZE will stop executing.

 

 

7 REPLIES 7
sbxkoenk
SAS Super FREQ

Hello,

 

Probably you need to submit the NWORDS macro definition first (i.e. compiling the macro such that you can call it afterwards).

You should be able to find the NWORDS macro definition on the

Wharton Research Data Services (WRDS) website.

If not I can send it to you in a private mail.

 

The WRDS Macro NWORDS
counts the Number of Words Within a Text String

and was written by Mark Keintz, WRDS (also active on these communities).

 

Kind regards,

Koen

xyxu
Quartz | Level 8

Thanks! Perhaps the way I use these macros is wrong. Here is how I tried to compile them:

 

%include "&dir\Code\nwords.sas";
%include "&dir\Code\winsorize.sas";
%winsorize(inset = have, outset = want, sortvar = region, vars = number, perc1 = 2.5, trim = 0);

Now the messages are:

 

 

### START.
### Winsorization
### Number of Variables: 1
### List of Variables: number
### Sorting...
ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The
condition was: 100-2.5
ERROR: The macro WINSORIZE will stop executing.

sbxkoenk
SAS Super FREQ

Hello,

 

It's perfectly fine to %INCLUDE the macro definition in order to compile it. I guess your &dir exists?

You can do a 

     %PUT &=dir;

to find out.

 

No idea what has gone wrong. Maybe @mkeintz can help you out.

Calling out to @mkeintz .

 

Good luck,

Koen

xyxu
Quartz | Level 8
Yes, my dir works. Not sure what causes the error here.
sbxkoenk
SAS Super FREQ

Hello,

 

Not sure about what that macro does, but you can also program the Winsorized stuff yourself.

  • Winsorized means are in PROC UNIVARIATE and in PROC CAPABILITY (SAS/QC).
  • PROC HPBIN has a Winsorized binning method.

    Winsorize is also in the Data Science Pilot Action Set.

     

    Cheers,

    Koen

ballardw
Super User

When using macros you can get a lot more detail in the log by adding some options. The System options MPRINT SYMBOLGEN and MLOGIC will provide details about the code generated, the steps in building macro variable values and the comparisons in the macro logic.

 

You would submit code such as this to turn the options, run the code then turn the option off.

 

options mprint; 

%winsorize(inset = have, outset = want, sortvar = region, vars = number, perc1 = 2.5, trim = 0);

options nomprint;

Now you will have more details in the log about the code generated.

 

I don't use this macro but the sort of message you get is typically the result of either not providing the correct option in one of the parameters or possibly an unexpected character in one of your parameters. I might suggest removing the space before the 2.5 in the macro call in case it is something other than an actual simple blank character which might happen when copy and pasting text from some sources. If that doesn't help then show us all the, yes it gets ugly, generated from the macro call that the MPRINT option creates. We may then suggest if SYMBOLGEN or MLOGIC may be needed.

 

mkeintz
PROC Star

The WINSORISE macro at WRDS calls another macro, also at WRDS, named NWORDS.  You didn't have the NWORDS macro, so it failed.

 

The NWORDS macro was developed a long time ago, prior to the availability of the COUNTW function.

 

So you should be able to replace

 

%let nvars = %nwords(&vars);

with

 

%let nvars = %sysfunc(countw(&vars));
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 1674 views
  • 4 likes
  • 4 in conversation