Announcement

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

  • Can you suggest the right command?

    Hi I have a group of individuals stored under (ID no) where 1 is the same individual, 2 is a separte individual and so on.
    vistno - is the number of times the individual when to clinic
    MI - is the condition the patient had

    Question:
    For each groups of indviduals, I want to generate a new variable 'ch1' whereby if in the MI column there is any presence of a '1' (as MI is binary)
    I would like to be coded as 1 only once

    SO

    If the same patient eg Individual2 went to a clinic twice, and had MI as 1 on each occasion, I only want it to appear once in the column ch1

    As you can see I tried:
    by IDno: egen ch1 = max(MI)

    However, this would code 1 for each time the individual (Idno) went to the clinic, when I only want Stata to just detect if ever the individual had a '1' on any occasion that person went to clinic.

    I can't not use the duplicate command - as you can see the numbers within the variables vary.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(IDno visitno MI ch1)
    1 1 0 0
    1 2 0 0
    2 1 1 1
    2 2 1 1
    3 1 1 1
    3 2 1 1
    3 3 1 1
    4 1 0 1
    4 2 0 1
    4 3 1 1
    4 4 1 1
    end

  • #2
    I have also tried this:

    egen test = max(MI), by(IDno)


    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(IDno visitno MI test)
    1 1 0 0
    1 2 0 0
    2 2 1 1
    2 3 1 1
    2 1 1 1
    3 3 1 1
    3 5 1 1
    3 4 1 1
    3 2 1 1
    3 1 1 1
    4 5 0 0
    4 4 1 1
    4 1 0 1
    4 3 1 1
    4 2 0 1
    end

    As you can see I would like to see the red text only and the remaining being 0.
    But with the above code its generate the Max no (which would be 1) for anytime it appears in MI and hence subsequently repeats itself per IDno in the test variable

    Comment


    • #3
      Hi there (again) - So I actually found the command
      This seems to work

      collapse (max) MI, by(IDno)

      As it produces this:

      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input float(IDno MI)
      1 0
      2 1
      3 1
      4 1
      end


      However, as I need to do this for 10 other variables and it seems cumbersome to keep saving the data all the time.....
      Is there a shorter way to this ?

      Comment


      • #4
        You can do this for multiple variables in a single -collapse- command. See
        Code:
        help collapse

        Comment

        Working...
        X