Bookmark and Share

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]

st: RE: possible nested loop with multiple items


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: possible nested loop with multiple items
Date   Wed, 28 Apr 2010 11:15:32 +0100

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] 

Dr. Bill Westman

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).  


*
*   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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index