Since the state ID becomes the hundreds place of the new ID, just multiply it by 100 and add the county ID:
gen id1=state*100+county
Making string versions of the variables is easy:
gen stateString=string(state)
gen countyString=string(county)
Combining them is also easy. With strings, tacking one string to the end of the other is just addition:
gen id2=stateString+countyString
Unfortunately it's also wrong. Do a list and you'll note that the leading zero you were asked to create is not there. Even worse, the first ten counties don't have a zero after the state, so you can't tell state 1, county 11 from state 11, county 1 (both have an id2 of 111).
One way to fix this problem is to detect cases that need leading zeros and add them:
replace stateString="0"+stateString if state<10
replace countyString="0"+countyString if county<10
Then you can combine the modified strings and get the proper result:
gen id3=stateString+countyString
A more elegant method gives the string() function a second argument, the format the number should be put in when converting it to a string. We won't discuss formats, but since it is a better solution here are the commands:
gen goodStateString=string(state,"%02.0f")
gen goodCountyString=string(county,"%02.0f")
Type help format if you'd like to learn more about them.
Complete do file:
clear all
set more off
capture log close
log using data_ex4.log, replace
use statecounty
gen id1=state*100+county
gen stateString=string(state)
gen countyString=string(county)
gen id2=stateString+countyString
list if state==1
replace stateString="0"+stateString if state<10
replace countyString="0"+countyString if county<10
gen id3=stateString+countyString
list
if state==1
log close
Last Revised: 12/17/2015