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]

st: RE: Loop over a list of strings with logs


From   "Sarah Edgington" <[email protected]>
To   <[email protected]>
Subject   st: RE: Loop over a list of strings with logs
Date   Wed, 29 Jan 2014 12:10:13 -0800

Brandon,
You want your third line of the loop to read:
    keep if school=="`i'"

Otherwise Stata reads it as  keep if school==adams
Since there is no variable adams to compare to, Stata complains.

You also probably want to close the quotes on your log file name.

-Sarah

-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Brandon Olszewski
Sent: Wednesday, January 29, 2014 11:56 AM
To: [email protected]
Subject: st: Loop over a list of strings with logs

I am using Stata 12. I have a list of schools that I want to loop some
commands over, including logging output. I have consulted the
"Speaking Stata:  How to face lists with  fortitudeQ2/02   SJ
2(2):202--222" article and searched the list, but I am still mystified. This
is what I want to do:

I have a list of schools (var = school, string format), call them "adams"
"billings" and "charles". I want to perform the following operations that
will yield by school results: start a log that is named for the respective
school, keep observations only for one school, display the school's name,
tab responses to item1 and item2, and close the log. I think it should be
something like this:

foreach i in adams billings charles {
    preserve
    log using "C:\stata\`i'_results.txt, text replace
    keep if school==`i'
    di in r school
    tab item1
    tab item2
    log close
    restore
    }

I know I can also do something like:

local schools "adams billings charles"
foreach i in `schools' {
   preserve
   etc...
   }

When I try anything, Stata tells me that "adams not found".

I would really appreciate any help. I know I've done this before, but just
don't see what I'm doing wrong. Thanks.
*
*   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