Announcement

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

  • Generating group variable for multiple variables

    I have 8 variables MC1 - MC8. All the values in the data are categorical and all have similar related values.
    I would like to find strings across MC1-MC8.

    I want to put MC1-MC8 into a group and so I can query .. if 'value' in group then

    Is this doable in STATA?

    cheers
    Janine

  • #2
    If your variables all have values say 1 2 3 4 5 and missing, then concatenation is not only possible but also sometimes useful.

    Concatenation is always possible but not always useful.

    Code:
    egen MCall = concat(MC?)
    and check out the options in help egen.

    Some subsetting is best performed in other ways. It's not easy to say more precisely without concrete examples of what you want to do.
    Last edited by Nick Cox; 02 Jun 2024, 05:44.

    Comment


    • #3
      Thanks Nick

      Concatenation won't give me what I need.

      My data does have values 1 2 3 4 5 & missing, encoded from string.
      What I need to do is to find all instances of say "4" which could be anywhere in the 8 variables MC1-MC8.

      1 4 7 8 25 6 13 5
      2 6 28 4 1 8 21 5
      4 8 2 1 8 22 12 10

      each row has a unique id.

      I did try putting the fields into a matrix but the mata code is above my level.
      Last edited by Janine McMinn; 02 Jun 2024, 21:29.

      Comment


      • #4
        Code:
        egen there_is_a_4 = anymatch(MC1-MC8), values(4)
        This doesn't group your variables, but it directly answers the problem of finding all instances where a 4 occurs among any of the 8 variables.

        If you will need to identify more complicated configurations, it is likely that you will find your solution using both -egen, anymatch()- and other -egen- functions. See -help egen-.

        Comment


        • #5
          Brilliant thanks you

          Comment

          Working...
          X