Hi friends,

I now face the following problem:

Code:
webuse auto,clear
lab var make "STATA#1, 90"
lab var price "STATA#11, 90"
lab var mpg "STATA#22, 91"
lab var rep78 "STATA#2, 91"

lookfor make price mpg rep78
local varlist= r(varlist)
local counter=0
foreach var in `varlist' {
    local varlab: var lab `var'  //extract the whole variable label string
    //di "`varlab'"
    local varlaby = real(reverse(substr(reverse("`varlab'"),1,2))) + 1900  //extract the last two digits in the label and add it by 1900
    di "`varlaby'"

    local varlabm = real(substr("`varlab'"),7,2)   //extract the number after #.
    //This command fails in the cases of make and rep78 and succeeds for price and mpg.
    //I am looking for a general solution to this problem
    di "`varlabm'"
    }
So my job is to extract the numbers in each variable's label, including both the last two digits (90 and 91 in the example) and the first number after # (1, 11, 22, 2 in the example). I know how to do the former, but can't figure out how to extract the number after #, because they vary in length and are always surrounded by nonnumeric symbols.

Thank you!