Hello,
I am currently working with some UN COMTRADE export data. I obtained a zipped folder with CSV files for each country-year I am interested in. After unzipping these, I used the following adapted code to get the files in DTA format:
This seems to have worked fine. The variables I have in each of these are:
-----------------------------------------------------------------------
storage display value
variable name type format label variable label
-----------------------------------------------------------------------
reporteriso3 str3 %9s ReporterISO3
partneriso3 str3 %9s PartnerISO3
productcode str6 %9s ProductCode
year int %8.0g Year
tradeflow byte %8.0g TradeFlow
tradevalue float %9.0g TradeValue
quantity double %10.0g Quantity
qtytoken byte %8.0g QtyToken
---------------------------------------
The problem is when I try to append these country-year files. This is the code I've landed on at the moment:
The variable, 'productcode', is sometimes stored as a 'str6' type but other times stored as something else, like long. When I try to append, I get the following:
variable productcode is str6 in master but long in using data
You could specify append's force option to ignore this
string/numeric mismatch. The using variable would then be
treated as if it contained "".
r(106);
When I try the force option, Stata replaces productcode from the non-master dataset type with blanks.
I've tried looking around the Internet for solutions, like tostring, generating new variables (with and without setting the type for new variables), and merging those missing data back in (says they are not unique). Quite frustrating...
I would be extremely grateful for any solutions the Stata community has to offer. Thank you.
I am currently working with some UN COMTRADE export data. I obtained a zipped folder with CSV files for each country-year I am interested in. After unzipping these, I used the following adapted code to get the files in DTA format:
Code:
// Getting CSV Files in DTA Files: https://www.statalist.org/forums/forum/general-stata-discussion/general/74053-import-multiple-csv-files clear cd "C:\Users\natal\OneDrive\Documents\Research\3rd Year Project\UN COMTRADE Data\WITS\COMTRADE1" local myfilelist : dir . files"*.csv" foreach file of local myfilelist { drop _all insheet using "`file'" local outfile = subinstr("`file'",".csv","",.) save "`outfile'", replace }
-----------------------------------------------------------------------
storage display value
variable name type format label variable label
-----------------------------------------------------------------------
reporteriso3 str3 %9s ReporterISO3
partneriso3 str3 %9s PartnerISO3
productcode str6 %9s ProductCode
year int %8.0g Year
tradeflow byte %8.0g TradeFlow
tradevalue float %9.0g TradeValue
quantity double %10.0g Quantity
qtytoken byte %8.0g QtyToken
---------------------------------------
The problem is when I try to append these country-year files. This is the code I've landed on at the moment:
Code:
tempfile building save `building', emptyok // Appending all DTA Files: https://stats.idre.ucla.edu/stata/faq/how-can-i-combine-a-large-number-of-files/ ! dir *.dta /a-d /b > filelist.txt file open myfile using filelist.txt, read file read myfile line foreach i in `line' { while r(eof)==0 { use `line', clear append using `building' save `"`building'"', replace file read myfile line } } use `building', clear
The variable, 'productcode', is sometimes stored as a 'str6' type but other times stored as something else, like long. When I try to append, I get the following:
variable productcode is str6 in master but long in using data
You could specify append's force option to ignore this
string/numeric mismatch. The using variable would then be
treated as if it contained "".
r(106);
When I try the force option, Stata replaces productcode from the non-master dataset type with blanks.
I've tried looking around the Internet for solutions, like tostring, generating new variables (with and without setting the type for new variables), and merging those missing data back in (says they are not unique). Quite frustrating...
I would be extremely grateful for any solutions the Stata community has to offer. Thank you.
Comment