Announcement

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

  • Generating dichotomous variables using the "or" qualifier

    Hi,

    I apologise in advance for my extremely novice coding question, but it has been some time since I last worked with Stata and I haven't been able to find a proper answer to my question through general online searching.

    Basically, I would like to create a dichotomous variable (dummy1) which equates to 1 if at least one of my 7 4-point likert scale-type variables equates to the value label of 1 i.e catvar1==1. Each of these likert variables have been coded in the exact same way.
    I've attempted to produce this variable by coding:

    dummy1=0
    replace dummy1=1 if catvar1==1 | catvar2==1 | catvar3==1 | catvar4==1 | catvar5==1 | catvar5==1 | catvar1==1

    My problem, however, is that I'm unsure if this is the correct code to use in order to create my desired variable. The output I receive when I cross-tabulate dummy1 with the various likert variables doesn't seem to match.

    Any assistance to provide some clarity on what the best code to use in this situation would be very appreciated!

    Thank you in advance for any time taken to address this seemingly simple question.
    Michaella

  • #2

    Code:
    egen rowmax = rowmax(catvar?)
    will be 1 if any of the (0, 1) input variables is 1 and 0 otherwise.

    Your code

    Code:
    dummy1=0
    replace dummy1=1 if catvar1==1 | catvar2==1 | catvar3==1 | catvar4==1 | catvar5==1 | catvar5==1 | catvar1==1 
    is right in spirit but incomplete and in need of a typo fix:

    Code:
    gen dummy1=0
    replace dummy1=1 if catvar1==1 | catvar2==1 | catvar3==1 | catvar4==1 | catvar5==1 | catvar6==1 | catvar7 == 1


    Note that your code could be condensed to

    Code:
    gen dummy1 = catvar1==1 | catvar2==1 | catvar3==1 | catvar4==1 | catvar5==1 | catvar5==1 | catvar6==1 | catvar7 == 1
    and also could be rewritten as

    Code:
    gen dummy1 = inlist(1, catvar1, catvar2, catvar3, catvar4, catvar5, catvar6, catvar7)


    In practice I think experienced users would tend to use either the first or the last solution.

    NOTE: In practice experienced users also make mistakes like everyone else. William (#3) caught that there are supposed to be 7 of these beasts.
    Last edited by Nick Cox; 21 Sep 2017, 18:14.

    Comment


    • #3
      Let me note that in seeing the typo I missed, Nick missed the typo I saw. Fully correct to account for all seven of the categorical variables mentioned in post #1 we have
      Code:
      replace dummy1=1 if catvar1==1 | catvar2==1 | catvar3==1 | catvar4==1 | catvar5==1 | catvar6==1 | catvar7==1

      Comment


      • #4
        Hi,

        Thank you so much for your advice! The code suggested looks much neater than mine, and I'll definitely incorporate it in my analysis. I apologise for the typos in my question. They were the product of a very late night.

        Comment

        Working...
        X