Announcement

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

  • Converting continuous variable into categorical

    Hi, I am new to STATA and would really appreciate if someone could advise me on the following:
    I would like to generate a new categorical variable that would correspond to histogram bins.
    I have 200759 observations, 7064 bins (0.005 widths); min value of the variable is -19.80374 and maximum 15.48759.

    I've tried gen with autocode but getting missing values
    Thanks.
    Last edited by Ivana Rozic; 21 Feb 2019, 17:11.

  • #2
    Have you looked at this article?

    Harrison, D. A. (2005). Stata tip 20: Generating histogram bin variables. Stata Journal, 5(2):280-281.
    https://www.stata-journal.com/sjpdf....iclenum=gr0014

    Also please see FAQ #18 on how to spell Stata: https://www.statalist.org/forums/help#spelling
    David Radwin
    Senior Researcher, California Competes
    californiacompetes.org
    Pronouns: He/Him

    Comment


    • #3
      Thank you for your help.

      I've tried the following code in Stata
      Code:
      twoway__histogram_gen Et, start(-19.805) width(0.005) frequency gen(h x)
      and it worked.
      However, it seems that bins without the observations are excluded from the x variable. Could you please advise me how I could get them as well.

      Comment


      • #4
        I'm not sure what you expect this function to produce for values of x with no observations. Do you want a value of x with a corresponding value of h equal to zero?
        David Radwin
        Senior Researcher, California Competes
        californiacompetes.org
        Pronouns: He/Him

        Comment


        • #5
          Thanks for the reply.
          What I would like to get is the following:
          If for example,

          var1
          1
          2
          3
          5
          8
          10

          I would like to get a list of all possible bins from 0 to 10. So I would like to get bins 4, 6, 7 & 9 as well.

          Comment


          • #6
            I'm not sure I understand. If you want to add 4 more observations, setting x to the values you showed and h to zero, you could do
            Code:
            set obs `=_N+4'
            replace x = 4 in `=_N-3'
            replace x = 6 in `=_N-2'
            replace x = 7 in `=_N-1'
            replace x = 9 in `=_N'
            replace h = 0 in `=_N-3'/`=_N'
            Otherwise, I don't think I, or other Statalist members, can give you any further useful advice without a better understanding of your data. Please follow the instructions in item 12.2 of https://www.statalist.org/forums/help#stata on using dataex to show some example data.
            David Radwin
            Senior Researcher, California Competes
            californiacompetes.org
            Pronouns: He/Him

            Comment


            • #7
              Thank you.

              Comment

              Working...
              X