DATA Step, Macro, Functions and more

How to create groups by deciles

Reply
Contributor
Posts: 58

How to create groups by deciles

Hello Everyone i am new to sas so fighting long to do a thing which may be simple to you. Kindly help me in this regards

i have just four variables,

PerMNO is firm identifier

Names Date = date (there are 10 years from 2004 to 2014 which i am interested.)

Returns = monthly returns

Mrktcapt = market capitalization

 

What i have am trying to do : 

1) organize this data in decile on the basis of Marktcapt

2) find the mean return and standard deviation of return in each year. 

i have attached a data file and the second sheet in this excel file show a sample for what i am trying to do.

Please give help me with some codes which can do this thing.

Thanks a lot

Super User
Posts: 17,824

Re: How to create groups by deciles

1. Use Proc Rank with group=10 if you want deciles. 

2. Look at Proc means to generate summary statistics. If you need summary statistic by group look at the CLASS or BY statement in Proc means. 

Contributor
Posts: 58

Re: How to create groups by deciles

[ Edited ]

i tried Proc Rank codes 

 

proc rank data=apt.nyse out=rnyse groups=10;
	by mrktcapt;
	var returns;
run;

but i don't think it is right. it converted my returns into groups (1-10) but i want them to be as they are and groups of "Mrktcapt" and plus i want their ranking in each year but i am not able to do it . 

kindly write some line of codes. thanks 

Super User
Posts: 17,824

Re: How to create groups by deciles

Your grouping variable goes into your BY statement. 

 

The var you want the deciles based on goes in your VAR statement. 

 

Add a RANK statement to store your Ranks

 

proc rank data=apt.nyse out=rnyse groups=10;
	by Year;
	var mrktcap;
        rank rank_mrkt_cap;
run;
Contributor
Posts: 58

Re: How to create groups by deciles

THanks Reeza this code worked. 
there is another issue . in my data i have two variables Month and year, i want to define a year which start from July and End with june , how can i do this ? 

thanks again

Super User
Posts: 17,824

Re: How to create groups by deciles

You have to recode this to create a new variable that will resolve to your year of interest.
Or use a custom format, possibly look at multilabel format. 

 

I'd probably create a new variable using if/then code. 

 

 

Super User
Posts: 10,500

Re: How to create groups by deciles

First step when involving date manipulation is often to make sure you have a SAS date value. If you currently have month and year as numeric values then in a datat step:

 

SasDate = mdy(month,1,year);

creates a date that represents the first day of the month.

Then a "modified" year such as you have can be made as:

 

NewYearVar = Year(SasDate) + (month(SASDate) ge 7);

 

These two lines should be in the same data step. Pick your own names for SasDate and NewYearVar.

 

(Looks like a State Fiscal Year to me).

Ask a Question
Discussion stats
  • 6 replies
  • 567 views
  • 2 likes
  • 3 in conversation