Announcement

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

  • What is the Stata command to generate a variable that would take the initial value of the other variable?

    Dear all,

    I have panel covering the period 1970-2010 with country-year being the unit of analysis. The data looks something like this:
    country year X
    A 1970 12
    A 1971 9
    B 1970 8
    B 1971 11
    B 1972 13
    B 1973 4
    C 1970 5
    C 1971 3
    C 1972 8
    I would like to generate a variable X1 in Stata that would take the first value of X( that is in 1970!) for the whole period. So for example, the X1 for country B should be "8" for all years. To illustrate this:
    country year X X1
    A 1970 12 12
    A 1971 9 12
    B 1970 8 8
    B 1971 11 8
    B 1972 13 8
    B 1973 4 8
    C 1970 5 5
    C 1971 5 5
    C 1972 5 5
    I've tried different things with "generate X1= if" what I can't come up how to solve this. Anyone here who can help me with this?

  • #2
    Try
    Code:
    bys country: gen x1 = x[1]
    Alternativley,

    Code:
    sort country year
    by country: gen x1 = x[1]

    Comment


    • #3
      Dear Justin: many thanks! It worked.

      Comment


      • #4
        Originally posted by Justin Blasongame View Post
        Try
        Code:
        bys country: gen x1 = x[1]
        Alternativley,

        Code:
        sort country year
        by country: gen x1 = x[1]
        The second syntax delivers what Faradj Koliev wants to do. The first syntax is
        1. not equivalent to the second syntax,
        2. is very dangerous as it might, or might not deliver the intended outcome.
        3. depends on whatever sort order you found your variables in (so whether it delivers or not, depends on the state in which you found your data).

        Here is an example where the first syntax goes wrong:

        Code:
        . clear
        
        . *(3 variables, 9 observations pasted into data editor)
        
        . bys country: gen x1 = x[1]
        
        . gen u = uniform()
        
        . sort country u
        
        . bys country: gen x11 = x[1]
        
        . list, clean
        
               country   year    x   x1          u   x11  
          1.         A   1971    9   12   .1779098     9  
          2.         A   1970   12   12    .327647     9  
          3.         B   1970    8    8   .2569509     8  
          4.         B   1973    4    8   .6950182     8  
          5.         B   1972   13    8   .7894226     8  
          6.         B   1971   11    8   .7955964     8  
          7.         C   1971    3    5   .2781929     3  
          8.         C   1972    8    5    .461437     3  
          9.         C   1970    5    5   .9061013     3  
        
        .

        Comment


        • #5
          The one line short syntax equivalent to

          Code:
           sort country year
          
          by country: gen x1 = x[1]
          [/CODE]

          is

          Code:
            
           bys country (year): gen x1 = x[1]
          Last edited by Joro Kolev; 12 Jan 2019, 09:23.

          Comment


          • #6
            Thanks, this turned out to be very useful in my case. I thought they all were sorted accordingly but I was wrong.

            Comment


            • #7
              Error on my part. Thanks for catching that Joro Kolev.

              Comment

              Working...
              X