Announcement

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

  • Generating dummy variables using non-numerical data

    Hello. I am working with survey data where participants are asked if they agree with a statement, in this case whether they consider themselves a public servant. They can choose between five answers. I would like to create a dummy variable PublicServant where 1 = yes and 0 = all other answers. I am not used to working with non-numerical answers and am unsure of the syntax needed. Do I use quotation marks around the potential answers? Is there a way to consolidate this code and say 1=yes and 0=all other answers?

    This is my drafted code but I get a mismatch error:

    gen publicservant=.
    replace publicservant=0 if PublicServant=="[2] No"
    replace publicservant=0 if PublicServant=="[3] Don't Know"
    replace publicservant=0 if PublicServant=="[-1] No answer"
    replace publicservant=1 if PublicServant=="[1] Yes"
    replace publicservant=0 if PublicServant=="[-2] Does not Apply"



  • #2
    Giving you a helpful answer here here is hard without an example of your data. Please re-read the StataList FAQ for new users on using -dataex- to show an example of your data.

    It is *possible* here, for example, that your error message has occurred because your variable PublicServant is not indeed a string, but that content such as "[2] No" is actually a value label assigned to it. Also, your long series of statements to create a dummy variable is almost certain unnecessary, and can probably be done in one line.
    Last edited by Mike Lacy; 17 Oct 2020, 08:52.

    Comment


    • #3

      Code:
      gen publicservant = PublicServant == "[1]Yes"
      or
      Code:
      gen publicservant = PublicServant == 1
      the first one works if Public Servant is a string. the second works if Public Servant is numeric with labels assigned to certain values. you can use

      Code:
      des PublicServant
      to find out if the variable is string or numeric.

      Comment


      • #4
        As Mike said, without you providing a data sample using -dataex-, it could be anything.

        Comment

        Working...
        X