Announcement

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

  • Assigning one data observation to a local macro

    Dear

    I'm trying to assig one specific data observation to a local macro named 'x' for posterior use in 'forvalue' loop computation, like in the following code.
    The data are structured as panel with 150 countries from 1962 to 2015.

    For each year in the panel, from 1962 to 2015, I need to get the value of variable GDPp corresponding to country "USA" (my benchmarking country)
    After that divide all GDPp of others countries by this 'x' and store the result into the NewVar.

    Note that the "replace ..." is a second loop computing NewVar for all countries each year within the first 'forvalues' loop.

    Of course, local macro works as a scalar so the line -- local x = GDPp if year==`i' & code=="USA" -- doesn't works because 'if' statement doens't apply.
    My problem is that I have stucked in visualize an alternative procedure to assign the GDPp of USA of a specific year to a local macro.

    Code:
    xtset code year     
    sort code year
    gen NewVar = .
    forvalues i=1962/2015 {
        local x = GDPp if year==`i' & code=="USA"
        replace NewVar = GDPp/`x' if year==`i'
    }
    Maybe the solution could be obvious,
    Thanks you by answer

  • #2
    You can do this

    Code:
     
    sysuse auto
    local frog = mpg[42]
    or this

    Code:
     
    su mpg if make == "Subaru"
    local toad = r(min)
    But for what you are doing http://www.stata-journal.com/sjpdf.h...iclenum=dm0055 offers a bundle of tricks and tips.

    This would be a better way:

    Code:
     
    egen GDPref = total(GDPp / (code == "USA")), by(year) 
    gen NewVar = GDPp/GDPref
    No loops, no locals.

    Comment


    • #3
      Thank you so much Nick.
      The thrid sugestion works perfectly. The command:
      Code:
      egen GDPref = total(GDPp / (code == "USA")), by(year)
      was the trick.
      And the article by Cox, Nicholas J. (2011). Speaking Stata: Compared with . . .The Stata Journal Vol 11, Number 2, pp. 305–314, was equally helpful.
      Last edited by JoaoBasilio Pereima; 09 Feb 2016, 13:52.

      Comment

      Working...
      X