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: A general tip: look carefully to see where -egen- allows expressions as arguments


From   Nick Cox <[email protected]>
To   [email protected]
Subject   st: A general tip: look carefully to see where -egen- allows expressions as arguments
Date   Fri, 24 Feb 2012 12:06:20 +0000

This kind of solution occurs so frequently that it deserves a more
general flag.

With -egen- it is easy to understand what is being done when you do
something like

bysort id : egen mean_y = mean(y)

or

bysort id: egen total_y = total(y)

The expression fed to the -egen- function, -mean()- or -total()- in
these examples, is just a single variable name, here -y-. But
expressions are more general than variable names. In the example below
the expression was something like

test == "IFA"

which evaluates to 1 or 0 depending on whether it is true or false.
The code permitted is thus concise as well as being (with practice!)
fairly easy to read.

On Fri, Feb 24, 2012 at 12:22 AM, Nick Cox <[email protected]> wrote:
> bysort pat_id : egen select = total(test == "IFA")
> by pat_id : egen reject = total(inlist(test, "DFA", "CULT", "BIN"))
> keep if select & !reject
>
> Your example implies that some tests might be repeated. If there were
> exact rules on such repetition then the code might be shorter; and
> possibly otherwise.
>
> Nick
>
> On Thu, Feb 23, 2012 at 10:57 PM, Peci, Adriana (OAHPP)
> <[email protected]> wrote:
>
>> I have a long form database in which each patient identified by pat_id
>> have up to four tests performed.
>> The test names are:IFA, DFA, CULT, BIN.
>> I want to select/know how many patients had IFA but not DFA, CULT, or
>> BIN.
>>
>> Here is an improvise of my data:
>>
>> Pat_id    Test
>> 1         IFA
>> 1         DFA
>> 1         CULT
>> 1         BIN
>> 2         BIN
>> 2         IFA
>> 2         DFA
>> 3         IFA
>> 4         DFA
>> 4         CULT
>> 4         CULT
>>

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