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


From   Nick Cox <[email protected]>
To   "[email protected]" <[email protected]>
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
[email protected]

On 6 April 2013 09:52, David Ashcraft <[email protected]> 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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index