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: Computation across observations


From   Maarten Buis <[email protected]>
To   [email protected]
Subject   Re: st: Computation across observations
Date   Mon, 7 Nov 2011 09:12:20 +0100

On Mon, Nov 7, 2011 at 5:29 AM, Dudekula, Anwar wrote:
> How do I create a variable(Eg: newvar) whose value would be 1 if any value in a row would be equal to a particular value( eg 701.8 etc) or a set of values( eg 701.8 456.5 22.35 etc) .
>
> As the values are not integers the "egen command"  would not work here !!

This has nothing to do with -egen-, the problem is precision. I assume
your variables are stored as floates (see: -help data_types-), in that
case you need to surround your number with -float()- in order to do
such checks:

*------------------ begin example ------------------
drop _all
input ///
var1    var2    var3    var4
560.9   596.1   568     305.1
355.8   441.4   496     530.81
530.81  .       .       .
568     530.81  .       414
701.9   565.1   .       .
714     530.81  493.9   729.1
564     305.1   789.04  .
537     786.59  403.9   585.9
566     569.2   995.92  565.1
end

gen byte newvar1 = 0
foreach var of varlist var* {
	replace newvar1 = newvar1 + (`var' == float(701.9))
}
replace newvar1 = newvar1 > 0

gen byte newvar2 = 0
foreach var of varlist var* {
    replace newvar2 = newvar2 + (inlist(`var',        ///
	                                    float(701.9), ///
                                        float(569.2), ///
                                        float(22.35)) ///
                                 )
}
replace newvar2 = newvar2 > 0
*---------------- end example -------------------
(For more on examples I sent to the Statalist see:
http://www.maartenbuis.nl/example_faq )

Hope this helps,
Maarten

--------------------------
Maarten L. Buis
Institut fuer Soziologie
Universitaet Tuebingen
Wilhelmstrasse 36
72074 Tuebingen
Germany


http://www.maartenbuis.nl
--------------------------

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index