Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

RE: st: RE: Fwd: Neverending loop....


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   RE: st: RE: Fwd: Neverending loop....
Date   Fri, 13 Dec 2002 12:04:56 -0000

Roberto Mura

> I have some ownership data of italian listed firms; it is
> very detailed. I
> have 60 ownership variables (own1-own60)containing shares, plus 60
> regarding the types of shareholders (type1-type60)....and
> others that are
> now irrelevant.
> My problem is to re-rank all of these since it is very
> helpful to have an
> idea about the largest shareholders, second largest and so on.

I think that I now understand the problem.

I am sure that you should not need to write your
own code to sort anything in Stata.

Your problem sounds like a need for a row sort on
the -own*-. You could do that in place if your values are all
integers,
using -rowsort- from SSC. But that won't take care
of your -type*- which I think you want to -sort-
simultaneously.

Thus I recommend this strategy:

-reshape-
-sort-
-reshape-

The approach of
	mapping to a different space
	doing stuff in that space
	mapping back
is very powerful: think logarithms, Fourier transforms, or
whatever, depending on your background.

You should have a firm identifier variable. Here is
a minimal analogue of what I understand your data
to be like:

. l

          firm      own1      own2      own3      type1      type2
type3
  1.         a        14        26        28          Z          q
z
  2.         b        65         5        12          Y          r
y
  3.         c        56        43        41          X          s
x
  4.         d        61        90        73          W          t
w
  5.         e        69        53        88          V          u
v
  6.         f        11        85        47          U          v
u
  7.         g        62        22        43          T          w
t
  8.         h         7        57        90          S          x
s
  9.         i        56        27         6          R          y
r
 10.         j        88        95        68          Q          z
q

We -reshape- to -long-:

. reshape long own type , i(firm)
(note: j = 1 2 3)

Data                               wide   ->   long
----------------------------------------------------------------------
-------
Number of obs.                       10   ->      30
Number of variables                   7   ->       4
j variable (3 values)                     ->   _j
xij variables:
                         own1 own2 own3   ->   own
                      type1 type2 type3   ->   type
----------------------------------------------------------------------
-------

. l

          firm         _j       own       type
  1.         a          1        14          Z
  2.         a          2        26          q
  3.         a          3        28          z
  4.         b          1        65          Y
< snip >

 25.         i          1        56          R
 26.         i          2        27          y
 27.         i          3         6          r
 28.         j          1        88          Q
 29.         j          2        95          z
 30.         j          3        68          q

Now we calculate a rank (N.B. replace 4 as
appropriate, or use -egen, rank() unique- instead):

. bysort firm (own) : gen rank  = 4 - _n

. sort firm rank

. keep firm own type rank

. l

          firm       own       type       rank
  1.         a        28          z          1
  2.         a        26          q          2
  3.         a        14          Z          3
  4.         b        65          Y          1
  5.         b        12          y          2
  6.         b         5          r          3
< snip >
 28.         j        95          z          1
 29.         j        88          Q          2
 30.         j        68          q          3

.  reshape wide own type , i(firm) j(rank)


. l

          firm      own1      type1      own2      type2      own3
type3
  1.         a        28          z        26          q        14
Z
  2.         b        65          Y        12          y         5
r
  3.         c        56          X        43          s        41
x
  4.         d        90          t        73          w        61
W
  5.         e        88          v        69          V        53
u
  6.         f        85          v        47          u        11
U
  7.         g        62          T        43          t        22
w
  8.         h        90          s        57          x         7
S
  9.         i        56          R        27          y         6
r
 10.         j        95          z        88          Q        68
q

Here's the code again:

reshape long own type , i(firm)
* !!! replace 4 as appropriate by #firms + 1
bysort firm (own) : gen rank  = 4 - _n
keep firm own type rank
reshape wide own type , i(firm) j(rank)

Check out [R] reshape and also

I am having problems with the reshape command.
Can you give further guidance?
http://www.stata.com/support/faqs/data/reshape3.html

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/



© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index