# st: RE: how to generate groups based on some characteristics and obtain the mean/median value for each group

 From "Nick Cox" To Subject st: RE: how to generate groups based on some characteristics and obtain the mean/median value for each group Date Wed, 19 Jun 2002 11:01:35 +0100

```Yi, Bingsheng

> I wonder whether you will help me figure out the codes to solve the
> following problem:
>
> I have  12 years panel data containing these four variables: Tobin's q,
> size, 4-digit industry code (ind4), and id. For each year, I want to make
> some adjusments in one variable (Tobin's q) based on the other
> two variables
> (industry and size). First I need to ensure that there are lat least 10
> firms within each industry. If the number of firms within a
> 4-digit industry
> code is less than 10, I use 3-digit industry code generated by gen str4
> ind3=substr(ind4,1,3), see whether the number of firms with the
> same 3-digit
> industry code is greater or equal to 10, if not, then generate and use
> 2-digit  industry code. So in the end there are at least 10 firms
> within an
> industry ( which are classified by 4-digit, 3-digit, 2-digit, or 1-digit
> industry code). The  problem is how to get and record the number
> of firms in
> each industry.

bysort ind4 (size) : gen freq = _N

> Then within each industry, I want  classify firms into three
> groups based on
> size, the smallest group contains the smallest 30% firms in size
> within that
> industry, the middle group contains the middle 40% firms in size (30% to
> 70%), and the large group contain firms whose size belongs to the largest
> 30% in that industry. Then I get the median or mean value of Tobin's q for
> each of these groups within an industry.
>
> the industry-size adjusted Tobin's q = Tobin's q of a firm in an
> industry -
> the mean tobin's q of a group to which the firm belongs within the same
> industry
>
> For example, if IBM belongs to the largest size group in computer
> industry,
> then the industry-size adjusted Tobin's q of IBM is equal to
> Tobin's q of IBM - the mean/median value of the largest size group within
> the computer industry.
>
> But I don't know how to generate these groups and obtain the industry-size
> adjusted Tobin's q for a sample firm. I will greatly appreciate

Within industry, you have sorted on -size- if you use the
command above.

by ind4 : gen .... if _n <= 0.3 * _N

gives the lowest 30%, etc.

Nick
n.j.cox@durham.ac.uk

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```