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]

Re: st: RE: RE: assigning a specific row as a local variable


From   Nick Cox <[email protected]>
To   [email protected]
Subject   Re: st: RE: RE: assigning a specific row as a local variable
Date   Tue, 2 Oct 2012 08:53:50 +0100

Just to spell this out, building on David's insight.

 y       cond_y

 1       1
 3       0
 1       0
 2       1
 4       1

Then

gen seq = sum(cond_y)
su y if seq == 2, meanonly
local y2 = r(min)

There is at most one observation being -summarize-d, so its value is
returned as the minimum, mean, and maximum, and you can use either.

Without leaving official Stata you could go

levelsof y if seq == 2, local(y2)

which has an extra advantage in that it can be used for string variables too.

Nick

On Tue, Oct 2, 2012 at 1:22 AM, Nick Cox <[email protected]> wrote:
> -valuesof- is from SSC. Please remember that you are asked to explain
> where user-written programs you refer to come from.
>
> When defining a -local- macro [as David rightly says, not variable]
> the definition cannot be followed by an -if- command or qualifier.
>
> See also
>
> 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
>
> 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
>
> for some related technique.
>
> Nick
>
> On Tue, Oct 2, 2012 at 12:08 AM, Pradipto Banerjee
> <[email protected]> wrote:
>> Thanks, David. I wanted to assign the value to local macro `x' instead of a variable `x'. Actually, I have found a program -valuesof- which is pretty much what I was looking for.
>>
>> -----Original Message-----
>> From: [email protected] [mailto:[email protected]] On Behalf Of David Radwin
>> Sent: Monday, October 01, 2012 7:01 PM
>> To: [email protected]
>> Subject: st: RE: assigning a specific row as a local variable
>>
>> There's no such thing as a "local variable" in Stata. I think you want a
>> -tempvar- (temporary variable with a local macro name). First you declare
>> the -tempvar-, then you use it.
>>
>> Based on your description, I would create two -tempvars-. In this example,
>> n = 2.
>>
>> . tempvar x
>>
>> . tempvar cond_y_sum
>>
>> . gen `cond_y_sum' = sum(cond_y) // creates cumulative sum of values where
>> cond_y equals 1
>>
>> . gen `x' = y if `cond_y_sum' == 2 // assigns value of y to `x' for the
>> nth observation where y equals 1
>>
>> David
>> --
>> David Radwin
>> Senior Research Associate
>> MPR Associates, Inc.
>> 2150 Shattuck Ave., Suite 800
>> Berkeley, CA 94704
>> Phone: 510-849-4942
>> Fax: 510-849-0794
>>
>> www.mprinc.com
>>
>>
>>> -----Original Message-----
>>> From: [email protected] [mailto:owner-
>>> [email protected]] On Behalf Of Pradipto Banerjee
>>> Sent: Monday, October 01, 2012 3:35 PM
>>> To: [email protected]
>>> Subject: st: assigning a specific row as a local variable
>>>
>>> Hi,
>>>
>>> I want to define a local variable x as the nth value of a column y that
>>> satisfies a particular condition.
>>>
>>> More specifically, suppose I have y and cond_y as follows:
>>>
>>> y       cond_y
>>>
>>> 1       1
>>> 3       0
>>> 1       0
>>> 2       1
>>> 4       1
>>>
>>> I want to assign
>>>
>>> local x = y if "y is say the 2nd value where cond_y is true"
>>>
>>> How can I achieve this?
*
*   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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index