Announcement

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

  • Update Stata code for retrieving COVID data?

    I've greatly enjoyed running Chuck Huber's Stata code that automated the download of COVID data from the Johns Hopkins data depository (see https://blog.stata.com/2020/03/24/up...covid-19-post/). Yet this code no longer updates as it is limited to 2020 data. Has anyone updated the code to retrieve 2020 and 2021 data? I'm guessing it is an easy fix, but my macro and looping skills within Stata are still underdeveloped.

    Has anyone updated this Stata code? If so, will you share?

  • #2
    This seems to work:

    Code:
    local URL = "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_daily_reports/"
    forvalues year = 2020/2021{
        forvalues month = 1/12 {
            forvalues day = 1/31 {
                local month = string(`month', "%02.0f")
                local day = string(`day', "%02.0f")
                local year = string(`year', "%02.0f")
                local today = "`month'-`day'-`year'"
                local FileName = "`URL'`today'.csv"
                clear
                capture import delimited "`FileName'"
                capture confirm variable ïprovincestate
                if _rc == 0 {
                    rename ïprovincestate provincestate
                    label variable provincestate "Province/State"
                }
                capture rename province_state provincestate
                capture rename country_region countryregion
                capture rename last_update lastupdate
                capture rename lat latitude
                capture rename long longitude
                capture save "`today'", replace
            }
        }
    }
    clear
    forvalues year = 2020/2021{
        forvalues month = 1/12 {
            forvalues day = 1/31 {
                local month = string(`month', "%02.0f")
                local day = string(`day', "%02.0f")
                local year = string(`year', "%02.0f")
                local today = "`month'-`day'-`year'"
                capture append using "`today'"
            }
        }
    }
    Same disclaimer applies as the blog post - I haven't checked or cleaned the data.
    Last edited by Ali Atia; 10 Jan 2021, 12:55.

    Comment


    • #3
      Thank you so much, Ali! Here's the slightly altered code that I used:

      local URL = "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_daily_reports/"
      forvalues year=2020/2021{
      forvalues month = 1/12 {
      forvalues day = 1/31 {
      local month = string(`month', "%02.0f")
      local day = string(`day', "%02.0f")
      local year = string(`year', "%02.0f")
      local today = "`month'-`day'-`year'"
      local FileName = "`URL'`today'.csv"
      clear
      capture import delimited "`FileName'"
      capture confirm variable ïprovincestate
      if _rc == 0 {
      rename ïprovincestate provincestate
      label variable provincestate "Province/State"
      }
      capture rename province_state provincestate
      capture rename country_region countryregion
      capture rename last_update lastupdate
      capture rename lat latitude
      capture rename long longitude
      generate tempdate = "`today'"
      capture save "`today'", replace
      }
      }
      }
      clear
      forvalues year=2020/2021{
      forvalues month = 1/12 {
      forvalues day = 1/31 {
      local month = string(`month', "%02.0f")
      local day = string(`day', "%02.0f")
      local year = string(`year', "%02.0f")
      local today = "`month'-`day'-`year'"
      capture append using "`today'"
      }
      }
      }
      generate date = date(tempdate, "MDY")
      format date %tdNN/DD/CCYY

      Comment

      Working...
      X