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: New variable based on two other variable


From   Nick Cox <njcoxstata@gmail.com>
To   "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu>
Subject   Re: st: New variable based on two other variable
Date   Sat, 6 Apr 2013 10:05:11 +0100

Your code does not catch the case of govshr == prishr and it assumes
that values are positive, which may well be true for your problem.
Otherwise I don't see why it shouldn't work.
But there are more direct ways. Here is one

gen right = max(prishr, govshr)

and here is another (although it can give an undesired answer with
missing values)

gen right = cond(prishr >= govshr, prishr, govshr)

The functions are the least appreciated important part of Stata. For a
selective review, see

Cox, N.J. 2011. Speaking Stata: Fun and fluency with functions.
Stata Journal 11: 460-471

Abstract.  Functions are the unsung heroes of Stata. This column is a
tour of functions that might easily be missed or underestimated, with
a potpourri of tips, tricks, and examples for a wide range of basic
problems.

Nick
njcoxstata@gmail.com

On 6 April 2013 09:52, David Ashcraft <ashcraftd@rocketmail.com> wrote:

> I want to generate a variable based on two existing variables.
>
> I have two variables : govshr and prishr and the variable I need is to take the value of govshr or prish whichever is greater.
> I tried the following but the result is not what I want
> 1. gen right=0
> 2. replace right=govshr if govshr>prishr
> 3. replace right=prishr if prishr>govshr
>
> What happend it, stata takes both commands (2 & 3) separately so I end up with the results of either (2) or (1).
*
*   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