Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Age of a company.

    I want to calculate the age of a company.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str6 gvkey double fyear
    "147329" 2006
    "001003" 1989
    "128178" 1999
    "128178" 2000
    "021542" 1993
    "021542" 1994
    "021542" 1995
    "021542" 1996
    "021542" 2005
    "021542" 2006
    "001004" 1989
    "001004" 1990
    "001004" 1991
    end

    in output I need to add another column showing age = No of year a specific company (gvkey) is appeared in the data set.
    out put
    gvkey fyear age
    147329 2006 1
    001003 1989 1
    128178 1999 2
    128178 2000 2
    021542 1993 6
    021542 1994 6
    021542 1995 6
    021542 1996 6
    021542 2005 6
    021542 2006 6
    001004 1989 3
    001004 1990 3
    001004 1991 3

  • #2
    Welcome to Statalist, and thank you for presenting a sample of your data using the dataex command.

    Here is an approach to what you need. The automatic variable _N gives the number of observations in the dataset in memory; when combined with the by (or bysort) prefix it gives the number of observations in each group.
    Code:
    . bysort gvkey (fyear): generate age = _N
    
    . list, sepby(gvkey)
    
         +----------------------+
         |  gvkey   fyear   age |
         |----------------------|
      1. | 001003    1989     1 |
         |----------------------|
      2. | 001004    1989     3 |
      3. | 001004    1990     3 |
      4. | 001004    1991     3 |
         |----------------------|
      5. | 021542    1993     6 |
      6. | 021542    1994     6 |
      7. | 021542    1995     6 |
      8. | 021542    1996     6 |
      9. | 021542    2005     6 |
     10. | 021542    2006     6 |
         |----------------------|
     11. | 128178    1999     2 |
     12. | 128178    2000     2 |
         |----------------------|
     13. | 147329    2006     1 |
         +----------------------+
    Last edited by William Lisowski; 10 Jun 2017, 06:29. Reason: Improved the answer.

    Comment


    • #3
      Thank you very much for your reply and welcome msg. I was thinking and thinking how to solve it. after your answer my head is spinning that it was so simple.
      Originally posted by William Lisowski View Post
      Code:
      . bysort gvkey (fyear): generate age = _N
      
      . list, sepby(gvkey)

      Comment


      • #4
        As long as there is no gap in the data!

        Comment


        • #5
          Robert's post raises an important point that I did not comment on. Your post #1 defines "age" as the number of years a company appears in the dataset. While gvkey 021542 appears in the dataset in six years, there is a gap, and overall it appears in the 14-year span from 1993 to 2006. I followed your definition and your example in my code, giving its age as 6 years. But if indeed you wanted the length of the span of years, as Robert interpreted "age", then you would want something like the following.
          Code:
          . bysort gvkey (fyear): generate age = fyear[_N]-fyear[1]+1
          
          . list, sepby(gvkey)
          
               +----------------------+
               |  gvkey   fyear   age |
               |----------------------|
            1. | 001003    1989     1 |
               |----------------------|
            2. | 001004    1989     3 |
            3. | 001004    1990     3 |
            4. | 001004    1991     3 |
               |----------------------|
            5. | 021542    1993    14 |
            6. | 021542    1994    14 |
            7. | 021542    1995    14 |
            8. | 021542    1996    14 |
            9. | 021542    2005    14 |
           10. | 021542    2006    14 |
               |----------------------|
           11. | 128178    1999     2 |
           12. | 128178    2000     2 |
               |----------------------|
           13. | 147329    2006     1 |
               +----------------------+

          Comment

          Working...
          X