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 at the end of May, and its replacement, is already up and running.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Replace function

From   daniel klein <>
Subject   Re: st: Replace function
Date   Wed, 20 Jun 2012 18:11:22 +0200


please read the help files for -if- and -in- qualifiers, where
expressions and ranges are explained, followed by examples.
Additionally, read the help file on -operators-. What you want is

replace sex=1 if id == 1 | id == 3 | id == 4 | id == 7 | id == 9

or, more convenient

replace sex = 1 if inlist(id, 1, 3, 4, 7, 9)

Unlike -replace-, which is a command, -inlist()- is a function.


Dear Statalisters,

I have a quick question that I cannot seem to find an answer to.

I want to replace the values of a variable only in a specified subset
of observations

For example:

set obs 10
gen id=_n
expand 3
gen sex=0
replace sex=1 if id== 1 3 4 7 9 (I only want to replace the value for
*   For searches and help try:

© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index