Statalist


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

st: Combining ifs


From   "Gabi Huiber" <[email protected]>
To   [email protected]
Subject   st: Combining ifs
Date   Fri, 25 Apr 2008 10:48:33 -0400

Hello all,

Can you combine the programmer's if with the in-line if?

General idea:

if var1<`k' {
replace var2=. if var3=="`w'"
}

More elaborate example:

Suppose I have a price list where a handful of products, labeled k.
Each product k has several versions. For each version there is a
different price, and with each price goes a price code that identifies
it. For each product code k I have a macro that lists the prices,
called newrate_k, and another macro that lists the corresponding price
codes, called ratecode_k. Can I do this:

foreach k in ``groupby'_list' {
di "product id is `k'"
local myseries "0 ``newrate'_`k''"            // start the list of
prices at zero
local myseries_n: list sizeof myseries

forvalues i=2/`myseries_n' {
local lo=`i'-1
local hi=`i'
local label: word `lo' of ``ratecode'_`k''

local lo: word `lo' of `myseries'
local hi: word `hi' of `myseries'

if `hi'<`myseries_n' {
replace `ratecode'="`label'" if rate>`lo' & rate<=`hi' & `groupby'=="`k'"
}
else {
replace `ratecode'="`label'" if rate>`hi'                       &
`groupby'=="`k'"
}

}
}

The thing works without any error messages. But I vaguely remember
some exhortation not to combine the two types of if, and I was unable
to Google the specifics. Does anybody have any views on this?

Thank you,

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