The cond() function


I really have not given attention to the -cond()- function until Nick Cox showed in Statalist how it can make life so much easier (and do-files much shorter). To see what I mean, check out this Statalist thread: st: generating parent variable in child level data (Shikha Sinha).

As you have seen, the 15-line solution I have suggested (re-posted below):

gen age_mother=.
gen age_father=.
gen educ_mother=.
gen educ_father=.
levelsof hhid, local(levels)
foreach i of local levels{
qui: sum age if relation==”mother” & hhid==i'
<strong>replace </strong><em>age_mother</em>=r(mean) if<em> hhid</em>==
i’

qui: sum age if relation==”father” & hhid==i'
<strong>replace </strong><em>age_father</em>=r(mean) if <em>hhid</em>==
i’

qui: sum education if relation==”mother” & hhid==i'
<strong>replace </strong>educ_mother=r(mean) if <em>hhid</em>==
i’

qui: sum education if relation==”father” & hhid==i'
<strong>replace </strong><em>educ_father</em>=r(mean) if <em>hhid</em>==
i’
}
recode age_* educ_* (*=.) if relation != “son” & relation != “daughter”

/* This last line is added as suggested by Eric Booth who has also suggested a very nice solution to the same problem. Thanks to Richard Ohrvall for his error alert. */

can be concisely written (by Nick Cox ) as:

egen age_mother = mean(cond(relation == “mother”, age, .)), by(hhid)
egen age_father = mean(cond(relation == “father”, age, .)), by(hhid)
egen educ_mother = mean(cond(relation == “mother”, education, .)), by(hhid)
egen educ_father = mean(cond(relation == “father”, education, .)), by(hhid)

/* I just added the last 3 lines to complete the code. Take note also of the caveats pointed out in the thread. */

Now, I really need to read this: Depending on conditions: a tutorial on the cond() function (Kandor and Cox, 2005).

2 Responses

  1. Interesting post. However, I think you made some mistake with your last line. That if-statement doesn’t make sense.

    All the best,
    Richard

Leave a Reply