Hi. I am trying to run the loop below over several individual datasets for the years 2007-2012. Some of the variables in each dataset have a year identifying suffix - 07X, 08X, etc. I am trying to run the loop below but get the following error: "08X invalid name". I've tried different combinations but all appear to yield the same error. I am also attaching a sample of 2007 dataset.

Code:
cd "$data"

local time "2007 2008 2009 2010 2011 2012"
local x "07X 08X 09X 10X 11X 12X"
local f "07F 08F 09F 10F 11F 12F"

foreach time in `time'{
  * import files
    use "P`time'", clear
  *keeping only the variables that matter - id, weight, payment vars & drug code and name
    keep DUPERSID RXNDC PERWT`f' RXSF`x' RXMR`x' RXMD`x' RXPV`x' RXXP`x' RXNAME
  
  *converting var names to lowercase
    rename *, lower 
  
  //Data is in long form by id and drug code - collapsing on drug code  condl on id
  //calculating totals and averages by id and drug code

    collapse (sum) rxsf`x' rxmr`x' rxmd`x' rxpv`x' rxxp`x' (mean) rxsf`x'_mean=rxsf`x' ///
    rxmr`x'_mean=rxmr`x' rxmd`x'_mean=rxmd`x' rxpv`x'_mean=rxpv`x' rxxp`x'_mean=rxxp`x' ///
   (first) perwt`f' rxname, by (dupersid rxndc)
   
  //to merge with MMS data
    gen year = `time'
  
    gen id2=_n

    drop if rxname=="-9" //drop all missing product name obs

    drop if real(substr(rxname,1,1))<. //drop all obs w product names starting with a number

    save "${output}\P`time'_dummy.dta", replace
    
    
*************Combinig with the MMS dataset***********


    matchit id2 rxname using "${drug}\MMS_WAC_data.dta", idu(id1) txtu(Product)
    //1- matchit first by the most relevant pair of columns - drug name

    // 2- bring back the other columns
    joinby id1 using "${drug}\MMS_WAC_data.dta"
    joinby id2 using "${output}\P`time'_dummy.dta"

    save "${output}\`time'+MMS.dta", replace
  }
Code:
data sample

input str8 DUPERSID str50 RXNAME str11 RXNDC double(RXSF07X RXMR07X RXMD07X RXPV07X RXXP07X PERWT07F)
"60001029" "ALLOPURINOL"                   "00378018105"  10.48      0     0 0  10.48  4693.506405
"60004013" "PREVACID"                      "00300304613"      0 175.01     0 0 175.01 14733.159185
"60004013" "PREVACID"                      "00300304613"    3.1 138.48     0 0 141.58 14733.159185
"60004013" "PREVACID"                      "00300304613"      0 169.26     0 0 169.26 14733.159185
"60004013" "PREVACID"                      "00300304613"      0 175.01     0 0 175.01 14733.159185
"60004013" "TOPROL XL"                     "00186109205"    3.1   32.6     0 0   35.7 14733.159185
"60004013" "TOPROL XL"                     "00186109205"    3.1   32.6     0 0   35.7 14733.159185
"60004013" "TOPROL XL"                     "00186109205"    3.1   32.6     0 0   35.7 14733.159185
"60004013" "IMDUR"                         "54868435301"   2.15   1.85     0 0      4 14733.159185
"60004013" "IMDUR"                         "54868435301"   2.15   1.85     0 0      4 14733.159185
"60004013" "IMDUR"                         "54868435301"   2.15   1.85     0 0      4 14733.159185
"60004013" "IMDUR"                         "54868435301"   2.15   1.85     0 0      4 14733.159185
"60004013" "INSULIN"                       "59060231404"   3.43  82.77     0 0   86.2 14733.159185
"60004013" "INSULIN"                       "59060231404"   5.24  80.96     0 0   86.2 14733.159185
"60004013" "INSULIN"                       "59060231404"   5.62  80.58     0 0   86.2 14733.159185
"60004013" "INSULIN"                       "59060231404"    1.8   84.4     0 0   86.2 14733.159185
"60004013" "INSULIN"                       "59060231404"      0   86.2     0 0   86.2 14733.159185
"60004020" "EXELON"                        "00078032444"      5 184.19     0 0 189.19 16364.385084
"60004020" "EXELON"                        "00078032444" 187.05      0     0 0 187.05 16364.385084
"60004020" "EXELON"                        "00078032444" 187.05      0     0 0 187.05 16364.385084
"60004020" "SEROQUEL (FILM-COATED)"        "00310027510"      5  122.7     0 0  127.7 16364.385084