Announcement

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

  • Date

    Dear Stata community,

    I am using data from multiple countries and from varying time periods. These data sets follow the general format "Data\household\country\year\purchases" for the file names. While the list of countries is fixed so I can create a local or global country-list, the beginnings and endings of the time periods (in years) vary from country to country: Example:
    "Data\household\Mexico\2017\purchases"
    "Data\household\Mexico\2018\purchases"
    "Data\household\Brazil\2015\purchases"
    "Data\household\Brazil\2016\purchases"

    I would prefer not to resort to typing the beginning and ending years in each period by hand. So I am trying to code this process in a loop, but can’t seem to manage the time loop. Is it possible to code so that the beginning/ending of each period is identified from the file name?

    Many thanks in advance.

  • #2
    On this evidence: suppose you're in a loop over countries and then you wish to select years. Loop over the maximum possible range and use capture so that a file not existing doesn't stop the code.

    Code:
    foreach c of local countrylist { 
         forval y = 2015/2018 { 
               capture <whatever> using Data/household/`c'/`y'/purchases
               if rc == 0 { 
                       /// file exists 
                       ...
              }
    }
    where / not \ is strongly advisable for reasons for you can search for using an expression such as "backstabbing backslash".

    Very likely your years will span a wider interval than 2015/2018.

    Comment


    • #3
      rc should be _rc

      Comment


      • #4
        It worked! Many thanks for your help. And the tips.

        Comment

        Working...
        X