types - How can I change the value stored in local macro from string to numeric in Stata? -


i want generate variable lagged year depending on year stored in "$s_date" macro

i have stored year in date macro:

. local date substr("$s_date",8,.) . display `date'   2015 

and want generate new variable with

gen start_year =`date'- y_passed 

where y_passed variable containing integers 1 10, , get:

. gen start_year = `date' - y_passed type mismatch r(109); 

i know happen because macro stored string.

how can change value stored in local macro string numeric?

if add =, stata evaluate expression defines local date:

clear set more off  set obs 10 gen y_passed = _n  local date = substr("$s_date",8,.) display `date'  gen start_year = `date' - y_passed  list 

otherwise, local holds string, not number in string type! see

. local date substr("$s_date",8,.)  . display `"`date'"' substr("14 may 2015",8,.) 

the confusion because display evaluate you. in reality,

display `date' 

expands to

display substr("14 may 2015",8,.) 

and latter results in

2015 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -