I'm working with Johns' Hopkins COVID-19 data. It comes in wide format and I must reshape it. The issue however is that the dataset is updated (daily?) and the name of the last variable changes every day. So, for the outcome data of interest, the data are v12, v13, v14... until today which is v645.

The approach I've tried so far is simply [Note, I use the user contributed greshape by Mauricio Caceres Bravo]

Code:
import delim "https://tinyurl.com/uynhaxd", clear // Imports cases data

cap which greshape

if _rc {
    
    ssc install gtools
    gtools, upgrade
}

qui: ds v12-v645

loc vars `r(varlist)'

local nwords :  word count `r(varlist)'

disp `nwords'

cls

forv i = 1/`nwords' {
       
    loc a: word `i' of `nwords'
    loc b: word `i' of `vars'
    
    qui: rename `b' day_`i'
}

drop if fips ==.

qui: greshape long day_, i(fips) j(num) string

rename (num day_) (date cases)

destring date, replace

sort fips date

replace date = date+21935
format date %td
Well...... what if I don't want to have to continuously check whatever the name of the last variable is? What if I want Stata to check? So I looked it up.


And as a result, I tried
Code:
import delim "https://tinyurl.com/uynhaxd", clear

cap which greshape

if _rc {
    
    ssc install gtools
    gtools, upgrade
}

** The code recommended in the link

    des

    loc lastvar: word `c(k)' of `r(varlist)'

**
qui: ds v12-`lastvar'

loc vars `r(varlist)'

local nwords :  word count `r(varlist)'

disp `nwords'

cls

forv i = 1/`nwords' {
       
    loc a: word `i' of `nwords'
    loc b: word `i' of `vars'
    
    qui: rename `b' day_`i'
}

drop if fips ==.

qui: greshape long day_, i(fips) j(num) string

rename (num day_) (date cases)

destring date, replace

sort fips date

replace date = date+21935
format date %td
This returns an error of
Code:
nothing found where name expected
r(198);
Well, I traced it, and essentially the issue is that
Code:
    des
    loc lastvar: word `c(k)' of `r(varlist)'
Isn't capturing the last variable, v645, as it's supposed to. Is there a different or better way to do this? A bug in my code I'm not seeing? I guess I could keep looking at the last variable, but I'd really appreciate knowing how to have Stata do this in context.