Announcement

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

  • Creating categories within a nominal variable

    Hello!

    I am conducting an honors thesis in sociology and need help with creating categories within a nominal variable. I have a nominal variable (called primarybc), which includes a list of about 20 birth control options (ex: abstinence, withdrawal, condoms, spermicide, IUD, patch, etc). I am trying to run analyses, however, I do not have enough observations in each option to give meaningful results. As such, I am trying to create classes, or categories, within primarybc to group some of the birth control options. For example, I want to create a subgroup called "barrier methods" which would include condoms and spermicides, and another subgroup called "hormonal self-administered methods" which would include pills and the patch.

    How do I go about doing this? I have tried to do the following: gen classbc = 4 if primarybc == 7 | primarybc == 10 | primarybc == 13 |

    No luck. Any thoughts?

  • #2
    Well "no luck" doesn't describe what went wrong. DId Stata crash? Or hang? Did you get an error message? Or did you get some output, but it isn't what you were aiming for? When you need help troubleshooting code, you always need to show exactly how Stata responded to your code: all output and error messages it produced. And, unless it's obvious, an explanation of in what way the results you got are different from what you wanted. It is also almost always a good idea to use the -dataex- command to post example data that demonstrates the problem. If you are running version 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.


    I do notice one thing: you have an extra | at the end of your command. | is a binary operator, like + or * or & and it goes in between pairs of expressions to indicate that they are "or"ed together.

    That said, it is easier to use the -inlist()- function:

    Code:
    gen classbc = 4 if inlist(primarybc, 7, 10, 13)
    Of course you will also need to assign other values to classbc for other sets of values of primarybc. A variable whose only values are 4 and missing will be very problematic to work with.

    Comment


    • #3
      Also see the output of help recode.
      Code:
      recode primarybc (7 10 13 = 4 "Barrier") ... , generate(classbc)

      Comment


      • #4
        Wow, thank you both so much! I am new to Statalist, so I appreciate the guidance on how to best participate in the forum. When I initially ran the code, I kept receiving a "type mismatch" error message. William, your code worked beautifully, as it allowed me to group the categories and apply the desired label all in one function. Thanks again for everything!

        Comment

        Working...
        X