Announcement

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

  • Show label value

    Hi all,
    I am new to Stata. I am working on a dataset where I have countries that are classified from 1 to 27 and each number is associated with a label (e.g. country 1 is Austria)
    I would like to play with data in order to see if there is any interesting result. For this reason I often run cycles like:

    Code:
    foreach X of numlist 1/27 {
    display `X'
    mean variableIWant if Country == `X'
    }
    but the problem is that it is displayed the number of the country while I would like to see the associated label in order to avoid wasting time while looking at the results.

    I have
    1 Austria
    2 Country2
    3 C3
    4 C4
    5 C5

    I have read all the guides that I found about labels but I have not understand how to display the associated label instead of the numeric value I would like to show "Austria" knowing that X==1
    (I got how to set, list, replace label but not how to show them)


    If someone knows python I would like to do - more or less - the same that in python is done via dictionaries e.g. countries = {1:'Austria', 2:'C2' etc.}
    countries[1] => Austria

    I hope I have been clear.
    Thanks in advance for your help.

    Have a nice time
    Last edited by Simone Fregior; 02 Feb 2015, 04:58.

  • #2
    double thread I apologize
    Last edited by Simone Fregior; 02 Feb 2015, 05:01.

    Comment


    • #3
      There are many ways of showing summaries by category in which value labels are shown. Here's one:

      Code:
       
      tab Country, su(VarIwant)
      Knowing other languages can obscure the fact that many problems which would involve loops in other languages are soluble directly in Stata.

      Comment


      • #4
        Dear Simone I think your loop can be substituted by the command tabstat or table like
        Code:
        tabstat varIWant, by(Country)

        Comment


        • #5
          Thanks a lot, this was helpful (tabulate & summarize) but do you know if it is possible to get the value of the label? (I am courious and it may be needed in more advanced programs)

          Comment


          • #6
            Yes; I can show you various techniques here.

            Code:
            . sysuse auto
            (1978 Automobile Data)
            
            . label dir
            origin
            
            . label list
            origin:
            0 Domestic
            1 Foreign
            
            . ds, has(vallabel)
            foreign
            
            . d `r(varlist)'
            
            storage display value
            variable name type format label variable label
            -------------------------------------------------------------------------------
            foreign byte %8.0g origin Car type
            
            . forval i = 0/1 {
            2. di "`i' : `: label origin `i''"
            3. }
            0 : Domestic
            1 : Foreign
            
            . forval i = 0/1 {
            2. di "`i' : `: label (foreign) `i''"
            3. }
            0 : Domestic
            1 : Foreign
            Last edited by Nick Cox; 02 Feb 2015, 05:42.

            Comment


            • #7
              @ Simone,

              I wonder if "numlabel" command wouldn't apply to your demands.

              Code:
              . numlabel, add
              You can also type "help numlabel" and see if that's what you want.

              Best,

              Marcos
              Best regards,

              Marcos

              Comment


              • #8
                Also, see Ben Jann's program fre, available from SSC.

                Code:
                . sysuse auto, clear
                (1978 Automobile Data)
                
                . fre foreign
                
                foreign -- Car type
                ----------------------------------------------------------------
                                   |      Freq.    Percent      Valid       Cum.
                -------------------+--------------------------------------------
                Valid   0 Domestic |         52      70.27      70.27      70.27
                        1 Foreign  |         22      29.73      29.73     100.00
                        Total      |         74     100.00     100.00           
                ----------------------------------------------------------------
                David Radwin
                Senior Researcher, California Competes
                californiacompetes.org
                Pronouns: He/Him

                Comment


                • #9
                  Thanks a lot. It works for the label of the elements of the variables but what if I need the label of a variable?
                  For example in the attached image ( http://www.hosting.universalsite.org...F_54D1F85A.png ) I would like to obtain "Human development index, 2011" knowing the variable "hdi"
                  I know probably it is a simple question but I could not find the answer anywhere

                  Have a nice time

                  Comment


                  • #10
                    What does "obtain" mean? You can see what the variable label by using describe.

                    Please read the FAQ Advice. It specifically advises that photos as attached images are at best awkward and at worst unreadable. We spell out how to show data, etc. in the Advice.

                    Comment


                    • #11
                      I have the "hdi" variable which label is "Human development index, 2011"
                      I would like just to have a command that returns in output "Human development index, 2011"
                      in particular I'd like to put it in

                      Code:
                      ytitle("variable label")
                      Last edited by Simone Fregior; 04 Feb 2015, 04:33.

                      Comment


                      • #12
                        Thanks; that makes the question precise.

                        Code:
                         
                        help macro 
                        
                        help extended fcn 
                        
                        ytitle("`: var label hdi'")

                        Comment

                        Working...
                        X