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 on April 23, and its replacement, is already up and running.

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

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

From   Dani Tilley <>
To   stata <>
Subject   st: Re: Storing output from a loop in only one variable
Date   Thu, 8 Jul 2010 09:16:26 -0700 (PDT)

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 

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

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.


----- Original Message ----


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

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

~ Eric

From: Dani Tilley <>
To: stata <>
Sent: Thu, July 8, 2010 11:32:19 AM
Subject: Storing output from a loop in only one variable


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

DF Tilley

*   For searches and help try:

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