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

st: Combining ifs

From   "Gabi Huiber" <>
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'                       &


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,

*   For searches and help try:

© Copyright 1996–2017 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index