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: Loop over a list of strings with logs


From   Brandon Olszewski <[email protected]>
To   [email protected]
Subject   Re: st: Loop over a list of strings with logs
Date   Wed, 29 Jan 2014 14:01:23 -0800

Thank you, Nick!
I like the encode suggestion to reduce issues with the strings. I'll
go for it. Thank you!
Cheers,
Brandon

On Wed, Jan 29, 2014 at 12:19 PM, Nick Cox <[email protected]> wrote:
> Stata is choking on
>
> keep if school == adams
>
> which only makes sense if -adams- is a string variable. But it's not,
> so Stata complains.
>
> It should work if you had set up your code so that you got.
>
> keep if school == "adams"
>
> That said, your code looks fragile if you have spaces in your names,
> e.g. "William Gould". Also, don't use backslashes before local macro
> references. Also, your -display- command won't do what you want.
>
> I'd recommend something more like this.
>
> encode school, gen(n_school)
> su n_school, meanonly
>
> forval i = 1/`r(max)' {
>     local where : label (n_school) `i'
>     di "`where'"
>     local where : subinstr local where " " "_", all
>     preserve
>     log using C:/stata/`where'_results.txt, text replace
>     keep if n_school==`i'
>     tab item1
>     tab item2
>     log close
>     restore
> }
>
> Nick
> [email protected]
>
>
> On 29 January 2014 19:56, Brandon Olszewski <[email protected]> wrote:
>> 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".
>>
> *
> *   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/



-- 

///////////////////////
Brandon Olszewski, Ph.D.
Senior Education Consultant, ISTE
Adjunct Instructor, University of Oregon
[email protected]
*
*   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