Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
From | "Furner, Zhan" <zhan.furner@ttu.edu> |
To | "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu> |
Subject | st: How to run same code on multiple datasets |
Date | Thu, 28 Nov 2013 02:54:10 +0000 |
Hello all, I'm trying to create a do file (see below) that would allow me to run the same code on multiple datasets (the first dataset is yearind19902, then yearind19903...). I think the file runs well for the first dataset because the variable 'ratio' is created correctly in the first dataset. However, Stata stopped executing for the rest of datasets and I got an error message as follows: invalid `"yearind19903.dta' r(198); ***Begin Code **** local workdir "C:\xxx\xxx\xxx" cd `workdir' local files: dir "`workdir'" files "*.dta" foreach aaa of local files { use `aaa' sort plan count if plan<1 gen n1=r(N) count if plan>0 gen n2=r(N) gen ratio=n1/n2 if plan<1 replace ratio=1 if missing(ratio) save `files', replace } *** End Code *** How should I amend my code to fix this problem? Thank you so much and happy Thanksgiving! Zhan * * For searches and help try: * http://www.stata.com/help.cgi?search * http://www.stata.com/support/faqs/resources/statalist-faq/ * http://www.ats.ucla.edu/stat/stata/