I have multiple unique test level observations per patient. I want to create a variable at patient level that identify all patients that had test x performed. I tried the code below but instead of getting the result at patient level I still get a result at test level. Can anyone help me with this ? Thank you bysort id:gen had_testx="."
foreach v of var analysis {
by id, sort: replace had_testx="Yes" if `v'=="X"
}
What I want What I get
patient analysis had_testx had_testx
1 X Yes Yes
1 Y Yes
2 X Yes Yes
3 Y Yes
4 X Yes Yes
4 Y Yes
5 Y
5 Y
6 X Yes Yes
foreach v of var analysis {
by id, sort: replace had_testx="Yes" if `v'=="X"
}
What I want What I get
patient analysis had_testx had_testx
1 X Yes Yes
1 Y Yes
2 X Yes Yes
3 Y Yes
4 X Yes Yes
4 Y Yes
5 Y
5 Y
6 X Yes Yes
Comment