Bookmark and Share

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   "Furner, Zhan" <[email protected]>
To   "'[email protected]'" <[email protected]>
Subject   RE: st: How to run same code on multiple datasets
Date   Thu, 28 Nov 2013 03:11:29 +0000

Good catch, thanks a lot Alfonso! However that didn't solve the problem. I'm getting the same error message. 

Zhan Z. Furner, CPA
Ph.D. Candidate
Rawls College of Business
Texas Tech University
[email protected]




-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Alfonso Sanchez-Penalver
Sent: Wednesday, November 27, 2013 9:00 PM
To: [email protected]
Subject: Re: st: How to run same code on multiple datasets

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/

*
*   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/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index