Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • select specific observation among several variables

    Hello everyone,

    Using STATA16, I have a dataset with 20 variables dx1 to dx20. I want to keep only observations with T40xxxx, T41xxxx, T42xxxx, and T43xxxx. When I use "keep" command for the first variable (dx1) I missed the other variables.

    keep if inlist(substr(dx1, 1, 3), "T40", "T41", "T42", "T43")

    Another way that I tried is to generate new var that helped me to gen a new var contains observation with T40xxxx, T41xxxx, T42xxxx, and T43xxxx.

    gen DX1= dx1 if inlist(substr(dx1, 1, 3), "T40", "T41", "T42", "T43")

    This way takes a lot of time. I am wondering how I can keep only observations in each variable (dx1 to dx20) that contain T40xxxx, T41xxxx, T42xxxx, and T43xxxx. Is there any other way for doing this, for example, writing a loop for :

    gen DX1= dx1 if inlist(substr(dx1, 1, 3), "T40", "T41", "T42", "T43")



    Thank you in advance for your advice.

    Best
    Click image for larger version

Name:	Data Editor (Browse) 2020-02-14 19-57-45.png
Views:	1
Size:	503.3 KB
ID:	1536669


Working...
X