Hi!
I have 10 separate csv datasets with transaction level data on all exports recorded in South Africa from 2020 - 2021, with each separate file containing the exports to a different country. Hence, an individual observation/row of data in this file is a single recorded export that looks like this:
I am trying to write a loop which essentially loads each csv file one at a time, transforms it (collapses the data to the port/district office it originates from), and then saves the transformed data into a file on disk. However, I do not know how to code the loop so that it "stacks" each iteration of the transformed data into the same file. What I have so far is:
forval i= 1/10 {
clear
capture import delimited using "C:\Users\Christopher Ujma\Documents\\Report (`i').csv", case(preserve)
//generate variable to count number of shipments
gen NumberOfShipments = 1
//Collapse data to Origin, Destination and Time
collapse (sum) NumberOfShipments CustomsValue, by(DistrictOfficeCode CountryOfDestinationName YearMonth)
//command to save to file on disk
}
Essentially I am looking to use something similar to the append command, but instead of appending data from a file on disk to data in memory I am looking to do it the other way around. Also, just a note that I am using Stata 15.
Thank you!!
I have 10 separate csv datasets with transaction level data on all exports recorded in South Africa from 2020 - 2021, with each separate file containing the exports to a different country. Hence, an individual observation/row of data in this file is a single recorded export that looks like this:
| TradeType | DistrictOfficeCode | DistrictOfficeName | CountryOfOrigin | CountryOfOriginName | CountryOfDestination | CountryOfDestinationName | Tariff | StatisticalUnit | TransportCode | TransportCodeDescription | YearMonth | CalendarYear | Section | SectionAndDescription | Chapter | ChapterAndDescription | TariffAndDescription | StatisticalQuantity | CustomsValue | WorldRegion |
| Exports | GMR | Germiston/Alberton | ZA | South Africa | CN | China | 22082019 | LI | 1 | Maritime | 202106 | 2021 | 4 | 4 - Prepared foodstuffs | 22 | 22 - Beverages, spirits and vinegar | 22082019 - Other | 90 | 7951 | ASIA |
I am trying to write a loop which essentially loads each csv file one at a time, transforms it (collapses the data to the port/district office it originates from), and then saves the transformed data into a file on disk. However, I do not know how to code the loop so that it "stacks" each iteration of the transformed data into the same file. What I have so far is:
forval i= 1/10 {
clear
capture import delimited using "C:\Users\Christopher Ujma\Documents\\Report (`i').csv", case(preserve)
//generate variable to count number of shipments
gen NumberOfShipments = 1
//Collapse data to Origin, Destination and Time
collapse (sum) NumberOfShipments CustomsValue, by(DistrictOfficeCode CountryOfDestinationName YearMonth)
//command to save to file on disk
}
Essentially I am looking to use something similar to the append command, but instead of appending data from a file on disk to data in memory I am looking to do it the other way around. Also, just a note that I am using Stata 15.
Thank you!!

Comment