Saving with filename defined by value in a cell
Hi,
I have a dataset
I'm using a loop to split it into country sets (master dataset is approx 400gb .csv).
the stata code I'm using is
This works fine to split the file into individual countries, but it saves the file as file1.dta, file2.dta, etc. This wouuld be ok, except I will need to append multiple files together to make a whole country dataset, and file1.dta is not always consistently AD (Andorra), it might be AE(United Arab Emirates), for example.
How can I tell Stata to use any value from the countrycode variable that it has in memory to use as the filename?
Thanks
Hi,
I have a dataset
Code:
* Example generated by -dataex-. For more info, type help dataex clear input str18 bvd_id_number str2 countrycode "PT503338435" "PT" "MA37163-81" "MA" "JP1011001052846" "JP" "FI10958757" "FI" "US115044924GN" "US" "US112599986GN" "US" "IT03704400237" "IT" "IT01868030543" "IT" "HU14010040" "HU" "PT501850066" "PT" end
I'm using a loop to split it into country sets (master dataset is approx 400gb .csv).
the stata code I'm using is
Code:
egen group = group(countrycode)
su group, meanonly
foreach i of num 1/`r(max)' {
preserve keep if group == \`i' save \`i'.dta
restore
}
This works fine to split the file into individual countries, but it saves the file as file1.dta, file2.dta, etc. This wouuld be ok, except I will need to append multiple files together to make a whole country dataset, and file1.dta is not always consistently AD (Andorra), it might be AE(United Arab Emirates), for example.
How can I tell Stata to use any value from the countrycode variable that it has in memory to use as the filename?
Thanks

Comment