Statalist The Stata Listserver


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

Re: st: Grouped data and changing a variable


From   n j cox <n.j.cox@durham.ac.uk>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Grouped data and changing a variable
Date   Thu, 25 May 2006 14:55:28 +0100

The trick used by David is explained more generally
in an FAQ:

How do I create a variable recording whether any members of a group (or all members of a group) possess some characteristic?

http://www.stata.com/support/faqs/data/anyall.html

Incidentally, the syntax of -egen, max()- is that it works
on an expression, and not just a variable name.

Hence

bysort a b c : egen newe = max(d == "Paper")

works, and there is no need for any intermediate variable.

Nick
n.j.cox@durham.ac.uk

Catrina Jordan wrote

I have a set of data where I need to replace a variable's content
depending if it is in a group and one of the variables contains a
certain value.

For example
a b c d e
101 1 01-May-06 Paper 1
101 1 01-May-06 Disc 0
101 1 01-May-06 Electronic 0
102 1 02-May-06 Paper 1
102 1 02-May-06 Disc 0
103 2 02-May-06 Disc 0
103 2 02-May-06 Electronic0


I know that the first three observations are in the same group (using
the egen command for variable a, b and c) and the second group is the
next two observations and so on but in each group if variable d contains
"Paper" then I want all the observations in the group for variable e
changed to a 1. If there is no "Paper" in the group then to leave as
they are.


So to end up with
a b c d e
101 1 01-May-06 Paper 1
101 2 01-May-06 Disc 1
101 3 01-May-06 Electronic 1
102 1 02-May-06 Paper 1
102 2 02-May-06 Disc 1
103 2 02-May-06 Disc 0
103 2 02-May-06 Electronic0

and David Harrison suggested

Does the current value of e just represent -d=="Paper"-?

If so...

bysort a b c: egen newe = max(e)

(If not, -replace e = d=="Paper"- first)


*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/




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