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

Dear all,

I hope that you can help me with the following problem.

My data file contains quarterly earnings surprises.

I want to create quintile-portfolios that are sorted on a quarterly basis.

In other words: I want to have 5 portfolios PF={1,2,3,4,5}.  Each quarter I want to rank the corresponding earnings-surprises and assign them to one of these portfolios. For example PF1 contains the 20% lowest earnings surprises of each period, and PF5 the 20% highest earnings surprises of each period.

I thought about simply including a variable Portfolio that can take the values PF={1,2,3,4,5}, and hence indicates the quintile the earnings surprise is in.

However, I do not know how to do that.

What I look for is the following:

A function that says: variable PF takes the value      1      if surprise<[20%-percentile of surprises in that quarter of that year]

                                                                            2      if [20%-percentile of surprises in that quarter of that year]< surprise < [40%-percentile of surprises in that quarter of that year]

                                                                            3 ..........

Below is a small sample of how the data looks like at the moment.

I would really appreciate it if ssomebody who knows how to solve this problem could help me.

Thank you very much in advance.

                
CUSIPdateSURPRISEquarteryear
20003602020090630-0.02322009
300036020200909300.09632009
40003611020090228-0.00112009
500036110200905310.05722009
600036110200908310.04432009
700036110200911300.07842009
80003611020100228-0.18812010
900036110201005310.02022010
1000036110201008310.18432010
1100036110201011300.29342010
1200081T10200812310.04542008
1300081T10201003311.00012010
1400081T10201006300.09122010
1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data have;
input SURPRISE     quarter     year ;
cards;
-0.023     2     2009
0.096     4     2009
-0.001     1     2009
0.057     2     2009
0.044     4     2009
0.078     4     2009
-0.03     2     2009
0.06     1     2009
-0.01     1     2009
0.57     2     2009
0.44     3     2009
0.08     4     2009
0.023     2     2009
0.06     3     2009
-0.01     1     2009
0.05     2     2009
0.04     3     2009
0.07     4     2009
-0.03     2     2009
0.06     1     2009
0.001     1     2009
0.57     2     2009
0.44     4     2009
0.08     4     2009
-0.3     2     2009
0.6     3     2009
-0.1     1     2009
0.7     2     2009
0.4     3     2009
0.78     4     2009
;
run;
proc sort data=have ;
by year quarter;
run;
proc rank data=have out=want groups=5 ;
by year quarter;
var surprise;
ranks groups;
run;

Ksharp

View solution in original post

2 REPLIES 2
Ksharp
Super User
data have;
input SURPRISE     quarter     year ;
cards;
-0.023     2     2009
0.096     4     2009
-0.001     1     2009
0.057     2     2009
0.044     4     2009
0.078     4     2009
-0.03     2     2009
0.06     1     2009
-0.01     1     2009
0.57     2     2009
0.44     3     2009
0.08     4     2009
0.023     2     2009
0.06     3     2009
-0.01     1     2009
0.05     2     2009
0.04     3     2009
0.07     4     2009
-0.03     2     2009
0.06     1     2009
0.001     1     2009
0.57     2     2009
0.44     4     2009
0.08     4     2009
-0.3     2     2009
0.6     3     2009
-0.1     1     2009
0.7     2     2009
0.4     3     2009
0.78     4     2009
;
run;
proc sort data=have ;
by year quarter;
run;
proc rank data=have out=want groups=5 ;
by year quarter;
var surprise;
ranks groups;
run;

Ksharp

FinanceCandidate
Calcite | Level 5

Thank you Ksharp.

I really appreciate it!

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 2 replies
  • 831 views
  • 2 likes
  • 2 in conversation