Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

RE: st: String vector


From   "Hendri Adriaens" <[email protected]>
To   <[email protected]>
Subject   RE: st: String vector
Date   Sat, 28 Apr 2007 12:43:10 +0200

> Just like Maarten said.
> 
> foreach var of varlist a b c {
>     forvalues i = 2/3 {
>         generate `var'`i' = `i' * `var'
>     }
> }

Ah, I see, thanks. I read the help of varlist, but didn't find applications
like this one there.

> If all you have in your dataset are variables a, b and c, 
> then you can use a
> shortcut
> 
> foreach var of varlist _all { . . .

Ok.

> If you want to store the list of variable names in a local 
> macro, then you
> can
> 
> local myvarlist
> foreach var of varlist a b c {
>     local myvarlist `myvarlist' `var'
> }
> 
> And then you can
> 
> foreach element of local myvarlist { . . .
> 
> (This last example is similar to the string array or vector 
> that you're
> seeking, but you can see that it's not so efficient to do 
> what you want to
> do as what Maarten suggested.)

No, indeed, it doesn't look very efficient.

> If you have some characteristic that you want to select the 
> variables on
> (i.e., you don't know the list of variables in advance), then you can
> 
> foreach var of varlist _all {
>     capture assert [or confirm] <`var' satisfies some set of criteria>
>     if _rc continue
>     <do what you want with the variable(s) that meet(s) the criteria>
>     . . .
> }

Ok, thanks a lot, that looks useful.

Thanks for the help both, best regards,
-Hendri.


*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   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