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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
6 REPLIES 6
PaigeMiller
Diamond | Level 26

PROC UNIVARIATE has a WINSORIZED= option, seems like that is what you need.

https://documentation.sas.com/?cdcId=pgmsascdc&cdcVersion=9.4_3.4&docsetId=procstat&docsetTarget=pro...

--
Paige Miller
Ksharp
Super User

@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;
kelxxx
Quartz | Level 8
ees, banj cos gif lieen lacj ko, zalo? giao luwu hocj taapj naof. sr neeus lamf phieenf nhes. neeus banj cos sas iml thif lamf nhuw anh been duowis chir, neeus khoong cos thif pm minhf.
Baolinhdo
Calcite | Level 5
Hi, minh ko co zalo sorry b
Thank you nhe
Rick_SAS
SAS Super FREQ

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."

 

Baolinhdo
Calcite | Level 5
Thanks! I wonder how the part “from the start of the sample until time t” will be resolved? Should I use a do loop?

It seems like the most critical part in my question is until time t for each observation rather than winsorising the whole sample at once?

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 982 views
  • 1 like
  • 5 in conversation