Announcement

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

  • Generate age variable in a loop

    Hi!

    I'm working building an anual dataframe, as the result of appending the respective monthly dataframe. At first, I build the first year with its respectiva variables and then I generate a loop for the next ones.
    When I am generating the age variable it returns me an error because the td() command doesn't recognize the variable `anio' in that loop.

    Here is an example of how I build it up without the loop:
    g edad_012021=int((td(01jan2021)-fecha_nac)/365)

    And there is the way I am coding into the loop (without other steps to prepare the dataframe):

    forvalues anio=2012(1)2020 {
    use "$datoscotiz\\`anio'_1_universo.dta", clear
    g anio=`anio'
    g mescargo=1
    forvalues mescargo=2(1)12 {
    append using "$datoscotiz\\`anio'_`mescargo'_universo.dta"
    replace anio=`anio' if anio==.
    replace mescargo=`mescargo' if mes==.
    }
    g edad_01`anio'=int((td(01jan`anio')-fecha_nac)/365)
    }


    I don't know if there is a problem with my code or maybe I should use another command.

    Wish u can help me!

    Thank's a lot for u all!

  • #2
    Please, let me know if you need a bit more information to understand the matter.

    Thanks again!!

    Comment


    • #3
      I don't think there should be a problem with the command recognising the macro anio at that point. Perhaps there is some other problem? Could you copy the contents of the Results window / log file so we can see the exact error?

      Comment

      Working...
      X