I'd like to simplify the following code:
gen prefid=00 if prefecture=="all"
gen countyid=00 if county=="all"
replace prefid=99 if prefecture=="high"
replace countyid=99 if county=="high"
replace prefid=01 if provid==11 & prefecture=="firstintcourt" & county=="firstintcourt"
replace countyid=99 if provid==11 & prefecture=="firstintcourt" & county=="firstintcourt"
replace prefid=01 if provid==11 & prefecture=="firstintcourt" & county=="changpingqu"
replace countyid=01 if provid==11 & prefecture=="firstintcourt" & county=="changpingqu"
replace prefid=01 if provid==11 & prefecture=="firstintcourt" & county=="haidianqu"
replace countyid=08 if provid==11 & prefecture=="firstintcourt" & county=="haidianqu"
replace prefid=01 if provid==11 & prefecture=="firstintcourt" & county=="mentougouqu"
replace countyid=09 if provid==11 & prefecture=="firstintcourt" & county=="mentougouqu"
replace prefid=01 if provid==11 & prefecture=="firstintcourt" & county=="shijingshanqu"
replace countyid=07 if provid==11 & prefecture=="firstintcourt" & county=="shijingshanqu"
replace prefid=02 if provid==11 & prefecture=="firstintcourt" & county=="yanqingqu" & year<=2015
replace countyid=29 if provid==11 & prefecture=="firstintcourt" & county=="yanqingqu" & year<=2015
replace prefid=01 if provid==11 & prefecture=="firstintcourt" & county=="yanqingqu" & year>=2016
replace countyid=19 if provid==11 & prefecture=="firstintcourt" & county=="yanqingqu" & year>=2016
replace prefid=01 if provid==11 & prefecture=="secondintcourt" & county=="daxingqu"
replace countyid=15 if provid==11 & prefecture=="secondintcourt" & county=="daxingqu"
replace prefid=01 if provid==11 & prefecture=="secondintcourt" & county=="fangshanqu"
replace countyid=11 if provid==11 & prefecture=="secondintcourt" & county=="fangshanqu"
replace prefid=01 if provid==11 & prefecture=="secondintcourt" & county=="fengtaiqu"
replace countyid=06 if provid==11 & prefecture=="secondintcourt" & county=="fengtaiqu"
replace prefid=01 if provid==11 & prefecture=="secondintcourt" & county=="secondintcourt"
replace countyid=98 if provid==11 & prefecture=="secondintcourt" & county=="secondintcourt"
The ChatGPT first advised the following:
local lookup "11 firstintcourt firstintcourt 01 99" ///
"11 firstintcourt changpingqu 01 01" ///
"11 firstintcourt haidianqu 01 08" ///
"11 firstintcourt mentougouqu 01 09" ///
"11 firstintcourt shijingshanqu 01 07" ///
"11 firstintcourt yanqingqu 02 29 if year<=2015" ///
"11 firstintcourt yanqingqu 01 19 if year>=2016" ///
"11 secondintcourt daxingqu 01 15" ///
"11 secondintcourt fangshanqu 01 11" ///
"11 secondintcourt fengtaiqu 01 06" ///
"11 secondintcourt secondintcourt 01 98"
// generate prefid and countyid using lookup table
gen prefid = ""
gen countyid = ""
foreach row of local lookup {
tokenize `row'
if provid == `1' & prefecture == "`2'" & county == "`3'" {
replace prefid = `4' if inlist(prefecture, "all", "high", "`2'")
replace countyid = `5' if inlist(county, "all", "high", "`3'")
}
}
I got the following error:
. gen prefid = ""
(5,049 missing values generated)
. gen countyid = ""
(5,049 missing values generated)
. foreach row of local lookup {
2. tokenize `row'
3. if provid == `1' & prefecture == "`2'" & county == "`3'" {
4. replace prefid = `4' if inlist(prefecture, "all", "high", "`2'")
5. replace countyid = `5' if inlist(county, "all", "high", "`3'")
6. }
7. }
firstintcourt not found
r(111);
end of do-file
r(111);
Then, the ChatGPT advised a second revised code:
// define lookup table as a matrix
matrix lookup = (11 "firstintcourt" "firstintcourt" 01 99 \
11 "firstintcourt" "changpingqu" 01 01 \
11 "firstintcourt" "haidianqu" 01 08 \
11 "firstintcourt" "mentougouqu" 01 09 \
11 "firstintcourt" "shijingshanqu" 01 07 \
11 "firstintcourt" "yanqingqu" 02 29 if year<=2015 \
11 "firstintcourt" "yanqingqu" 01 19 if year>=2016 \
11 "secondintcourt" "daxingqu" 01 15 \
11 "secondintcourt" "fangshanqu" 01 11 \
11 "secondintcourt" "fengtaiqu" 01 06 \
11 "secondintcourt" "secondintcourt" 01 98)
// generate prefid and countyid using lookup table
gen prefid = ""
gen countyid = ""
forvalues i = 1/rows(lookup) {
if provid == lookup[`i',1] & prefecture == lookup[`i',2] & county == lookup[`i',3] {
replace prefid = lookup[`i',4] if inlist(prefecture, "all", "high", lookup[`i',2])
replace countyid = lookup[`i',5] if inlist(county, "all", "high", lookup[`i',3])
}
}
However, running this code generates the following error:
. matrix lookup = (11 "firstintcourt" "firstintcourt" 01 99 \
too few ')' or ']'
r(132);
end of do-file
r(132);
Thank you very much!
Linghui
gen prefid=00 if prefecture=="all"
gen countyid=00 if county=="all"
replace prefid=99 if prefecture=="high"
replace countyid=99 if county=="high"
replace prefid=01 if provid==11 & prefecture=="firstintcourt" & county=="firstintcourt"
replace countyid=99 if provid==11 & prefecture=="firstintcourt" & county=="firstintcourt"
replace prefid=01 if provid==11 & prefecture=="firstintcourt" & county=="changpingqu"
replace countyid=01 if provid==11 & prefecture=="firstintcourt" & county=="changpingqu"
replace prefid=01 if provid==11 & prefecture=="firstintcourt" & county=="haidianqu"
replace countyid=08 if provid==11 & prefecture=="firstintcourt" & county=="haidianqu"
replace prefid=01 if provid==11 & prefecture=="firstintcourt" & county=="mentougouqu"
replace countyid=09 if provid==11 & prefecture=="firstintcourt" & county=="mentougouqu"
replace prefid=01 if provid==11 & prefecture=="firstintcourt" & county=="shijingshanqu"
replace countyid=07 if provid==11 & prefecture=="firstintcourt" & county=="shijingshanqu"
replace prefid=02 if provid==11 & prefecture=="firstintcourt" & county=="yanqingqu" & year<=2015
replace countyid=29 if provid==11 & prefecture=="firstintcourt" & county=="yanqingqu" & year<=2015
replace prefid=01 if provid==11 & prefecture=="firstintcourt" & county=="yanqingqu" & year>=2016
replace countyid=19 if provid==11 & prefecture=="firstintcourt" & county=="yanqingqu" & year>=2016
replace prefid=01 if provid==11 & prefecture=="secondintcourt" & county=="daxingqu"
replace countyid=15 if provid==11 & prefecture=="secondintcourt" & county=="daxingqu"
replace prefid=01 if provid==11 & prefecture=="secondintcourt" & county=="fangshanqu"
replace countyid=11 if provid==11 & prefecture=="secondintcourt" & county=="fangshanqu"
replace prefid=01 if provid==11 & prefecture=="secondintcourt" & county=="fengtaiqu"
replace countyid=06 if provid==11 & prefecture=="secondintcourt" & county=="fengtaiqu"
replace prefid=01 if provid==11 & prefecture=="secondintcourt" & county=="secondintcourt"
replace countyid=98 if provid==11 & prefecture=="secondintcourt" & county=="secondintcourt"
The ChatGPT first advised the following:
local lookup "11 firstintcourt firstintcourt 01 99" ///
"11 firstintcourt changpingqu 01 01" ///
"11 firstintcourt haidianqu 01 08" ///
"11 firstintcourt mentougouqu 01 09" ///
"11 firstintcourt shijingshanqu 01 07" ///
"11 firstintcourt yanqingqu 02 29 if year<=2015" ///
"11 firstintcourt yanqingqu 01 19 if year>=2016" ///
"11 secondintcourt daxingqu 01 15" ///
"11 secondintcourt fangshanqu 01 11" ///
"11 secondintcourt fengtaiqu 01 06" ///
"11 secondintcourt secondintcourt 01 98"
// generate prefid and countyid using lookup table
gen prefid = ""
gen countyid = ""
foreach row of local lookup {
tokenize `row'
if provid == `1' & prefecture == "`2'" & county == "`3'" {
replace prefid = `4' if inlist(prefecture, "all", "high", "`2'")
replace countyid = `5' if inlist(county, "all", "high", "`3'")
}
}
I got the following error:
. gen prefid = ""
(5,049 missing values generated)
. gen countyid = ""
(5,049 missing values generated)
. foreach row of local lookup {
2. tokenize `row'
3. if provid == `1' & prefecture == "`2'" & county == "`3'" {
4. replace prefid = `4' if inlist(prefecture, "all", "high", "`2'")
5. replace countyid = `5' if inlist(county, "all", "high", "`3'")
6. }
7. }
firstintcourt not found
r(111);
end of do-file
r(111);
Then, the ChatGPT advised a second revised code:
// define lookup table as a matrix
matrix lookup = (11 "firstintcourt" "firstintcourt" 01 99 \
11 "firstintcourt" "changpingqu" 01 01 \
11 "firstintcourt" "haidianqu" 01 08 \
11 "firstintcourt" "mentougouqu" 01 09 \
11 "firstintcourt" "shijingshanqu" 01 07 \
11 "firstintcourt" "yanqingqu" 02 29 if year<=2015 \
11 "firstintcourt" "yanqingqu" 01 19 if year>=2016 \
11 "secondintcourt" "daxingqu" 01 15 \
11 "secondintcourt" "fangshanqu" 01 11 \
11 "secondintcourt" "fengtaiqu" 01 06 \
11 "secondintcourt" "secondintcourt" 01 98)
// generate prefid and countyid using lookup table
gen prefid = ""
gen countyid = ""
forvalues i = 1/rows(lookup) {
if provid == lookup[`i',1] & prefecture == lookup[`i',2] & county == lookup[`i',3] {
replace prefid = lookup[`i',4] if inlist(prefecture, "all", "high", lookup[`i',2])
replace countyid = lookup[`i',5] if inlist(county, "all", "high", lookup[`i',3])
}
}
However, running this code generates the following error:
. matrix lookup = (11 "firstintcourt" "firstintcourt" 01 99 \
too few ')' or ']'
r(132);
end of do-file
r(132);
Thank you very much!
Linghui
Comment