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   Sergiy Radyakin <[email protected]>
To   "[email protected]" <[email protected]>
Subject   Re: st: How to run same code on multiple datasets
Date   Wed, 27 Nov 2013 22:01:18 -0500

Zhan, you probably need save `"`aaa'"', replace. And perhaps something
better than "aaa", perhaps "oneFile" for readability. Best, Sergiy
Radyakin

On Wed, 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/


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