Announcement

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

  • Graph with a string variable

    I have a string variable that I need to keep as the x variable. I tried transforming it to numeric, but the scale is now all messed up. I really need to keep string variable because it represents product codes. Thanks for the help.

  • #2
    At a guess you need to map to a (well-behaved) numeric variable, and here is one way:

    Code:
     
    egen numvar = group(strvar), label
    Illustration code:

    Code:
     
    clear
    input str1 strvar 
    "A"
    "B"
    "D" 
    "G" 
    "K"
    end 
    egen numvar = group(strvar), label 
    label var numvar "my preferred variable label" 
    d
    l

    Comment


    • #3
      Thanks Nick! My problem is that when I convert my product codes (which are string variables) into numeric, stata inputs the numeric code as chronological numbers, and this is why the scale of my X axis no longer reflects the scale of my original string variable.

      pssc7 prodcode
      0014101 00
      0176002 01
      0230001 02
      .
      .
      9710300 97


      When I graph the frequencies, however, my scale ends at 65 instead of 97.

      Comment


      • #4
        I don't see that you tried my solution. In any case you haven't showed us a reproducible problem. No code, no graph, ...

        See e.g. http://www.statalist.org/forums/help Section 12 for how to ask a better question.

        My wild guess is that you used a numeric variable but forgot to spell out that you want to see value labels. But why should we have to guess?
        Last edited by Nick Cox; 07 Jan 2015, 03:41.

        Comment


        • #5
          Originally posted by annettepelkmans View Post
          Thanks Nick! My problem is that when I convert my product codes (which are string variables) into numeric, stata inputs the numeric code as chronological numbers, and this is why the scale of my X axis no longer reflects the scale of my original string variable.

          pssc7 prodcode
          0014101 00
          0176002 01
          0230001 02
          .
          .
          9710300 97


          When I graph the frequencies, however, my scale ends at 65 instead of 97.
          Hi, I would try:
          Code:
          encode prodcode, generate(prodcode_num)
          If the syntax:
          Code:
          pssc7 prodcode
          0014101 00
          0176002 01
          0230001 02
          corresponds to the extract from the input command you should indicate that variable is a string:
          Code:
          input str2 prodcode
          Kind regards,
          Konrad
          Version: Stata/IC 13.1

          Comment


          • #6
            Thanks Konrad, this works!

            Comment

            Working...
            X