Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, statalist.org is already up and running.


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

Re: st: Storing output from a loop in only one variable


From   Eric Booth <ebooth@ppri.tamu.edu>
To   "<statalist@hsphsun2.harvard.edu>" <statalist@hsphsun2.harvard.edu>
Subject   Re: st: Storing output from a loop in only one variable
Date   Thu, 8 Jul 2010 15:45:09 +0000

<>

Sorry, that should be "x", not "`x'" in the line:

> replace x = `a' if !mi(`a') & mi(`x')


~ Eric

__
Eric A. Booth
Public Policy Research Institute
Texas A&M University
ebooth@ppri.tamu.edu
Office: +979.845.6754

On Jul 8, 2010, at 10:41 AM, Eric Booth wrote:

> <>
> 
> I think you just want:
> 
> gen x = .
> foreach a in `range' {
> replace x = `a' if !mi(`a') & mi(`x')
> }
> 
> Unless I am not understanding your description (if not, an example with some sample data would be useful)
> 
> Also, note that your loop:
> 
> scalar i=1
> foreach a in {range} {
> ...
> gen x={output} in i
> i++ //or i=i+1
> }
> 
> would not work because you are asking Stata to re-generate "x" each time it passes through the loop.
> 
> ~ Eric
> 
> __
> Eric A. Booth
> Public Policy Research Institute
> Texas A&M University
> ebooth@ppri.tamu.edu
> Office: +979.845.6754
> 
> On Jul 8, 2010, at 10:32 AM, Dani Tilley wrote:
> 
>> Hi,
>> 
>> I'm trying to store certain output in a new variable while I loop through 
>> different categories. At the end I want to average what I've stored and perhaps 
>> do some other manipulations. Right now, my code looks like this:
>> 
>> foreach a in {range} {
>> ...
>> gen x`a'={output} in 1
>> ...
>> }
>> 
>> su x*, mean
>> di r(mean)
>> drop x*
>> 
>> So I create `a' variables that have the desired output as their 1st observations 
>> and N-1 missing points. I then average across these variables and drop them. 
>> While this gets the job done, I feel it's very clumsy. I'm looking to create a 
>> single variable, x, that will hold all `a' outputs. Something like
>> 
>> scalar i=1
>> foreach a in {range} {
>> ...
>> gen x={output} in i
>> i++ //or i=i+1
>> }
>> 
>> Thanks,
>> DF Tilley
>> 
>> 
>> 


*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index