Announcement

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

  • Local macro and foreach-numlist, trying to increase the value of number in the list, and making new local

    I have been working with Stata for some year, but at a very basic level I think.
    I am improving at the moment, with the use of books, Stata-documentation and reading this forum (and the archive of the maillist)
    But I have meet a problem, where I have not been able to find an answer, and hopes for a possible guidance?
    (I am using Stata/MP 14.2, on a system outside my control, so update is not possible)

    I am working on a large dataset of health information. I am trying to estimate the 5-year cumulatede incidens for a subset of disease. For the periods 1980-1984, 1985-1989, [...] , 2010-2014.
    I am trying to use a foreach-numlist-loop. Where I "inside" the loop is trying to increase the number.
    It works some places in the loop, like the:>> display "period: " `num' "-" `num'+4 <<, but the crucial part: storing informations (marked as Note1) in a new local macros, there it seems that Stata and I disagree about how to do things.

    Note1: I am trying to store the count of living persons in the local macro named Alive1980_1984 etc. The `num'+4 obviously doesn't work, and I have tried several variations - some inspired by the documentation or old posts at the statalist, other out of sheer fustration. A small selection:
    local Alive`num'_`num'+4
    local Alive`num'_{`num'+4}
    local Alive`num'_``num'+4'
    local Alive`num'_(`num'+4)
    local `"Alive`num'_`num'+4"'

    When looking them op with macro dir, it looks like they are all cutoff after Alive`num'.

    Code:
    clear
    set obs 3
    generate diagnosis_year = 1980 in 1
    replace diagnosis_year = 1975 in 2
    replace diagnosis_year = 1999 in 3
    generate death_year= 2000 in 1
    replace death_year = 1985 in 2
    replace death_year = 1999 in 3
    
    foreach num of numlist 1980 (5) 1995 {
    display "period: " `num' "-" `num'+4 _n  /*This works fine*/
    
    count if (diagnosis_year<=(`num'+4) & death_year>=`num')  /* this seems to work */
    local Alive`num'_`num'+4 = r(N)  /* Note1
    }
    
    local population1980 1 /* will normlay be drawn from at macro outside the foreach loop, called population1980 to population2015 */
    local population1981 2
    local population1982 3
    local population1983 4
    local population1984 5
    
    local populationmean = ((population`num'+(population`num'+1 ... + population`num'+4)/5) }
    if I modify it a bit, then it seems to do, what (I belive) I am asking Stata to do:

    Code:
    clear
    set obs 3
    generate diagnosis_year = 1980 in 1
    replace diagnosis_year = 1975 in 2
    replace diagnosis_year = 1999 in 3
    generate death_year= 2000 in 1
    replace death_year = 1985 in 2
    replace death_year = 1999 in 3
    
    foreach num of numlist 1980 (5) 1995 {
    local num1=`num'+1
    local num2=`num'+2
    local num3=`num'+3
    local num4=`num'+4
    
    display "period: " `num' "-" `num4' _n
    count if (diagnosis_year<=(`num4') & death_year>=`num')
    local Alive`num'_`num4' = r(N) /* Note1
    }
    
    local population1980 1 /* will normlay be drawn from at macro outside the foreach loop, called population1980 to population2015 */
    local population1981 2
    local population1982 3
    local population1983 4
    local population1984 5
    
    local populationmean = ((population`num'+population`num1'+population`num2'+population`num3'+population`num4')/5)
    }
    But the creation of new intermediate locals seems clumsy. Am I asking Stata to do the impossible or am I asking it impolitely ?
    I have a strong feeling, that I am missing a simple and obviously explanation, but I can't find it.

  • #2
    It sounds like what you want is:
    Code:
    local Alive`num'_`=`num'+4'

    Comment


    • #3
      Thank you!
      It works perfect, and the way I had hoped!

      Comment

      Working...
      X