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: RE: RE: assigning a specific row as a local variable


From   Pradipto Banerjee <pradipto.banerjee@adainvestments.com>
To   "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu>
Subject   RE: st: RE: RE: assigning a specific row as a local variable
Date   Tue, 2 Oct 2012 08:27:09 -0500

Thanks Nick, for the two examples. Both work as alternatives.

-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Nick Cox
Sent: Tuesday, October 02, 2012 3:54 AM
To: statalist@hsphsun2.harvard.edu
Subject: Re: st: RE: RE: assigning a specific row as a local variable

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 <njcoxstata@gmail.com> 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
> <pradipto.banerjee@adainvestments.com> 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: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of David Radwin
>> Sent: Monday, October 01, 2012 7:01 PM
>> To: statalist@hsphsun2.harvard.edu
>> 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: owner-statalist@hsphsun2.harvard.edu [mailto:owner-
>>> statalist@hsphsun2.harvard.edu] On Behalf Of Pradipto Banerjee
>>> Sent: Monday, October 01, 2012 3:35 PM
>>> To: statalist@hsphsun2.harvard.edu
>>> 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/

 This communication is for informational purposes only. It is not intended to be, nor should it be construed or used as, financial, legal, tax or investment advice or an offer to sell, or a solicitation of any offer to buy, an interest in any fund advised by Ada Investment Management LP, the Investment advisor.  Any offer or solicitation of an investment in any of the Funds may be made only by delivery of such Funds confidential offering materials to authorized prospective investors.  An investment in any of the Funds is not suitable for all investors.  No representation is made that the Funds will or are likely to achieve their objectives, or that any investor will or is likely to achieve results comparable to those shown, or will make any profit at all or will be able to avoid incurring substantial losses.  Performance results are net of applicable fees, are unaudited and reflect reinvestment of income and profits.  Past performance is no guarantee of future results. All f!
 inancial data and other information are not warranted as to completeness or accuracy and are subject to change without notice.

Any comments or statements made herein do not necessarily reflect those of Ada Investment Management LP and its affiliates. This transmission may contain information that is confidential, legally privileged, and/or exempt from disclosure under applicable law. If you are not the intended recipient, you are hereby notified that any disclosure, copying, distribution, or use of the information contained herein (including any reliance thereon) is strictly prohibited. If you received this transmission in error, please immediately contact the sender and destroy the material in its entirety, whether in electronic or hard copy format.

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