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: Re: Storing output from a loop in only one variable
From
Dani Tilley <[email protected]>
To
[email protected]
Subject
Re: st: RE: Re: Storing output from a loop in only one variable
Date
Thu, 8 Jul 2010 12:21:17 -0700 (PDT)
Thanks so much. This is very clear and helpful.
----- Original Message ----
From: Nick Cox <[email protected]>
To: [email protected]
Sent: Thu, July 8, 2010 12:30:22 PM
Subject: st: RE: Re: Storing output from a loop in only one variable
A fairly general recipe is
gen output = .
local i = 1
foreach ... {
...
replace output = ... in `i++'
...
}
su output
The presumption is that you have enough observations for this to work.
The `i++' uses the stored value of local macro i, then bumps it up.
You asked why does
... in t
not work, when -t- is a numeric variable.
-replace- expects to see a literal number or numeric range as the
argument of -in-.
... in `=t'
would work as a contraction of `=t[1]' but using local macros as above
is greatly preferable. It would work because the evaluation of `=t'
takes place before -replace- sees the rest of the command line.
On loops, see for example my namesake's articles
SJ-3-2 pr0009 . . . . . . . . . . . . . Speaking Stata: Problems with
lists
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N.
J. Cox
Q2/03 SJ 3(2):185--202 (no
commands)
discusses ways of working through lists held in macros
SJ-2-2 pr0005 . . . . . . Speaking Stata: How to face lists with
fortitude
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N.
J. Cox
Q2/02 SJ 2(2):202--222 (no
commands)
demonstrates the usefulness of for, foreach, forvalues, and
local macros for interactive (non programming) tasks
Nick
[email protected]
Dani Tilley
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.
Eric Booth
Sorry, that should be "x", not "`x'" in the line:
> replace x = `a' if !mi(`a') & mi(`x')
From: Dani Tilley <[email protected]>
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
}
*
* 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/
*
* 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/