Announcement

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

  • Unbudling a sum score of bitwise values from a checkbox survey question

    Hi,

    I made a survey of which one of the questions was in a checkbook format with 11 options. The participants were allowed to choose more than 1 option (e.g. What have you tried: nothing, workout, opiods, acupuncture, etc..)

    Bitwise values are used for the checkbox option. Each option is assigned a numeric value (i.e., 1, 2, 4, 8, 16, 32, 64,etc.). These values are summed and the total given for each particpant

    How can I unbundle the sum score and create additional variables to indicate if a participant endorsed the first option (yes/no), second option (yes/no) and so on.

    Thanks!

    Olivier





  • #2
    egenmore (SSC) has a base() function for converting numbers to strings of binary digits. Here's a stupid example.

    Code:
    clear
    set obs 100
    gen foo = _n
    egen base2 = base(foo)
    l
    forval j = 1/7 {
         gen y`j' = real(substr(base2, `j', 1))
    }
    edit
    I thought official Stata had something similar, but I didn't find it in a very brief search.

    EDIT: I did find inbase, as Joe did in #3, but it's a calculator command. The function above avoids the need for a loop over observations and building your string.
    Last edited by Nick Cox; 06 Jan 2016, 10:02.

    Comment


    • #3
      Olivier,

      It seems strange to me that you would not have access to the original responses. Be that as it may, what you are trying to do is convert the sum from base 10 to base 2. This can be done using the Stata inbase command. For example to convert the number 12345 to base 2:

      Code:
      inbase 2 12345
      This gives the binary number 110000001110001, where each digit in your case would represent the result from the individual checkbox. Of course, to apply this to entire dataset (as opposed to a single number) will take more work. Here is one possibility, using the auto dataset as an example:

      Code:
      sysuse auto, clear
      
      gen price2=""
      forvalues x=1/`=_N' {
       
        local num=price[`x']
        inbase 2 `num'
        replace price2=strofreal(`r(base)',"%014.0f") in `x'
      
      }
      Perhaps others will have an easier way to do this. If you are interested, this can also be done in Mata, which has an inbase() function.

      Regards,
      Joe

      Comment


      • #4
        Olivier,

        Nick's solution (which I didn't see until after I posted) is better than mine in that it can convert the entire dataset at once instead of one-by-one; you just need to install the egenmore package.. In my solution you would also have to add some statements such as he suggests (starting with "forval...") to parse the binary string into individual variables.

        Regards,
        Joe

        Comment


        • #5
          Dear Joe and Nick,

          Thank you so much, it worked perfectly!

          Kind regards,

          Olivier

          Comment

          Working...
          X