Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: identifying even & odd numbers

From   Roger Newson <>
Subject   Re: st: identifying even & odd numbers
Date   Tue, 30 Mar 2004 19:18:55 +0100

At 13:10 30/03/04 -0500, Danielle Ferry wrote:
I apologize if this has been addressed previously. I haven't found anything in the archives, though I feel as though it surely must be there somewhere. I am wondering if there is a way to create a new variable that takes on a value if another variable is an even or an odd number. For example, can you do something like this:
ge newvar = 1 if oldvar is even
If this has made its way thru the list before, perhaps someone can suggest better search keywords for the archives? Thanks.
Under -help functions-, you can click on -mathfun-, and find the -mod()- function. The assignment

generate z=mod(x,y)

causes -z- to be equal to -x- modulo -y-, ie the remainder of -x- if divided by -y-. So, if you type

gene odd=mod(x,2)

then odd will be 1 for odd -x- and 0 for even -x-.

I hope this helps.


Roger Newson
Lecturer in Medical Statistics
Department of Public Health Sciences
King's College London
5th Floor, Capital House
42 Weston Street
London SE1 3QD
United Kingdom

Tel: 020 7848 6648 International +44 20 7848 6648
Fax: 020 7848 6620 International +44 20 7848 6620
or 020 7848 6605 International +44 20 7848 6605

Opinions expressed are those of the author, not the institution.

* For searches and help try:

© Copyright 1996–2015 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index