Announcement

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

  • For each loop generates empty values after first loop, but runs without error

    Dear all,

    I am trying to combine datasets which are saved in files per country per year.
    I start with a database of hourly electricity prices for a number of countries and now want to add load data by running a loop using joinby for the countries and years.
    As stated in the title, the code runs smoothly and does not generate any error terms, but generates empty values "." for all hourly slots after Austria, 2015 (which should be the first pair on the loops).

    This is my code: (the country list is actually significantly longer, but I am running it like this to work the kinks out of the code)

    local countrylist "AT BE"
    local years "2015 2016 2017 2018 2019 2020 2021 2022 2023"

    foreach year of local years {
    foreach country of local countrylist {
    clear
    use "${pathBASE}/2. working/Database merged.dta"
    joinby iso2code datetimelocal using "${pathBASE}/1. raw data/load data/`country'/load_`country'_`year'.dta", unmatched(both)
    drop _merge
    save "${pathBASE}/2. working/Database merged.dta", replace

    }
    }

    iso2code is a string variable, datetimelocal is a double.

  • #2
    For these kinds of questions, it is difficult to help you without a data example. Once you merge the datasets the first time around, the load variable exists in both the master and using datasets. For subsequent joinbys, it would appear that you need to include the option -update- so that missing values in the master file are updated with values from the using file. From

    Code:
    help joinby
    update varies the action that joinby takes when an observation is matched. By default, values from the master data are retained when the same variables are found in both datasets. If update is specified, however, the values from the using dataset are retained where the master dataset contains missing.
    In any case, before writing a loop, you will want to try out a few instances to see whether it does what you want.

    Comment


    • #3
      Thank you so much, update is exactly what was missing!
      And yes, I completely agree: this is my testing of the loop.

      Comment

      Working...
      X