Anthony Hauser
Anthony Hauser

Reputation: 719

Create new dataset with maximum values

This is my dataset in Stata:

Dataset

I want to create a new dataset that has one row for each patient: the max value of observation.


EDIT:

I would also like to keep the date variable.

Upvotes: 0

Views: 109

Answers (1)

user8682794
user8682794

Reputation:

You can use the collapse command to get what you want:

clear

input patient observation
17133 2.2
17133 2.3
17154 3.2
17154 1.2
17154 2.2
end

collapse (max) observation, by(patient)

list, abbreviate(15)

     +-----------------------+
     | patient   observation |
     |-----------------------|
  1. |   17133           2.3 |
  2. |   17154           3.2 |
     +-----------------------+

Type help collapse for more details.


EDIT:

If you want to keep the date too:

clear

input patient observation str10 date1
17133 2.2 "01jul1965"
17133 2.3 "30jun1965"
17144 4.1 "01jul1965"
17144 3.2 "30jun1965"
17144 1.5 "30jun1965"
17154 3.2 "30jun1965"
17154 1.2 "01jul1965"
17154 2.2 "01jul1965"
end

generate date2 = daily(date1, "DMY")
format %tdDDMonCCYY date2

bysort patient (observation): keep if _n == _N

list patient observation date2, abbreviate(15)

     +-----------------------------------+
     | patient   observation       date2 |
     |-----------------------------------|
  1. |   17133           2.3   30Jun1965 |
  2. |   17144           4.1   01Jul1965 |
  3. |   17154           3.2   30Jun1965 |
     +-----------------------------------+

Upvotes: 3

Related Questions