# AW: st: Extracting scalar from a vector

 From "Martin Weiss" To Subject AW: st: Extracting scalar from a vector Date Wed, 7 Oct 2009 20:31:05 +0200

```<>

The -local- is redundant, then (as is the stepsize "(1)" ):

*************
forval i = `=x[1]'/16 {
*************

Just out of curiosity, John: You -keep- observations for state 6 first time
through the loop. In the second round, you tell Stata to -keep- observations
for state 8. How is that possible without somehow restoring the original
data?

HTH
Martin

-----Ursprüngliche Nachricht-----
Von: owner-statalist@hsphsun2.harvard.edu
[mailto:owner-statalist@hsphsun2.harvard.edu] Im Auftrag von John Pfaff
Gesendet: Mittwoch, 7. Oktober 2009 18:35
An: statalist@hsphsun2.harvard.edu
Betreff: Re: st: Extracting scalar from a vector

Thanks--that worked perfectly!

>>> Jeph Herrin <junk@spandrel.net> 10/07/09 12:21 PM >>>
If I understand correctly, -x- is a variable that
is constant across each value of -state-. In that
case

foreach s in 6 8 17 21 {
keep if state == `s'
local start=x[1]
forval i = `start' (1) 16 {
....
}
}

should do it.

hth,
Jeph

John Pfaff wrote:
> Thanks to those who helped me with my question yesterday. One more quick
question after that.
>
> I'm trying to extract a scalar value from a vector and use it in a forval
loop. The code I have is this:
>
> foreach s in 6 8 17 21 {
>   keep if state == `s'
>   forval i = XXX (1) 16 {
>  ....
>  }
> }
>
> It's the XXX that is the problem. I have a vector x that takes on
different values for each state s, and I want the forval loop to run from
that value to 16. In other words, x == 14 when s  == 6, x == 10 when s == 8,
and so on. But I have a lot of observations per state, so while x is
constant for each state it is still a vector. How do I extract its scalar
value to insert where the big XXX is in my code? This seems like it should
be simple, but I've been thwarted for a while now.
>
> Thanks so much!
>
> All the best,
>
> John Pfaff
>
> *
> *   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/

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