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: Re: Storing output from a loop in only one variable


From   Eric Booth <[email protected]>
To   "<[email protected]>" <[email protected]>
Subject   Re: st: Re: Storing output from a loop in only one variable
Date   Thu, 8 Jul 2010 16:43:43 +0000

<>

Your description of what you want (and why) is still cryptic ... here's a guess at what you want to do:

*********!
webuse auto, clear

foreach a in price mpg weight turn rep78 for{
egen x`a'=mean(`a')
**you don't need "in" to go row by row**
}

su x*, mean
di r(mean)  
**note that this is only giving you the mean of the last variable in the varlist

/*       From Dani:
. I'm looking to create a single variable, x, that will hold all `a' outputs. Something like
foreach a in {range} {
gen x={output} in i
i++ //or i=i+1
}
*/

keep x*
g obs = 1==_n
keep if obs==1
drop obs
xpose, clear
rename v1 x
**you can remerge this back into your original dataset (if this really makes sense)
*********!


For your follow-up question, Stata doesn't understand the variable "t" as a number, you need to subscript it:
*****
clear
inp z y
1 0
2 0
3 0
end
gen x=.
gen int t=2
replace x=123 if _n==t
**or**
replace x =123 if _n==`=t[1]'


Again, if this is still not helping, an example dataset and code is probably a good idea (for instance, what does {range} and {output} in your original example mean?)

~ Eric
__
Eric A. Booth
Public Policy Research Institute
Texas A&M University
[email protected]
Office: +979.845.6754

On Jul 8, 2010, at 11:16 AM, Dani Tilley wrote:

> Hi Eric,
> 
> Thanks for your reply. Unfortunately, this doesn't give me what I'm looking for. 
> With the correction you pointed below, I get an x variable that only has the 
> output from the first loop on ALL its entries. I'm rather looking for a variable 
> that has -a- entries (the output from each loop on each line) and -N-a- missing 
> values. 
> 
> 
> Incidentally, could anyone tell me why I'm getting this error:
> 
> gen x=.
> gen int t=2
> replace x=123 in t
> 't' invalid obs no
> r(198);
> 
> what is a valid obs no apart from actual integers plugged in? If I can figure 
> out a way to generate a variable that would not trigger this error, I can then 
> replace var=var+i and solve the problem I have.
> 
> Thanks,
> Dani
> 
> 
> 
> 
> 
> 
> ----- Original Message ----
> 
> <>
> 
> Sorry, that should be "x", not "`x'" in the line:
> 
> 
>> replace x = `a' if !mi(`a') & mi(`x')
> 
> 
> ~ Eric
> 
> From: Dani Tilley <[email protected]>
> To: stata <[email protected]>
> Sent: Thu, July 8, 2010 11:32:19 AM
> Subject: Storing output from a loop in only one variable
> 
> 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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index