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

Hello,

 

I would like to calculate the Herfindahl index each year. There are three steps.

 

1. calculate the total industry sales for each year, which is the sum of sales for each firm each year.

2. calculate the market share square for each firm each year. Market_share_sqr = (sales for each firm each year / total industry sale for that year)^2.

3. select the 50 largest market share each year and add them up.

 

The sample data is below.

 

FirmIDYearSales
100119921
100119932
100119943
100219924
100219935
100219946
100319927
100319938
100319949
1004199210
1004199311
1004199412

 

Want: Since I have 4 firms here, I only select the larget two market shares. In my complete data, there are more than 200 firms and I would like to select the largest 50 firms each year.

 

YearH_Index
19920.307851
19930.273669
19940.25

 

 

What program do I need to use? Thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
unison
Lapis Lazuli | Level 10

Here's a way to go about it

 

data have;
	input firmid year sales;
	datalines;
1001 1992 1
1001 1993 2
1001 1994 3
1002 1992 4
1002 1993 5
1002 1994 6
1003 1992 7
1003 1993 8
1003 1994 9
1004 1992 10
1004 1993 11
1004 1994 12
;
run;

proc sql;
	create table test as select a.firmid, a.year, (a.sales/tot_sales.sales)**2 as 
		market_share_sqr from have a left join (select year, sum(sales) as sales from 
		have group by year) tot_sales on a.year=tot_sales.year order by a.year, 
		market_share_sqr desc;
quit;

proc rank data=test out=ranked(where=(firm_rank<=2)) descending;
	by year;
	var market_share_sqr;
	ranks firm_rank;
run;

proc means nway missing noprint data=ranked;
	class year;
	var market_share_sqr;
	output out=want(drop=_type_ _freq_) sum=;
run;

Change firm_rank<=2 to <=200 if you want top 200.

 

-unison

View solution in original post

2 REPLIES 2
Norman21
Lapis Lazuli | Level 10

This question has already been asked - and answered...

 

https://communities.sas.com/t5/General-SAS-Programming/Calculating-an-Index/td-p/377449

Norman.
SAS 9.4 (TS1M6) X64_10PRO WIN 10.0.17763 Workstation

unison
Lapis Lazuli | Level 10

Here's a way to go about it

 

data have;
	input firmid year sales;
	datalines;
1001 1992 1
1001 1993 2
1001 1994 3
1002 1992 4
1002 1993 5
1002 1994 6
1003 1992 7
1003 1993 8
1003 1994 9
1004 1992 10
1004 1993 11
1004 1994 12
;
run;

proc sql;
	create table test as select a.firmid, a.year, (a.sales/tot_sales.sales)**2 as 
		market_share_sqr from have a left join (select year, sum(sales) as sales from 
		have group by year) tot_sales on a.year=tot_sales.year order by a.year, 
		market_share_sqr desc;
quit;

proc rank data=test out=ranked(where=(firm_rank<=2)) descending;
	by year;
	var market_share_sqr;
	ranks firm_rank;
run;

proc means nway missing noprint data=ranked;
	class year;
	var market_share_sqr;
	output out=want(drop=_type_ _freq_) sum=;
run;

Change firm_rank<=2 to <=200 if you want top 200.

 

-unison

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 3463 views
  • 0 likes
  • 3 in conversation