Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Replacing observations with values from other observations


From   Nick Cox <njcoxstata@gmail.com>
To   "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu>
Subject   Re: st: Replacing observations with values from other observations
Date   Mon, 3 Jun 2013 14:57:45 +0100

Stata does not have a simple syntax for what you seem to want.

The point is made by considering

su y if obsID==374474

Your intention is, I guess wildly, that precisely one observation
satisfies the -if- qualifier, so that by extension

gen z = y if obsID ==374474 in 42

would look up the value of -y- if obsID == 374474 and then plug it
into -z- in observation 42.

But that is not how Stata works.

1. That would make -if- behave differently from -in-.

2. In any case, how -if- works is already pre-defined.

3. Syntax and semantics collide here. What would such an expression
mean if zero observations satisfied the -if-, or multiple
observations? It's not out of the question absolutely to start
erecting extra syntax or extra rules here, but I think they would
prove highly confusing.

The best Stata can do for you is something like

su y if obsid == 374474

if r(N) == 1 {
       replace z = r(min) in 42
}

where I added an explicit check on uniqueness.

See also for related discussion

SJ-6-4  dm0025  . . . . . . . . . .  Stata tip 36: Which observations? Erratum
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
        Q4/06   SJ 6(4):596                              (no commands)
        correction of example code for Stata tip 36

SJ-6-3  dm0025  . . . . . . . . . . . . . .  Stata tip 36: Which observations?
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
        Q3/06   SJ 6(3):430--432                                 (no commands)
        tip for identifying which observations satisfy some
        specified condition

 http://www.stata-journal.com/sjpdf.html?articlenum=dm0025


Nick
njcoxstata@gmail.com


On 3 June 2013 14:26, Pounds, Moses A. <MAPounds@bcps.k12.md.us> wrote:

Thanks Ryan! Using observation numbers in brackets mostly gets the job
done. Unfortunately, there is not a one-to-one correspondence between
my obsID variable and Stata's own observation numbers. Accordingly it
would make my code easier to write (and for others to later read) if
there were some way of using an expression identifying the observation
number by criteria (e.g. "if obsID==374474"), in place of Stata's
observation number. Does anyone know of a way of doing that?

Ryan Stevens

> To reference observations in Stata you use [#] after your variable, so observation number 6 for var1 can be accessed as var1[6]. Now you have to remember your obsID variable and the observation number according to Stata may be different. So your code could look like:
>
> replace var1 = var1[6] if obsID==5
>
> This replaces the var1 with the value of var1 at Stata observation 6 if obsID==5. Now this could all be simplified if obsID matches Stata's observation numbers. If there is a one-to-one mapping of obsID to Stata's observation numbering you can do this as
>
> replace var1 = var1[6] in 5
>
> This replaces the value of observation 6 at observation 5.

 On Fri, May 31, 2013 at 4:31 PM, Pounds, Moses A.

>> I would like to replace specific observations with values from other
>> observations. I can use
>>
>> replace var1 = 500 if obsID==5
>>
>> to replace the value for variable 1 in the observation with ID number
>> 5, with the quantity 500. What I would like is code that allows me to
>> make a reference to another observation, in place of that "500." Does
>> anyone have a way to do this? (My option of last resort is to do this
>> in Excel, but I would prefer to keep working in Stata.)

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index