Hi there,

I have panel data set in long format with company data containing funding data over 72 periods. So the data looks something like that:

Company, funding1, funding2, ..., funding72
1, 0, 0, ..., 2000000
2, 0, 100000, ..., 0
.
.
.

For each of the companies an event happened at a certain period [1 -72]. I'd like to calculate the total funding before that event happened. For convenience reasons I'd like to keep the long format if possible. My problem is that I can't extract the information of the period from the variable name funding*. Among others I have tested the following approach but obviously the value of the variable is taken and not the variable name.

Code:
gen priorFunding = 0
foreach x of varlist funding* {
    local xyz substr(`x',7,.)
    destring `xyz', replace force
    if `xyz' < eventgperiod {
        priorFunding == priorFunding + `x'
        }        
    }

Is there a way to extract the period of funding out of the variable name without converting to wide format? I could not find any solution in Statalist nor in other forums, but probably (or hopefully) I just did not know what to search for.

Thanks a lot in advance!

P.S. I'm using Stata/MP 13.0