Here is a SAS macro for winsorizing data that seems to be commonly used in accounting and finance research. This code comes from replication materials provided by Fang, Huang and Karpoff ("FHK") here. Below I provide an R function that produces equivalent results.
%MACRO winsorize(var, var_out, by_var, left, right, input, output);
%LET var_number = 1;
%LET var&var_number = %QSCAN(&var, &var_number, %STR( ));
%DO %WHILE (&&var&var_number NE);
%LET var_number = %EVAL(&var_number + 1);