Statalist The Stata Listserver

[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: Re: Generating new variable from selected observations

From   "Michael Blasnik" <>
To   <>
Subject   st: Re: Generating new variable from selected observations
Date   Sat, 18 Mar 2006 08:32:00 -0500

You should use foreach (and better syntax). One way, assuming that the observations of interest are 14 and 27, would be:

foreach var of varlist variable1-variable200{
replace `var'=(`var'+`var'[_n+13])/2 if _n==14

Note that -if- qualifiers come before the comma and, although I don't fully recall, I think that the ltype(numeric) for the deprecated -for- command would come after a comma (not a colon) and would not be needed here anyway. Another approach might be just keep the two observations and use collapse, then append the combined observation onto the master dataset and drop the original 2.

Michael Blasnik

----- Original Message ----- "Julian Jiro Wimbush" <> wrote

I'm trying to adjudicate one observation that has two sets of responses by taking the mean for all
my variablers in the two sets of responses and using those means as the data.

More specifically: each observation is a medical center, and one medical center sent in two
completed surveys (two different people in the org somehow responded), and I simply want what is
currently coded incorrectly as two medical center observations to be one medical center
observation, basically by taking the means of two sets of responses for all my variables. So I
started writing some code like this:

for variable1-variable200: ltype(numeric): replace @=(@[14]+@[27])/2, if id==14 or id==27

and then simply dropping observation 27, but it didn't work. (I was trying to specify observation
14 and 27 in the replace part, but I think I've done something completely different.)

I have a feeling I may well be completely on the wrong track, but any help would be appreciated!
Thanks in advance.

*   For searches and help try:

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