OK. The variable you show is a numeric variable with a value label attached. So what you want to split is the value labels.
It is important to remember that in Stata, what you see, is not always what you get. Just because something looks like a string in the browser, or in Stata output, doesn't necessarily mean it's a string variable. It can, instead, be a value-labeled numeric variable, or, sometimes a date or clock variable with an alphanumeric display format attached. It is important to distinguish between these. This is perhaps simplest to do in the browser. True string variables in the browser display in a reddish brown color. Value labeled numeric variables show in blue. And display formatted date variables display in black. (These are the default color assignments: if you have changed those assignments when setting your Stata preferences, then they may differ from what I have said.) String functions only apply to string variables. To apply string values to things that look like strings but are really value-labeled numeric variables, you have to first -decode- the variable to create a true string, and then apply the string function to the newly created string variable.
Code:
decode congdist, gen(str_congdist) gen state_code = substr(str_congdist, 1, 2) gen district_code = substr(str_congdist, 3, .) // AND, OPTIONALLY, IF YOU PREFER DISTRICT_CODE TO BE NUMERIC: destring district_code, replace

Comment