Notice: On March 31, it was **announced** that Statalist is moving from an email list to a **forum**. The old list will shut down on April 23, and its replacement, **statalist.org** is already up and running.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

From |
"Rubil Ivica" <irubil@eizg.hr> |

To |
<statalist@hsphsun2.harvard.edu> |

Subject |
RE: st: issue with forvalues/foreach |

Date |
Fri, 14 Dec 2012 16:24:03 +0100 |

Thanks, Maarten, your suggestion works perfectly fine. So, I should change the obviously bad habit of using backslash instead of slash. -- Ivica Rubil Ekonomski institut || The Institute of Economics, Zagreb Trg J. F. Kennedyja 7, 10 000 Zagreb, Croatia tel. +385-1-2362-269 || fax. +385-1-2335-165 irubil@eizg.hr || www.eizg.hr -----Original Message----- From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Maarten Buis Sent: 14. prosinac 2012 16:15 To: statalist@hsphsun2.harvard.edu Subject: Re: st: issue with forvalues/foreach What was causing your problem before is the backslash, which prevents the evaluation of your local macro `i'. Because Stata was not evaluating the macro Stata was looking for the file "`i'" in the directory F:\nov_dot\decomposition\data\, and obvously could not find it. Instead if you used the forward slash for your file specification, e.g.: F:/nov_dot/decomposition/data/`i', than you could gotten this loop to work without using -cd-. For more see: Nicholas J. Cox (2008) "Stata tip 65: Beware the backstabbing backslash" The Stata Journal, 8(3): 446-447. <http://www.stata-journal.com/article.html?article=pr0042> Hope this helps, Maarten On Fri, Dec 14, 2012 at 4:04 PM, Rubil Ivica wrote: > William, Rebecca, Nick, Ben, thank you all. I have finally come to > something that works: > > cd "F:\nov_dot\decomposition\data\" > > foreach i in alb arm aze bel bih bul cro geo hun /// > kaz kyr lat lit mol mon pol rom ser taj ukr { > > use "`i'.dta", clear > sum y0, d > local pline = 0.5 * r(p50) > replace y0 = y0 / `pline' > replace y1 = y1 / `pline' > save "`i'.dta", replace > > } > > > So, the -cd- solution works, but I made a mistake when I first tried it: > I used the whole path after -use- and not just the file name. > > Thank you people once again. > > Ivica > > > > -- > Ivica Rubil > Ekonomski institut || The Institute of Economics, Zagreb > Trg J. F. Kennedyja 7, 10 000 Zagreb, Croatia > tel. +385-1-2362-269 || fax. +385-1-2335-165 > irubil@eizg.hr || www.eizg.hr > > > > > > -----Original Message----- > From: owner-statalist@hsphsun2.harvard.edu > [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Benjamin > Volland > Sent: 14. prosinac 2012 15:50 > To: statalist@hsphsun2.harvard.edu > Subject: Re: st: issue with forvalues/foreach > > Bok Ivice, > > I don't know what's going on in Stata. However, I can offer a simple > solution. > Based on your initial example I ran the following code > > sysuse auto.dta > rename price var1 > rename mpg var2 > keep var1 var2 > save "F:\project\data\Test Folder\data_1.dta", replace > > sysuse autornd.dta > rename weight var1 > rename mpg var2 > keep var1 var2 > save "F:\project\data\Test Folder\data_2.dta", replace > > sysuse bplong.dta > rename patient var1 > rename bp var2 > keep var1 var2 > save "F:\project\data\Test Folder\data_3.dta", replace > > clear > cd "F:\project\data\Test Folder\" > forval i=1/3 { > use "data_`i'.dta,clear > compress var1 var2 > save "data_`i'b.dta, replace > } > > And ran into the same problem, i.e. > > . file data_1.dta, clear not found > . r(601); > > However, after simply changing the loop to > > forval i=1/3 { > use "data_`i'.dta > compress var1 var2 > save "data_`i'b.dta, replace > clear > } > > everything runs like a charm. > > HTH, Ben > > > On 14/12/2012 15:38, Rubil Ivica wrote: >> Nick, >> here is one code that I tried to run recently. >> >> >> foreach i in alb arm aze bel bih bul cro geo hun kaz kyr lat lit mol > mon >> pol rom ser taj ukr { >> >> use "F:\nov_dot\decomposition\data\`i'.dta", clear >> sum y0, d >> local pline = 0.5 * r(p50) >> replace y0 = y0 / `pline' >> replace y1 = y1 / `pline' >> save "F:\nov_dot\decomposition\data\`i'.dta", replace >> clear >> >> } >> >> And what Stata gives me is: >> >> file F:\nov_dot\decomposition\data`i'.dta not found >> r(601); >> >> end of do-file >> >> r(601); >> >> >> Strangely, Stata seems not to recognize the last "\" in the first line >> within the loop. >> >> Again, this happens whenever I have a path within a loop, be it >> -forvalues- or -foreach-. >> >> Hope you can help. >> >> Ivica >> >> >> >> -- >> Ivica Rubil >> Ekonomski institut || The Institute of Economics, Zagreb >> Trg J. F. Kennedyja 7, 10 000 Zagreb, Croatia >> tel. +385-1-2362-269 || fax. +385-1-2335-165 >> irubil@eizg.hr || www.eizg.hr >> >> >> -----Original Message----- >> From: owner-statalist@hsphsun2.harvard.edu >> [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Nick Cox >> Sent: 14. prosinac 2012 15:17 >> To: statalist@hsphsun2.harvard.edu >> Subject: Re: st: issue with forvalues/foreach >> >> Sorry, but your question remains very difficult to answer. But know >> that the macro used in the -forval- or -foreach- statement is not >> visible beyond the loop. >> >> Nick >> >> On 14 Dec 2012, at 13:27, "Rubil Ivica" <irubil@eizg.hr> wrote: >> >>> I didn't send the actual code because it is long. >>> The same problem appears whenever I have a path within the > -forvalues- >>> or -foreach- loop. >>> The files are certainly in the folders that I am referring to in >>> paths. >>> >>> >>> >>> >>> -- >>> Ivica Rubil >>> Ekonomski institut || The Institute of Economics, Zagreb >>> Trg J. F. Kennedyja 7, 10 000 Zagreb, Croatia >>> tel. +385-1-2362-269 || fax. +385-1-2335-165 >>> irubil@eizg.hr || www.eizg.hr >>> >>> -----Original Message----- >>> From: owner-statalist@hsphsun2.harvard.edu >>> [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Rebecca >>> Pope >>> Sent: 13. prosinac 2012 17:52 >>> To: statalist@hsphsun2.harvard.edu >>> Subject: Re: st: issue with forvalues/foreach >>> >>> Try this, since it looks like your files are all in the same >>> directory: >>> cd "F:\project\data\" >>> forval i=1/30 { >>> use `"dataset_`i'.dta"', clear >>> compress var1 var2 >>> save `"dataset_`i'.dta"', replace >>> } >>> >>> Note on your -foreach- loop, you tell Stata that "country" is the >>> macro. Therefore, your code should say: >>> foreach country in croatia albania { >>> use "F:\project\data\dataset_`country'.dta", clear >>> compress var1 var2 >>> save "F:\project\data\dataset_`country'.dta", replace >>> } >>> >>> I also changed the colon in the save to an underscore, since I assume >>> that is what you meant. >>> >>> If that doesn't work, please post the code you are really submitting. >>> Perhaps there is something particular to it that isn't captured by >>> your example. >>> >>> Best, >>> Rebecca >>> >>> >>> On Thu, Dec 13, 2012 at 10:36 AM, Rubil Ivica <irubil@eizg.hr> wrote: >>>> William, what your suggested does not work either, even after >>>> sustituting "/" for "\". >>>> >>>> >>>> >>>> >>>> >>>> -- >>>> Ivica Rubil >>>> Ekonomski institut || The Institute of Economics, Zagreb >>>> Trg J. F. Kennedyja 7, 10 000 Zagreb, Croatia >>>> tel. +385-1-2362-269 || fax. +385-1-2335-165 >>>> irubil@eizg.hr || www.eizg.hr >>>> >>>> >>>> -----Original Message----- >>>> From: owner-statalist@hsphsun2.harvard.edu >>>> [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of William >>>> Buchanan >>>> Sent: 13. prosinac 2012 17:28 >>>> To: statalist@hsphsun2.harvard.edu >>>> Subject: Re: st: issue with forvalues/foreach >>>> >>>> Hi Ivica, >>>> >>>> Try: >>>> >>>> forv i = 1/30 { >>>> use `"F:/project/data/dataset_`i'"', clear >>>> compress var1 var2 >>>> save `"F:/project/data/dataset_`i'b"', replace >>>> clear >>>> } >>>> >>>> It's probably a good idea not to permanently modify your data files >>> (so >>>> adding "b" to the end of the filename should make it easier to >>>> distinguish the two). Also notice the use of "/" instead of "\" in >>> the >>>> file path. Using the "/" character will work on all platforms, and >>>> avoids some of the problems that you faced. >>>> >>>> HTH, >>>> Billy >>>> >>>> >>>> On Dec 13, 2012, at 8:21 AM, Rubil Ivica wrote: >>>> >>>>> Dear Statalisters: >>>>> >>>>> When I to run the following (two artificial examples just to >>>> illustrate >>>>> the issue) >>>>> >>>>> forvalues i = 1(1)30 { >>>>> use "F:\project\data\dataset_`i'.dta", clear >>>>> compress var1 var2 >>>>> save "F:\project\data\dataset_`i'.dta", replace >>>>> clear >>>>> } >>>>> >>>>> or for instance >>>>> >>>>> foreach country in croatia albania { >>>>> use "F:\project\data\dataset_`i'.dta", clear >>>>> compress var1 var2 >>>>> save "F:\project\data\dataset:`i'.dta", replace >>>>> clear >>>>> } >>>>> >>>>> Stata returns >>>>> >>>>> file F:\project\data`i'.dta not found >>>>> >>>>> It seems strange to me that Stata "forgets" the exactly specified >>>> path: >>>>> "F:\project\data\dataset_`i'.dta" in the first example >>>>> and >>>>> "F:\project\data\dataset_`i'.dta" in the second example. >>>>> >>>>> >>>>> >>>>> >>>>> -- >>>>> Ivica Rubil >>>>> Ekonomski institut || The Institute of Economics, Zagreb >>>>> Trg J. F. Kennedyja 7, 10 000 Zagreb, Croatia >>>>> tel. +385-1-2362-269 || fax. +385-1-2335-165 >>>>> irubil@eizg.hr || www.eizg.hr >>>>> >>>>> >>>>> * >>>>> * 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/ >>> * >>> * 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/ >> >> * >> * 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/ -- --------------------------------- Maarten L. Buis WZB Reichpietschufer 50 10785 Berlin Germany http://www.maartenbuis.nl --------------------------------- * * 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/

