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 
 
David Ashcraft <[email protected]> 
To 
 
"[email protected]" <[email protected]> 
Subject 
 
Re: st: New variable based on two other variable 
Date 
 
Sat, 6 Apr 2013 02:42:59 -0700 (PDT) 
Thanks to both Nick and Daniel. I do have missing values. I did manage to produce new variable by using the following command as suggested by Nick:
gen right = max(prishr, govshr)
I want to change the missing values to zeros I tried to -mvencode- I got the following results
mvencode govsh mv(0), override
factor variables and time-series operators not allowed
Based on an old post from Nick, I did the following
. gen gov =  govsh
(936 missing values generated)
. 
. gen pub =  pubsh
(1404 missing values generated)
. su  govsh pubh gov pub
    Variable |       Obs        Mean    Std. Dev.       Min        Max
-------------+--------------------------------------------------------
       GovSh |       660    40.53545    28.24789         10        100
       PubSh |       192       36.47    20.73895      10.94         83
         gov |       660    40.53545    28.24789         10        100
         pub |       192       36.47    20.73895      10.94         83
mvencode  gov pub mv(0)
factor variables and time-series operators not allowed
r(101);
Please tell me, how can I encode the missing values to zero?
Regards
David
----- Original Message -----
From: Nick Cox <[email protected]>
To: "[email protected]" <[email protected]>
Cc: 
Sent: Saturday, April 6, 2013 12:05:11 PM
Subject: Re: st: New variable based on two other variable
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/
*
*   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/