Hello,

I am trying to subset one CSV file into multiple CSV files. I wrote the codes below utilizing preserve and restore, and it's for one CSV file with 1150 records. It works.

Code:
import delimited "10_Exercise session.csv", clear
scalar file_num=ceil(r(N)/100)

preserve
drop if _n>100
export delimited "10_Exercise session1.csv", replace
restore

preserve
drop if 100<=_n & _n<200
export delimited "10_Exercise session2.csv", replace
restore

...

preserve
drop if 1100<=_n 
export delimited "10_Exercise session12.csv", replace
restore
However, I have other CSV files with different numbers of records (e.g., 500 records or 700 records) so the number of CSV files at the end will be different from file to file.
I tried to use scalar for r(N) then use foreach command but failed. I don't think I have enough coding skills for this yet, so I would appreciate it if you can advise on how to write codes for the task.

Thank you.