I have a large dataset of 20 cities and I'd like to split it into smaller ones for each city. Each variable in the dataset will be exported into a text file.
foreach i in Denver Blacksburg {
use "D:\Data\All\AggregatedCount.dta", clear
drop if MetroArea != `i'
export delimited lnbike using "D:\Data/`"`i'"'/DV/lnbike.txt", delimiter(tab) replace
export delimited lnped using "D:\Data/`"`i'"'/DV/lnped.txt", delimiter(tab) replace
}
I tried i' and"`i'"' in the export commands but none of them worked. The error is
"Denver not found."
I also have cities that have space in between, such as Los Angeles. I tried
local city `" "Blacksburg" "Los Angeles" "Denver" "'
foreach i of city {
use "D:\Data\All\AggregatedCount.dta", clear
drop if MetroArea != `i'
export delimited lnbike using "D:/Data/`"`i'"'/DV/lnbike.txt", delimiter(tab) replace
export delimited lnped using "D:/Data/`"`i'"'/DV/lnped.txt", delimiter(tab) replace
}
This didn't work either. Do you have any suggestion?
for(l in levels(iris$Species){data.table::fwrite(iris[iris$Species==l,],paste0(l,".csv"))}You could also usewrite.csv()instead offwriteso that you don't need a library, butfwriteis faster.