Announcement

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

  • How do I get a table that shows what each variables' numeric value is?

    I've literally spent 2 hours Googling this. How <moderator edit> is this so hard?!?! I just want a list of the corresponding numerical values of each variable. I'm so freaking frustrated. Something like

    Civic - 1
    VW Gofl - 2
    F-150 - 3

    And after that, is there a way to get stata to recognize words instead of numbers? Like if I use inlist(), can I make it so I can write inlist(model, Civic, VW Golf, F-150) instead of inlist(model, 1, 2, 3)

    Thanks
    Last edited by sladmin; 07 Nov 2016, 08:41.

  • #2
    for the first question, it sounds like you want "label list" followed by the label name (assumes your variables is numeric with labels; if not, please give more info)

    for the second, when you use insist with strings, you must surround each string with double quote marks

    Comment


    • #3
      You're perhaps the first person in 22 years of Statalist to use some 12-year old boys' favourite rude word. (Perhaps that should be 7 year-olds. I am not in touch.) I hope it's 22 more years before we hear it again. Rude language is not appreciated.

      Your first question could mean several things. The example comes out of nowhere. If you mean something like this

      Code:
      . sysuse auto, clear
      (1978 Automobile Data)
      
      . gen make1 = word(make, 1)
      
      . tab make1
      
            make1 |      Freq.     Percent        Cum.
      ------------+-----------------------------------
              AMC |          3        4.05        4.05
             Audi |          2        2.70        6.76
              BMW |          1        1.35        8.11
            Buick |          7        9.46       17.57
             Cad. |          3        4.05       21.62
            Chev. |          6        8.11       29.73
           Datsun |          4        5.41       35.14
            Dodge |          4        5.41       40.54
             Fiat |          1        1.35       41.89
             Ford |          2        2.70       44.59
            Honda |          2        2.70       47.30
            Linc. |          3        4.05       51.35
            Mazda |          1        1.35       52.70
            Merc. |          6        8.11       60.81
             Olds |          7        9.46       70.27
          Peugeot |          1        1.35       71.62
            Plym. |          5        6.76       78.38
            Pont. |          6        8.11       86.49
          Renault |          1        1.35       87.84
           Subaru |          1        1.35       89.19
           Toyota |          3        4.05       93.24
               VW |          4        5.41       98.65
            Volvo |          1        1.35      100.00
      ------------+-----------------------------------
            Total |         74      100.00
      that's one answer. Otherwise F-150 could not be a variable name in Stata, as hyphens could not be part of a variable name, so it could not have a value. Please rephrase your question with a real data-based example if I am not understanding.

      inlist() takes string arguments too. Its help tells you that, which is the first thing to look at.

      In the example, I just created you could do things like

      Code:
      . count if inlist(make1, "Toyota", "Volvo")
          4


      Comment


      • #4
        So I take it you have a variable, let's call it car, that is numerically coded 1, 2, 3, etc. but has value labels attached, so that it displays as Civic, VW Golf, F-150 etc.

        So the first step is to find the name of the label:
        Code:
        des car
        The output will have a column headed "value label" and underneath that column header you will find the name of the value label. Let's say that that name is "brand".

        Then all you need to get the correspondence is:
        Code:
        label list brand
        and Stata will show it to you.

        As for your second question, again assuming the name of the value label is "brand", you can write
        Code:
        ...inlist(model, "Civic":brand, "VW Golf":brand, "F-150":brand)
        and Stata will use the numeric values associated with those text strings in the value label brand.

        I've literally spent 2 hours Googling this. How <moderator edit> is this so hard?!?! I just want a list of the corresponding numerical values of each variable. I'm so freaking frustrated.
        It isn't hard at all. Did you look at the on-line help or manual section for -label-? I suppose not. I suppose it didn't occur to you because, I suppose, you don't even know about value labels and the -label- family of commands. So, just as, Apple Corp. not withstanding, it usually pays to read the instructions before using a product, one of the most efficient uses you can make of your time as a Stata user is to read the Getting Started [GS] and User's Guide [U] sections of the on-line manuals that come with your Stata installation. They lead you through the fundamental commands used in data management and analysis in Stata. You won't absorb all the details or remember all the syntax from one reading, but at least you will know what's available. And had you done that, probably your first instinct with this problem would have been to check the help for the -label- command. It would have saved you hours of fruitless Googling. But it's not too late. If you do it now, it'll save you hours of frustration in the future.

        As an aside, but seriously, do you think you might have found a more professional way to phrase your question?

        Added: Crossed with Nick's response, which interprets your question differently.

        Added later: Also crossed with Rich's response, which interprets the question as I did.
        Last edited by sladmin; 07 Nov 2016, 08:45. Reason: Questionable language choice from original posting.

        Comment


        • #5
          Cross-posted by "Hutchins" at http://stackoverflow.com/questions/4...iable-in-stata

          and by "curiosdog222" in https://www.reddit.com/r/stata/comme...attributed_to/

          http://www.statalist.org/forums/help#crossposting explains our cross-posting policy, which is that you should tell us about it.

          In the first you say

          I have a dataset. each variable has a corresponding value.
          If a dataset has a single observation, then yes. Otherwise it's hard to know what that means.
          Last edited by Nick Cox; 04 Nov 2016, 18:08.

          Comment


          • #6
            So do I need to attach ":brand" after every term? And what does "brand" refer to in this hypothetical dataset?

            Also forgive me for swearing under high tempers. As fellow human beings, I'm sure you've all let your emotions prevail against cooler heads before. I'll be more civil form now on. I'm sorry.
            Last edited by Tommie Thompson; 04 Nov 2016, 23:17.

            Comment


            • #7
              So, when your head is cooler and you can concentrate, re-read #4 carefully: it fully explains in the beginning what "brand" is in the hypothetical data set and how you find it. I can't think of a simpler way to explain it than what is already there.

              Comment


              • #8
                I'm confused because you used both "model" and "brand." The dataset I'm imagining is a column with the header "model," and a bunch of values underneath. So what would "brand" correspond to?

                My understanding is that "model" refers to the "variable name," while "brand" refers to the "value label." i would need to use both in inlist(). Correct?

                If so, just out of curiosity, why is it done this way? Why can't the variable name be the same thing as the variable label? In other words, why is are the labels divided between a "variable name" and a "variable label," since to my understanding as a beginner, they seem to function the exact same way. Thanks
                Last edited by Tommie Thompson; 04 Nov 2016, 23:08.

                Comment


                • #9
                  My understanding is that "model" refers to the "variable name," while "brand" refers to the "value label."
                  That's correct.
                  i would need to use both in inlist(). Correct?
                  Yes. The first argument would be the variable name. The other arguments would all be suffixed by ":variable_label_name"

                  Why can't the variable name be the same thing as the variable label?
                  It can be. And when I create my own data sets, I usually use the same name for the variable and the value label (except for using certain standard value labels like "boolean", or when there are several variables that are all coded the same way and I want to use the same value label for all of them.) The point is,though, that if you didn't create this data set yourself, somebody else may have had their reasons for using a different name for the value label and for the variable name. And if I illustrated it on the assumption that both were named the same, but your actual data set isn't that way, you would be left not knowing what to do with that. So I illustrated the more general situation: if the value label is also called model, then you just use :model every place that I used :brand. You have to use whatever the actual value label name is in that context. As noted, you can find the value label's name by running -des model-.

                  Comment


                  • #10
                    Cool thanks! I think I get it now.

                    Comment

                    Working...
                    X