Hi, I am a new SAS user, I have panel data with many months and many stocks each month. I also have 10 other variables associated with each stock each month.I want to winsorise data following the instruction below:
-For a particular observation, for example, Var_1(i,t) (Variable 1 of stock i in month t), to winsorize it, first calculate the 5th and 95th percentiles of that variable in the dataset that includes all stocks and all time periods from the start of the sample up until time t.
-If Var_1(i,t) is less than the 5th percentile, set its value to the 5th percentile value. If Var_1(i,t) is greater than the 95th percentile, set its value to the 95th percentile value.
-Repeat for all observations.
-Repeat for all variables.
Thank you.
First read "Winsorization: The good, the bad, and the ugly."
After reading, if you still want to Winsorize , see "How to Winsorize data in SAS."
PROC UNIVARIATE has a WINSORIZED= option, seems like that is what you need.
@Rick_SAS wrote a blog before.
Here is winsorized data code , JUST for one group .
And you need SAS/IML .
data have; /*Winsorized Data*/
do i=1 to 100;
a=ceil(ranuni(1)*100);
b=ceil(ranuni(2)*100);
output;
end;
drop i;
run;
%let low=0.05 ;
%let high=0.95 ;
proc iml;
use have;
read all var _num_ into x[c=vname];
close have;
call qntl(q,x,{&low ,&high});
do i=1 to ncol(x);
x[loc(x[,i]<q[1,i]),i]=q[1,i];
x[loc(x[,i]>q[2,i]),i]=q[2,i];
end;
create want from x[c=vname];
append from x;
close want;
quit;
First read "Winsorization: The good, the bad, and the ugly."
After reading, if you still want to Winsorize , see "How to Winsorize data in SAS."
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.