Dear members,
I have data that i wish to reshape into the long format. This is the code i have run but does seem to give me the desired result. I would wish to have country firm year and then the variables be the columns. Looking for a panel data format.
// load data
import excel using Combined.xlsx, firstrow clear
duplicates tag country_id firm_id var_id, gen(dupl)
duplicates drop country_id firm_id var_id, force
egen sid = group(country_id firm_id var_id)
drop if sid == .
*clean variable name
replace VARIABLE = subinstr(VARIABLE," ","",.)
replace VARIABLE = subinstr(VARIABLE,"-","_",.)
replace VARIABLE = substr(VARIABLE,1,20)
rename VARIABLE variable
*convert all variables in yr2000 to yr2022 to similar formats (strings. Use destring to convert to numeric)
forvalues i = 2005/2021 {
tostring yr`i', replace force
}
reshape long yr, i(sid) j(year)
drop sid
egen xid = group(country_id firm_id year)
*duplicates drop xid, force
reshape wide variable, i(firm_id) j(variable) string
I have data that i wish to reshape into the long format. This is the code i have run but does seem to give me the desired result. I would wish to have country firm year and then the variables be the columns. Looking for a panel data format.
// load data
import excel using Combined.xlsx, firstrow clear
duplicates tag country_id firm_id var_id, gen(dupl)
duplicates drop country_id firm_id var_id, force
egen sid = group(country_id firm_id var_id)
drop if sid == .
*clean variable name
replace VARIABLE = subinstr(VARIABLE," ","",.)
replace VARIABLE = subinstr(VARIABLE,"-","_",.)
replace VARIABLE = substr(VARIABLE,1,20)
rename VARIABLE variable
*convert all variables in yr2000 to yr2022 to similar formats (strings. Use destring to convert to numeric)
forvalues i = 2005/2021 {
tostring yr`i', replace force
}
reshape long yr, i(sid) j(year)
drop sid
egen xid = group(country_id firm_id year)
*duplicates drop xid, force
reshape wide variable, i(firm_id) j(variable) string

Comment