Announcement

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

  • local macro in foreach - under construction

    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 1981 (5) 1995 {
    display " ******* " _n "period: " `num' "-" `num'+4 _n 
    
    count if (diagnosis_year<=(`num'+4) & death_year>=`num')
    local Alive`num'_``num'+4'  = r(N)
    macro dir 
    }
    
    local population1981 55 /* will normlay be drawn from at macro outside the foreach loop*/
    local population1982 66
    local population1983 77
    
    display `population`num''
    display `population `num'+1'
    display `population =(`num'+4)'
    
    display "population" `num'+10 _n _n
    
    }

  • #2

    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 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-2015. I am trying to use a foreach-numlist-loop. Where I "inside" the loop is trying to increase the number. I works some places in the loop, like the:>> display "period: " `num' "-" `num'+4 <<, but the crucial parts: storing informations (marked as Note1) in new local macros is seems as 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: 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 thecreation of new intermediate locals seems clumsy. Am I asking Stata to do the impossible or am I asking it impolitely ?

    Comment


    • #3
      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-2015.
      I am trying to use a foreach-numlist-loop. Where I "inside" the loop is trying to increase the number.
      I works some places in the loop, like the:>> display "period: " `num' "-" `num'+4 <<, but the crucial parts: storing informations (marked as Note1) in new local macros is seems as 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:
      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 ?




















      Comment

      Working...
      X