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]
[no subject]
--------------------------------------------------------------------------------
There is no nesting here. It should suffice to cycle over the numerators
and find the matching denominators, or vice versa.
Here is one way:
foreach num of var q*num {
local den : subinstr local num "num" "den", all
local stub : subinstr local num "num" "", all
gen `stub' = `num' / `den'
}
Here is another way. This time we focus on getting the stubs by getting
a varlist of all the numerators and zapping the text "num".
unab nums : q*num
local stubs : subinstr local nums "num" "", all
foreach s in local stubs {
gen `s' = `s'num/`s'den
}
As you can see, I don't trap any cases in which a numerator doesn't have
a matching denominator, or indeed vice versa, but that could be done.
Nick
[email protected]
--- On Tue, 4/27/10, Dr. Bill Westman <[email protected]> wrote:
> From: Dr. Bill Westman <[email protected]>
> Subject: st: possible nested loop with multiple items
> To: [email protected]
> Date: Tuesday, April 27, 2010, 5:12 PM
> This is probably quite simple, but
> I've tried and failed a few times to write a working loop.
>
> What I have isthe number of observations stored in
> variables representing numerators and denominators of
> several hundred questions. The list has matched
> numerators and denominators, but each item has different
> numbers of letters (e.g.q_01a and q_01b, but q_02a-c).
> Also numbers skip.
>
> q_01a_num
> q_01b_num
> q_02a_num-
> q_02x_num
> q_02a_den
> q_02b_den
> q_02x_den
>
> I'd like to either loop through each item to
> gen prop_`i' = q_***_num/q_***_den
>
> or contract (if that is easier).
>
> Any suggestions would be appreciated.
>
> Thanks!
>
> Bill
>
>
>
> *
> * 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/