Hi Stata users,
I am trying to use, split and save hundreds of files with the following names below. The numbers at the start of the filenames are in the interval of 50 from 1 to 5150.
1-50_ProductAffected
51-100_ProductAffected
101-150_ProductAffected
151-200_ProductAffected
...
5101-5150_ProductAffected
Once the variable AffectedProducts is split, the dataset will be saved in it's original filename (e.g., 1_50ProductAffected) but different folder. When I run the code above, only the first filename (i.e., 1-50_ProductAffected) is correctly used, split, and saved. I can no longer use the next filename (i.e. 51-100_ProductAffected). Instead what appears is the error message: file 1-100_ProductAffected.dta not found
Definitely, my looping fails to correctly capture the naming pattern of the filenames. But I don't know how to correct it. All help is welcome.
Thank you!
I am trying to use, split and save hundreds of files with the following names below. The numbers at the start of the filenames are in the interval of 50 from 1 to 5150.
1-50_ProductAffected
51-100_ProductAffected
101-150_ProductAffected
151-200_ProductAffected
...
5101-5150_ProductAffected
Code:
forval c=1(50)51{ forval i=50(50)100{ use "`c'-`i'_ProductAffected.dta", clear //Split AffectedProducts// generate id = _n split HScode, p(,) drop HScode reshape long HScode, i(id) keep if HScode!="" drop id _j gen H2=substr(HScode, 1, 2) label var H2 "HS2 codes of HScode varialble" save "Split\\`c'-`i'_ProductAffected.dta", replace } }
Once the variable AffectedProducts is split, the dataset will be saved in it's original filename (e.g., 1_50ProductAffected) but different folder. When I run the code above, only the first filename (i.e., 1-50_ProductAffected) is correctly used, split, and saved. I can no longer use the next filename (i.e. 51-100_ProductAffected). Instead what appears is the error message: file 1-100_ProductAffected.dta not found
Definitely, my looping fails to correctly capture the naming pattern of the filenames. But I don't know how to correct it. All help is welcome.
Thank you!
Comment