Announcement

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

  • Filling in missing variables for a group

    Hello,

    I am trying to fill in the same value for each missing variable within an id (pid in my dataex example below). So the variables ad1, ad2, etc. should all be the same value for each pid.

    Looking at previous Stata forum posts, I feel like I should be able to use the code:
    bysort pid(ad1):replace ad1=ad1[1] if missing(ad1)

    Or the code:

    bysort pid(ad10):replace ad10=ad10[1]

    However, when I try these I am getting the error "coding operators not allowed."

    I also tried to install xfill but the link doesn't work.

    Any advice?

    Thank you,

    Sarah


    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input long pid float(ad1 ad2 ad3 ad4 ad5 ad6 ad7 ad8 ad9 ad10)
    1000006         0        .         .        .          .          .        .          .        .         .
    1000006         . .2142857         .        .          .          .        .          .        .         .
    1000006         .        .         1        .          .          .        .          .        .         .
    1000006         .        .         . .5714286          .          .        .          .        .         .
    1000006         .        .         .        . .071428575          .        .          .        .         .
    1000006         .        .         .        .          .   .9285714        .          .        .         .
    1000006         .        .         .        .          .          .        1          .        .         .
    1000006         .        .         .        .          .          .        .          1        .         .
    1000006         .        .         .        .          .          .        .          .        1         .
    1000006         .        .         .        .          .          .        .          .        .  .2142857
    1000006         .        .         .        .          .          .        .          .        .         .
    1000006         .        .         .        .          .          .        .          .        .         .
    1000006         .        .         .        .          .          .        .          .        .         .
    1000008 .07692308        .         .        .          .          .        .          .        .         .
    1000008         . .4285714         .        .          .          .        .          .        .         .
    1000008         .        .  .2142857        .          .          .        .          .        .         .
    1000008         .        .         . .2857143          .          .        .          .        .         .
    1000008         .        .         .        .   .3571429          .        .          .        .         .
    1000008         .        .         .        .          .   .2142857        .          .        .         .
    1000008         .        .         .        .          .          .        0          .        .         .
    1000008         .        .         .        .          .          .        . .071428575        .         .
    1000008         .        .         .        .          .          .        .          .        0         .
    1000008         .        .         .        .          .          .        .          .        . .14285715
    1000008         .        .         .        .          .          .        .          .        .         .
    1000008         .        .         .        .          .          .        .          .        .         .
    1000008         .        .         .        .          .          .        .          .        .         .
    1000012  .7857143        .         .        .          .          .        .          .        .         .
    1000012         .        1         .        .          .          .        .          .        .         .
    1000012         .        . .14285715        .          .          .        .          .        .         .
    1000012         .        .         . .7857143          .          .        .          .        .         .
    1000012         .        .         .        .   .4285714          .        .          .        .         .
    1000012         .        .         .        .          .          0        .          .        .         .
    1000012         .        .         .        .          .          .        0          .        .         .
    1000012         .        .         .        .          .          .        .          0        .         .
    1000012         .        .         .        .          .          .        .          .        0         .
    1000012         .        .         .        .          .          .        .          .        .         0
    1000012         .        .         .        .          .          .        .          .        .         .
    1000012         .        .         .        .          .          .        .          .        .         .
    1000012         .        .         .        .          .          .        .          .        .         .
    1000048  .4285714        .         .        .          .          .        .          .        .         .
    1000048         .        0         .        .          .          .        .          .        .         .
    1000048         .        .  .5714286        .          .          .        .          .        .         .
    1000048         .        .         .        0          .          .        .          .        .         .
    1000048         .        .         .        . .071428575          .        .          .        .         .
    1000048         .        .         .        .          .         .5        .          .        .         .
    1000048         .        .         .        .          .          .        0          .        .         .
    1000063         0        .         .        .          .          .        .          .        .         .
    1000063         . .6428571         .        .          .          .        .          .        .         .
    1000063         .        .  .8571429        .          .          .        .          .        .         .
    1000063         .        .         . .8571429          .          .        .          .        .         .
    1000063         .        .         .        .   .8571429          .        .          .        .         .
    1000063         .        .         .        .          .   .6428571        .          .        .         .
    1000063         .        .         .        .          .          .        1          .        .         .
    1000063         .        .         .        .          .          .        .  .53846157        .         .
    1000063         .        .         .        .          .          .        .          . .4285714         .
    1000063         .        .         .        .          .          .        .          .        .        .5
    1000063         .        .         .        .          .          .        .          .        .         .
    1000063         .        .         .        .          .          .        .          .        .         .
    1000070  .4285714        .         .        .          .          .        .          .        .         .
    1000070         .       .5         .        .          .          .        .          .        .         .
    1000070         .        .  .4285714        .          .          .        .          .        .         .
    1000070         .        .         .        1          .          .        .          .        .         .
    1000070         .        .         .        .   .9285714          .        .          .        .         .
    1000070         .        .         .        .          .   .9285714        .          .        .         .
    1000070         .        .         .        .          .          . .9285714          .        .         .
    1000070         .        .         .        .          .          .        .   .7142857        .         .
    1000070         .        .         .        .          .          .        .          . .9285714         .
    1000070         .        .         .        .          .          .        .          .        .  .9285714
    1000070         .        .         .        .          .          .        .          .        .         .
    1000070         .        .         .        .          .          .        .          .        .         .
    1000070         .        .         .        .          .          .        .          .        .         .
    1000081         0        .         .        .          .          .        .          .        .         .
    1000081         . .2857143         .        .          .          .        .          .        .         .
    1000081         .        .         0        .          .          .        .          .        .         .
    1000081         .        .         .        0          .          .        .          .        .         .
    1000081         .        .         .        .          0          .        .          .        .         .
    1000081         .        .         .        .          .          0        .          .        .         .
    1000081         .        .         .        .          .          .        0          .        .         .
    1000081         .        .         .        .          .          .        .          0        .         .
    1000081         .        .         .        .          .          .        .          .        0         .
    1000089         0        .         .        .          .          .        .          .        .         .
    1000089         .        0         .        .          .          .        .          .        .         .
    1000089         .        .  .2142857        .          .          .        .          .        .         .
    1000089         .        .         .        0          .          .        .          .        .         .
    1000089         .        .         .        .   .2142857          .        .          .        .         .
    1000089         .        .         .        .          . .071428575        .          .        .         .
    1000089         .        .         .        .          .          .        1          .        .         .
    1000089         .        .         .        .          .          .        .   .6428571        .         .
    1000089         .        .         .        .          .          .        .          . .9285714         .
    1000089         .        .         .        .          .          .        .          .        .  .6428571
    1000089         .        .         .        .          .          .        .          .        .         .
    1000089         .        .         .        .          .          .        .          .        .         .
    1000089         .        .         .        .          .          .        .          .        .         .
    1000089         .        .         .        .          .          .        .          .        .         .
    1000098         0        .         .        .          .          .        .          .        .         .
    1000098         . .2857143         .        .          .          .        .          .        .         .
    1000098         .        .  .9285714        .          .          .        .          .        .         .
    1000098         .        .         . .7857143          .          .        .          .        .         .
    1000098         .        .         .        .   .9285714          .        .          .        .         .
    1000098         .        .         .        .          .   .9285714        .          .        .         .
    end

  • #2
    You need a blank space between pid and (ad10) (or adwhatever) in the -bysort-.

    By the way, you can probably do this more easily as:

    Code:
    collapse (firstnm) ad*, by(pid)

    Comment


    • #3
      Hi,

      I'm trying to fill the missing cells of the variable "Classtype".

      I have 3 dimensions: CompCountryName, Groupe and Year.

      I want to be able to fill the missings in the variable "Classtype" by country, Group and Year.
      My variable Classtype can have 6 different types:
      1. Large Metropolitan
      2. Medium Sized
      3. Metropolitan
      4. Out Metropolitan
      5. Small Sized
      6. Very Small Sized

      For example, we can see in the data below, that Groupe 1 in Austria has 3 types during the year 2002. I would like to fill the 3 other missings (regardless of the order)
      1. Out Metropolitan
      2. Small Sized
      3. Very Small Sized
      Thank you a lot !


      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input str20 CompCountryName float(Groupe Year) str18 Classtype
      "Austria" 1 2002 "Metropolitan"      
      "Austria" 1 2002 "Medium Sized"      
      "Austria" 1 2002 "Large Metropolitan"
      "Austria" 1 2002 "."                 
      "Austria" 1 2002 "."                 
      "Austria" 1 2002 "."                 
      "Austria" 1 2016 "Large Metropolitan"
      "Austria" 1 2016 "Metropolitan"      
      "Austria" 1 2016 "Medium Sized"      
      "Austria" 1 2016 "."                 
      "Austria" 1 2016 "."                 
      "Austria" 1 2016 "."                 
      "Austria" 2 2002 "Large Metropolitan"
      "Austria" 2 2002 "Out Metropolitan"  
      "Austria" 2 2002 "Medium Sized"      
      "Austria" 2 2002 "Metropolitan"      
      "Austria" 2 2002 "Small Sized"       
      "Austria" 2 2002 "."                 
      "Austria" 2 2016 "Large Metropolitan"
      "Austria" 2 2016 "Metropolitan"      
      "Austria" 2 2016 "."                 
      "Austria" 2 2016 "."                 
      "Austria" 2 2016 "."                 
      "Austria" 2 2016 "."                 
      "Belgium" 1 2002 "Large Metropolitan"
      "Belgium" 1 2002 "Medium Sized"      
      "Belgium" 1 2002 "Small Sized"       
      "Belgium" 1 2002 "Very Small Sized"  
      "Belgium" 1 2002 "Metropolitan"      
      "Belgium" 1 2002 "Out Metropolitan"  
      "Belgium" 1 2016 "Very Small Sized"  
      "Belgium" 1 2016 "Out Metropolitan"  
      "Belgium" 1 2016 "Small Sized"       
      "Belgium" 1 2016 "Large Metropolitan"
      "Belgium" 1 2016 "Medium Sized"      
      "Belgium" 1 2016 "Metropolitan"      
      "Belgium" 2 2002 "Metropolitan"      
      "Belgium" 2 2002 "Small Sized"       
      "Belgium" 2 2002 "Medium Sized"      
      "Belgium" 2 2002 "Out Metropolitan"  
      "Belgium" 2 2002 "Large Metropolitan"
      "Belgium" 2 2002 "."                 
      "Belgium" 2 2016 "Metropolitan"      
      "Belgium" 2 2016 "Medium Sized"      
      "Belgium" 2 2016 "Small Sized"       
      "Belgium" 2 2016 "Out Metropolitan"  
      "Belgium" 2 2016 "Very Small Sized"  
      "Belgium" 2 2016 "Large Metropolitan"
      "Denmark" 1 2002 "Large Metropolitan"
      "Denmark" 1 2002 "Metropolitan"      
      "Denmark" 1 2002 "Small Sized"       
      "Denmark" 1 2002 "Very Small Sized"  
      "Denmark" 1 2002 "Medium Sized"      
      "Denmark" 1 2002 "Out Metropolitan"  
      "Denmark" 2 2002 "Small Sized"       
      "Denmark" 2 2002 "Metropolitan"      
      "Denmark" 2 2002 "Out Metropolitan"  
      "Denmark" 2 2002 "Very Small Sized"  
      "Denmark" 2 2002 "Medium Sized"      
      "Denmark" 2 2002 "Large Metropolitan"
      "Denmark" 2 2016 "Very Small Sized"  
      "Denmark" 2 2016 "Medium Sized"      
      "Denmark" 2 2016 "Out Metropolitan"  
      "Denmark" 2 2016 "Large Metropolitan"
      "Denmark" 2 2016 "Small Sized"       
      "Denmark" 2 2016 "."                 
      "Finland" 1 2002 "Medium Sized"      
      "Finland" 1 2002 "Small Sized"       
      "Finland" 1 2002 "Large Metropolitan"
      "Finland" 1 2002 "Out Metropolitan"  
      "Finland" 1 2002 "Metropolitan"      
      "Finland" 1 2002 "."                 
      "Finland" 1 2016 "Out Metropolitan"  
      "Finland" 1 2016 "Metropolitan"      
      "Finland" 1 2016 "Small Sized"       
      "Finland" 1 2016 "Medium Sized"      
      "Finland" 1 2016 "Large Metropolitan"
      "Finland" 1 2016 "."                 
      "France"  1 2002 "Large Metropolitan"
      "France"  1 2002 "Small Sized"       
      "France"  1 2002 "Metropolitan"      
      "France"  1 2002 "Out Metropolitan"  
      "France"  1 2002 "Very Small Sized"  
      "France"  1 2002 "Medium Sized"      
      "France"  1 2016 "Out Metropolitan"  
      "France"  1 2016 "Very Small Sized"  
      "France"  1 2016 "Large Metropolitan"
      "France"  1 2016 "Metropolitan"      
      "France"  1 2016 "Medium Sized"      
      "France"  1 2016 "Small Sized"       
      "France"  2 2002 "Medium Sized"      
      "France"  2 2002 "Large Metropolitan"
      "France"  2 2002 "Very Small Sized"  
      "France"  2 2002 "Small Sized"       
      "France"  2 2002 "Out Metropolitan"  
      "France"  2 2002 "Metropolitan"      
      "France"  2 2016 "Small Sized"       
      "France"  2 2016 "Out Metropolitan"  
      "France"  2 2016 "Metropolitan"      
      "France"  2 2016 "Very Small Sized"  
      end

      Comment


      • #4
        Code:
        drop if Classtype == "."
        fillin CompCountryName Groupe Year Classtype

        By the way, in the future, avoid using "." to denote missing value for a string variable. Stata's missing value for strings is "", and if you use that you will be able to use the -missing()- function and have predictable sorting of missing string values to the top.


        Comment


        • #5
          Thank you a lot Clyde !
          I really appreciate it.

          Best,

          Comment

          Working...
          X