Announcement

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

  • Generating a variable that reflect the same responses while ignoring missing values

    Hi everyone,

    I have a dataset with travel modes used in trips by individuals. The number of observations varies beween individuals. Also, not all the travel modes are informed (so i have for every individual some missing values). I need to know the individuals that did not change their travel mode between the different trips.

    The variables I use are:
    ID: individuals
    Ndep: the trip code (starting from 1)
    mode: used mode (variable with missing values)

    I tried the command:
    by ID, sort : gen same= mode[1]==mode[_N]

    But STATA do not ignore the missing values. So it considers that for an individual, an observation with a missing value is different than the others, even if the other observations have the same value.

    How to tell STATA to ignore the missing values?

    ​​​​​​​Thanks

  • #2
    The gen command appears only to compare the first and last case within ID. Is that what you want, or do you want a run?

    I'd dataex some data. You'll get more and better help that way. (And please post some interesting data that reveals your problem).

    Comment


    • #3
      Thank you for your answer,

      I want to compare all the cases per individual, if they are the same while ignoring the missing values.

      here an example

      ID_bis ndep mode
      1 1 .
      1 2 3
      1 3 .
      1 4 3
      1 5 .
      1 6 .
      2 1 2
      2 2 3
      2 3 .
      2 4 3
      3 1 3
      3 2 3
      4 1 1
      4 2 1
      4 3 1
      4 4 1
      4 5 1
      4 6 1
      4 7 .
      4 8 .
      4 9 1
      4 10 1
      4 11 1

      The 1st indidividual used only the mode 3, while the 2nd used different modes (2 and 3).

      I want to know the individuals that only use one particular mode in all their trips
      Last edited by imane imane; 22 Mar 2023, 10:04.

      Comment


      • #4
        ssc install egenmore
        Code:
        egen temp = nvals(mode) , by(id_bis)
        egen modecount = max(temp), by(id_bis)
        drop temp

        Comment


        • #5
          Thank you George

          Comment

          Working...
          X