Announcement

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

  • Issue with enerating difference within a panel variable

    Hi,

    I have a panel of 40 countries with 30 years. I need to generate a new variable like in the table:
    country year var1 new variable
    A 2000 a00 a00-c00
    A 2001 a01 a01-c01
    B 2000 b00 b00-c00
    B 2001 b01 b01-c01
    C 2000 c00 0
    C 2001 c01 0


    I have two ideas how to do it, but I don't know the command.
    First idea: use bysort country (year): var1-vector(c00,c01), but I don't know how to tell stata to use the vector(c00,c01)
    Second idea: generate new variable repeating the vector(c00,c01) and than subtract this new variable from var1. Again I don't know how to generate this variable of the repeating sequence.

    I will need a simple solution since the reference country (the data that I subtract) will be changing.

    Many thanks in advance for any help!

    Best,
    Martin Stoyanov


  • #2
    You have two zeros in the new variable and I don't know where they come from (typo?). Maybe something like:
    Code:
    clear
    set more off
    
    input ///
    str1 country     year     str3 var str7 newvar
    A     2000     a00     a00-c00
    A     2001     a01     a01-c01
    B     2000     b00     b00-c00
    B     2001     b01     b01-c01
    C     2000     c00     0
    C     2001     c01     0
    end
    
    list, sep(0)
    
    gen newvar2 =  var + "-c" + substr(var, -2, .)
    
    list, sep(0)
    ?

    If not, then you need to clarify the rule for generating the new variable. See also - help string functions-.

    Note there is a strong preference for real full names in this list. You can ask site administrators to change your user name hitting the "contact us" button at the bottom of the page.
    You should:

    1. Read the FAQ carefully.

    2. "Say exactly what you typed and exactly what Stata typed (or did) in response. N.B. exactly!"

    3. Describe your dataset. Use list to list data when you are doing so. Use input to type in your own dataset fragment that others can experiment with.

    4. Use the advanced editing options to appropriately format quotes, data, code and Stata output. The advanced options can be toggled on/off using the A button in the top right corner of the text editor.

    Comment


    • #3
      So now I see this is actually a numerical computation...

      I still think you need to clarify the rule for your new variable. Are you just subtracting the values of the last country from all countries (by year)?
      Last edited by Roberto Ferrer; 24 Oct 2014, 08:18.
      You should:

      1. Read the FAQ carefully.

      2. "Say exactly what you typed and exactly what Stata typed (or did) in response. N.B. exactly!"

      3. Describe your dataset. Use list to list data when you are doing so. Use input to type in your own dataset fragment that others can experiment with.

      4. Use the advanced editing options to appropriately format quotes, data, code and Stata output. The advanced options can be toggled on/off using the A button in the top right corner of the text editor.

      Comment


      • #4
        Roberto Ferrer is spot on here. This is a guessing game. We can probably work out code for you, but the rule you are using just is not clear at all.

        Comment


        • #5
          Sorry for not being clear..
          Yes, it is a numerical computation. I have to subtract the values of country C from each other country by year. Country C is my reference country in this case. The problem is that the values of C are part of the variable from which I have to subtract them, This is way I have the zeros for country C.

          Comment


          • #6
            Thanks for the clarification. Now it's clear.

            I wrote this

            http://www.stata-journal.com/sjpdf.h...iclenum=dm0055

            as a guide to technique in this territory.

            Comment


            • #7
              I really appreciate the help!

              I just went over the article, but I couldn't find a code which takes the exact values of a group - in my case the exact values of country C. What I need is similar to:

              .bysort country (year): gen NewVariable=var1-var1[country=="C"]

              When I generate it this way, the result is a new variable consisting only of missing values. The second part is trying to tell Stata to subtract each time the values for country C, but apparently it doesn't work this way. I tried using

              .bysort country (year): gen new=var1-var1 if country=="C"

              but then Stata estimated the difference only for country C, which are zeros, and the rest is missing values. The multiplication that you discuss in the article, works similarly to the "if" approach

              .gen basevalue = sum((year == 1960) * le_female)

              but it doesn't help unfortunately.

              Do you have another idea or did I miss a command in the article? Thanks again!
              Last edited by martin_s_bg; 24 Oct 2014, 10:06.

              Comment


              • #8
                Yes to both parts of your last question. One way to make country C your reference is

                Code:
                egen var1C = total(var1 / (country == "C")), by(year)
                replace var1C = var1 - var1C
                Another is

                Code:
                gen var1C = var1 if country == "C"
                bysort year (var1C) : replace var1C = var1C[_n-1] if missing(var1C)
                replace var1C = var1 - var1C
                Looking at your code, I think you have been using R.... In

                Code:
                 
                bysort country (year): gen NewVariable=var1-var1[country=="C"]
                country=="C" evaluates as 0 or 1 and that's legal but not going to help here.



                Last edited by Nick Cox; 24 Oct 2014, 10:29.

                Comment


                • #9
                  Thank you! Works perfectly.

                  Comment

                  Working...
                  X