Announcement

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

  • categorical dummy variable

    In my dataset I have a categorical frequency variable with the values:
    Less frequently than monthly, fortnightly, weekly, and more frequently than weekly

    How do I create a dummy (indicator) variable for: Higher or equal frequency to weekly.

    I have tried using encode to generate numerical values for the frequencies and creating a dummy from that, but this hasn't worked.

    Thanks

  • #2
    Saying that something "hasn't worked" isn't very useful. There are many, many different ways in which what you did might have failed to produce your desired results--and knowing only that it hasn't worked gives no clue as to what actually went wrong. Moreover, a description of how you "tried" leaves out the many important details where an error may lurk. There is no substitute for showing the exact actual code you tried, along with an example of the data (using the -dataex- command), along with the results that Stata gave you (including any error messages). Only if you do all that will anyone be able to give you specific advice.

    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.

    When asking for help with code, always show example data. When showing example data, always use -dataex-.

    Comment


    • #3
      Hi Clyde, apologies

      Here is an example of my data:

      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input str28 FreqDR
      "Fortnightly"                 
      "Fortnightly"                 
      "Less frequently than Monthly"
      "Fortnightly"                 
      "Fortnightly"             
      "Weekly"                      
      "Weekly"                      
      "Weekly"                      
      "Weekly"                      
      end
      The code I ran was:
      Code:
      "encode FreqDR, generate (FreqDR2)"
      "generate FDRDum=0"
      
      "Freq.   Numeric  Label"
      "312         1  0"
      "2,991     2  Fortnightly"
      "13         3  Less frequently than Monthly"
      "36         4  Monthly"
      "16         5  More frequently than Weekly"
      " 548         6  Weekly"
      
      "replace  FDRDum=1 if FreqDR2=5"
      "invalid syntax"
      "r(198);"
      end

      Comment


      • #4
        I have not checked everything in your example, but in your "replace" command, you need a double equals sign between the "2" and the "5" at the end

        Comment


        • #5
          encode is wrongly applied here. It treats your categories alphabetically unless told otherwise, which produces a silly ordering in your case.

          Your distinctions seem fuzzy to me, but it is to be hoped that the original survey codebook had the ordering as administered.

          Also, your string variable has 312 values of "0" here, so what do they mean?

          This code is indicative, not definitive.

          Code:
          clear
          input str28 FreqDR
          "Fortnightly"                 
          "Less frequently than Monthly"
          "Fortnightly"             
          "Weekly"                      
          end
          
          label define freq 1 "Weekly" 2 "More frequently than Weekly" 3 "Fortnightly" 
          label define freq 4 "Less frequently than Monthly" 5 "Monthly", modify 
          
          encode FreqDR, generate(FreqDR_better) label(freq)
          In short, you must define the labels before you fire up encode. Much more at https://journals.sagepub.com/doi/abs...867X1801800413

          Comment

          Working...
          X