Announcement

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

  • Count year available for unbalanced panel data

    I have id and wave (year) for unbalanced panel data. All I have is 10 years panel but I am only using 4 years for my research. What is the code to count how many years's dummy is 1 for the same id? For example, for id 1, is 2, for id 2 is 2 and id 3 is 1.
    Attached Files

  • #2
    Yiyi:
    you may want to consider something along the following toy-example:
    Code:
    . use "https://www.stata-press.com/data/r17/nlswork.dta"
    (National Longitudinal Survey of Young Women, 14-24 years old in 1968)
    
    . bysort idcode: egen wanted=count(year)
    
    .
    Before that, yoiu should create a single column for you data waves.
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      Originally posted by Carlo Lazzaro View Post
      Yiyi:
      you may want to consider something along the following toy-example:
      Code:
      . use "https://www.stata-press.com/data/r17/nlswork.dta"
      (National Longitudinal Survey of Young Women, 14-24 years old in 1968)
      
      . bysort idcode: egen wanted=count(year)
      
      .
      Before that, yoiu should create a single column for you data waves.
      Thank you Carlo.

      My code is

      . gen num_year=dwave6+dwave7+dwave8+dwave9
      (24 missing values generated)

      . bysort idind: egen num_wave=count( num_year )
      invalid syntax
      r(198);

      There is a mistake to run

      Comment


      • #4
        Yiyi:
        see:
        Code:
        . set obs 4
        Number of observations (_N) was 0, now 4.
        
        . g id=1 in 1/2
        
        . replace id=2 if id==.
        
        . g wave_1=1 in 1/3
        
        . replace wave_1=0 if wave_1==.
        
        . g wave_2=0 in 1/3
        
        . replace wave_2=1 if wave_2==.
        
        . list
        
             +----------------------+
             | id   wave_1   wave_2 |
             |----------------------|
          1. |  1        1        0 |
          2. |  1        1        0 |
          3. |  2        1        0 |
          4. |  2        0        1 |
             +----------------------+
        
        . egen wanted=rowtotal(wave_*)
        
        . list
        
             +-------------------------------+
             | id   wave_1   wave_2   wanted |
             |-------------------------------|
          1. |  1        1        0        1 |
          2. |  1        1        0        1 |
          3. |  2        0        1        1 |
          4. |  2        1        0        1 |
             +-------------------------------+
        
        .
        Kind regards,
        Carlo
        (Stata 19.0)

        Comment


        • #5
          Originally posted by Carlo Lazzaro View Post
          Yiyi:
          see:
          Code:
          . set obs 4
          Number of observations (_N) was 0, now 4.
          
          . g id=1 in 1/2
          
          . replace id=2 if id==.
          
          . g wave_1=1 in 1/3
          
          . replace wave_1=0 if wave_1==.
          
          . g wave_2=0 in 1/3
          
          . replace wave_2=1 if wave_2==.
          
          . list
          
          +----------------------+
          | id wave_1 wave_2 |
          |----------------------|
          1. | 1 1 0 |
          2. | 1 1 0 |
          3. | 2 1 0 |
          4. | 2 0 1 |
          +----------------------+
          
          . egen wanted=rowtotal(wave_*)
          
          . list
          
          +-------------------------------+
          | id wave_1 wave_2 wanted |
          |-------------------------------|
          1. | 1 1 0 1 |
          2. | 1 1 0 1 |
          3. | 2 0 1 1 |
          4. | 2 1 0 1 |
          +-------------------------------+
          
          .
          Thank you Carlo.

          What about this one:
          Click image for larger version

Name:	1.png
Views:	1
Size:	19.0 KB
ID:	1721874


          I have community ID (commid) and several waves. What I am interested in is wave 2004 and wave 2011. Dis_2004 means the distance between market and community in 2004 and dis_2011 is the distance between market and community in 2011. What I would like to calculate is the change of distance between 2004 and 2011 for each community. For example, the change in the distance between 2004 and 2011 for community 211101 is 0-0=0, the change in the distance between 2004 and 2011 for community 211102 is 0-10=-10,...
          What is the code for that? I tried code bysort commid : gen dis_diff= dis_2011 - dis_2004, but it does not work.
          Thank you!

          Comment


          • #6
            Yiyi:
            why not considering the following toy-example?
            Code:
            . set obs 2
            Number of observations (_N) was 0, now 2.
            
            . g id=1
            
            . g year=2004 in 1
            
            . replace year=2011 in 2
            
            . expand 2
            
            . replace id=2 in 3/4
            
            . bysort id (year): g wanted=uniform()
            
            . bysort id (year): g diff= wanted[2]- wanted[1] if _n==1
            
            . list
            
                 +----------------------------------+
                 | id   year     wanted        diff |
                 |----------------------------------|
              1. |  1   2004   .3488717    -.081986 |
              2. |  1   2011   .2668857           . |
              3. |  2   2004   .1366463   -.1080894 |
              4. |  2   2011   .0285569           . |
                 +----------------------------------+
            
            .
            Kind regards,
            Carlo
            (Stata 19.0)

            Comment

            Working...
            X