Hi all,

I have a dataset with variable names from taxdebt_20120101 to taxdebt_20131231 with the last digits being a date format. Those variables display values from 0 - 100,000
I am trying to create a new variable that equals the date of the variable name if the value of that variable is > 0.

My code so far:

gen date = .

forval j = 20120101/20131231 {
replace date = `j' if taxdebt_`j' > 0
}

tostring date, replace format(%20.0f)
gen date2 = date(date,"YMD")
format date2 %td


Unfortunately the code doesn't quite work. I keep getting the date 20120132. A date is also displayed for values = 0.
Hope you can help.