Statalist


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

R: st: a possible - foreach - question?


From   "Carlo Lazzaro" <[email protected]>
To   <[email protected]>
Subject   R: st: a possible - foreach - question?
Date   Tue, 14 Oct 2008 18:06:25 +0200

Dear Nick,
thanks a lot for your kind reply.
At the end of the day, it seems I got off with a remarkable support from
Austin, Ben, Johannes, Martin and You (in alphabetical order).

Thanks a lot and Kind Regards,

Carlo

-----Messaggio originale-----
Da: [email protected]
[mailto:[email protected]] Per conto di Nick Cox
Inviato: martedì 14 ottobre 2008 17.12
A: [email protected]
Oggetto: RE: st: a possible - foreach - question?

This was in essence my reading too. -foreach- and especially -levelsof-
are not at all suited for the problem originally posed by Carlo. I would
tweak Austin's recipe, although I have not tested the difference:

gen d = . 
gen work = . 

qui forval i = 1/10000 { 
	scalar temp = a[`i'] 
	replace work = temp * b - c 
	su work, meanonly 
	replace d = r(mean) in `i' 
} 

The main difference is to get Stata to look up the element of -a- just
once in each loop, rather than 10,000 times, unless Stata optimises that
(my guess is it doesn't). 

Nick 
[email protected] 

Austin Nichols

Carlo Lazzaro <[email protected]>
Not sure I've understood, but if so, here is one method of calculation.

use bp* tcresult using http://www.stata-press.com/data/r9/nhanes2, clear
ren bpsystol a
ren bpdiast b
ren tcresult c
g double d=.
qui forv i=1/`=_N' {
 if a[`i']<. {
  g double tmp=a[`i']*b-c
  su tmp, meanonly
  replace d=r(mean) in `i'
  drop tmp
  }
 }

On Tue, Oct 14, 2008 at 4:50 AM, Carlo Lazzaro
<[email protected]> wrote:

> I have three variables (a; b; C with 10,000 observations each) in long
> format.
> For each value of a I would like to obtain the following one:
>
> (a1 * b1) - c1
> .
> .
> .
> .
> (a1*b10,000)-c10,000;
>
> Repeat the above reported would-be 10,000 times (i.e., from a1 to
a10,000)
>
> Create a new variable d in which the mean of each one of the 10,000
> iterations are stored and can be displayed.
>
> I suppose there's way of doing this with Stata 9.2/SE, but I cannot
figure
> out the proper syntax.

*
*   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/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index