**Follow-Ups**:**RE: st: issue with forvalues/foreach***From:*David Kantor <kantor.d@att.net>

**References**:**st: issue with forvalues/foreach***From:*"Rubil Ivica" <irubil@eizg.hr>

**Re: st: issue with forvalues/foreach***From:*William Buchanan <william@williambuchanan.net>

**RE: st: issue with forvalues/foreach***From:*"Rubil Ivica" <irubil@eizg.hr>

**Re: st: issue with forvalues/foreach***From:*Rebecca Pope <rebecca.a.pope@gmail.com>

**RE: st: issue with forvalues/foreach***From:*"Rubil Ivica" <irubil@eizg.hr>

**Re: st: issue with forvalues/foreach***From:*Nick Cox <njcoxstata@gmail.com>

**RE: st: issue with forvalues/foreach***From:*"Rubil Ivica" <irubil@eizg.hr>

**Re: st: issue with forvalues/foreach***From:*Benjamin Volland <volland@econ.mpg.de>

**RE: st: issue with forvalues/foreach***From:*"Rubil Ivica" <irubil@eizg.hr>

**Re: st: issue with forvalues/foreach***From:*Maarten Buis <maartenlbuis@gmail.com>

- Prev by Date:
**Re: st: Beta coefficients** - Next by Date:
**Re: st: Beta coefficients** - Previous by thread:
**Re: st: issue with forvalues/foreach** - Next by thread:
**RE: st: issue with forvalues/foreach** - Index(es):