Announcement

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

  • Forvalues with an arbitrary list of numbers

    Hello I am trying to append some files using the command below


    forvalues year =96/05 {
    use file_year_`x'.dta
    if `x' !=96 append using file_year_`x'.dta
    ​save file_all_years.dta, replace
    }

    The trouble here is that the files beginning from the the year 2000 to 2005 are labelled as "00" to "05". I want to assume that logically stata will assume that from 00 to 05 the values are not consecutive as it should be when using the command “forvalues” so files are only appended up to the year 1999. How can I fix this problem?

  • #2
    You can append several files at once. Odds are that they fit a pattern ending ??.dta

    Otherwise loop over 96/105 extracting a remainder:

    Code:
    local suffix : di %02.0f mod(‘x’, 100)
    where you need to fix the opening left quotation mark.

    Comment


    • #3
      More at

      SJ-10-1 pr0051 . . . . . . . . . . . . Stata tip 85: Looping over nonintegers
      . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox
      Q1/10 SJ 10(1):160--163 (no commands)
      tip on using forvalues to loop over numbers

      Comment

      Working...
      X