Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: st: How to run same code on multiple datasets
From
Alfonso Sanchez-Penalver <[email protected]>
To
"[email protected]" <[email protected]>
Subject
Re: st: How to run same code on multiple datasets
Date
Wed, 27 Nov 2013 22:00:19 -0500
My first take is that you should use
use 'aaa', clear
You are not clearing the dataset in memory on the second iteration in the loop.
Best,
Alfonso Sanchez-Penalver
> On Nov 27, 2013, at 9:54 PM, "Furner, Zhan" <[email protected]> wrote:
>
> 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/
*
* 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/