as you can see, below are four parts of repetitive codes, and the only difference is ccode2 (31, 40, 41, 42). so i am wondering if there is anyway to write a loop to simplify these repetitive codes? Thanks in advance and happy holidays!
Code:
use "/Users/jack/Library/Mobile Documents/com~apple~CloudDocs/data/COW_Trade_4.0/cow_trade_4.0_rr.dta", clear drop edtc sort ccode1 year by ccode1 year: egen edtc= sum(trade_sha1) if ccode1==2 & ccode2 != 31 sort ccode1 ccode2 year save "/Users/jack/Library/Mobile Documents/com~apple~CloudDocs/data/COW_Trade_4.0/cow_trade_4.0_rr.dta", replace drop if edtc == . sort ccode1 year quietly by ccode1 year: gen dup = cond(_N==1,0,_n) drop if dup>1 drop dup keep ccode1 ccode2 year edtc rename edtc edtc1 replace ccode2 = 31 sort ccode1 ccode2 year save "/Users/jack/Library/Mobile Documents/com~apple~CloudDocs/data/COW_Trade_4.0/cow_edtc.dta", replace merge ccode1 ccode2 year using "/Users/jack/Library/Mobile Documents/com~apple~CloudDocs/data/COW_Trade_4.0/cow_trade_4.0_rr.dta" drop _merge drop if ccode1_1 ==. save "/Users/jack/Library/Mobile Documents/com~apple~CloudDocs/data/COW_Trade_4.0/cow_trade_4.0_rr.dta", replace drop edtc sort ccode1 year by ccode1 year: egen edtc= sum(trade_sha1) if ccode1==2 & ccode2 != 40 sort ccode1 ccode2 year save "/Users/jack/Library/Mobile Documents/com~apple~CloudDocs/data/COW_Trade_4.0/cow_trade_4.0_rr.dta", replace drop if edtc == . sort ccode1 year quietly by ccode1 year: gen dup = cond(_N==1,0,_n) drop if dup>1 drop dup keep ccode1 ccode2 year edtc rename edtc edtc1 replace ccode2 = 40 sort ccode1 ccode2 year save "/Users/jack/Library/Mobile Documents/com~apple~CloudDocs/data/COW_Trade_4.0/cow_edtc.dta", replace merge ccode1 ccode2 year using "/Users/jack/Library/Mobile Documents/com~apple~CloudDocs/data/COW_Trade_4.0/cow_trade_4.0_rr.dta" drop _merge drop if ccode1_1 ==. save "/Users/jack/Library/Mobile Documents/com~apple~CloudDocs/data/COW_Trade_4.0/cow_trade_4.0_rr.dta", replace drop edtc sort ccode1 year by ccode1 year: egen edtc= sum(trade_sha1) if ccode1==2 & ccode2 != 41 sort ccode1 ccode2 year save "/Users/jack/Library/Mobile Documents/com~apple~CloudDocs/data/COW_Trade_4.0/cow_trade_4.0_rr.dta", replace drop if edtc == . sort ccode1 year quietly by ccode1 year: gen dup = cond(_N==1,0,_n) drop if dup>1 drop dup keep ccode1 ccode2 year edtc rename edtc edtc1 replace ccode2 = 41 sort ccode1 ccode2 year save "/Users/jack/Library/Mobile Documents/com~apple~CloudDocs/data/COW_Trade_4.0/cow_edtc.dta", replace merge ccode1 ccode2 year using "/Users/jack/Library/Mobile Documents/com~apple~CloudDocs/data/COW_Trade_4.0/cow_trade_4.0_rr.dta" drop _merge drop if ccode1_1 ==. save "/Users/jack/Library/Mobile Documents/com~apple~CloudDocs/data/COW_Trade_4.0/cow_trade_4.0_rr.dta", replace drop edtc sort ccode1 year by ccode1 year: egen edtc= sum(trade_sha1) if ccode1==2 & ccode2 != 42 sort ccode1 ccode2 year save "/Users/jack/Library/Mobile Documents/com~apple~CloudDocs/data/COW_Trade_4.0/cow_trade_4.0_rr.dta", replace drop if edtc == . sort ccode1 year quietly by ccode1 year: gen dup = cond(_N==1,0,_n) drop if dup>1 drop dup keep ccode1 ccode2 year edtc rename edtc edtc1 replace ccode2 = 42 sort ccode1 ccode2 year save "/Users/jack/Library/Mobile Documents/com~apple~CloudDocs/data/COW_Trade_4.0/cow_edtc.dta", replace merge ccode1 ccode2 year using "/Users/jack/Library/Mobile Documents/com~apple~CloudDocs/data/COW_Trade_4.0/cow_trade_4.0_rr.dta" drop _merge drop if ccode1_1 ==. save "/Users/jack/Library/Mobile Documents/com~apple~CloudDocs/data/COW_Trade_4.0/cow_trade_4.0_rr.dta", replace
0 Response to creating a loop
Post a Comment