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]

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


From   "David Radwin" <dradwin@mprinc.com>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: assigning a specific row as a local variable
Date   Mon, 1 Oct 2012 16:00:33 -0700 (PDT)

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?
> 
> Thanks.

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