Announcement

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

  • Capitalising first letter of value labels

    In Stata 14 I can change letter case of variables names in a single click by the following formula:

    Code:
    country
    
    rename *, uppercase
    
    COUNTRY

    Is it possible to change the value labels as well:

    Code:
    algeria to Algeria

  • #2
    Your post is a bit confusing. -rename *, upper- does not just capitalize the first letter of the variable names. It changes them completely to upper case. If you want to change only the first letter of a value label to upper case, the following code illustrates the approach using the online nlswork data set as an example:

    Code:
    webuse nlswork, clear
    
    label dir
    label list racelbl
    
    levelsof race, local(races)
    
    foreach r of local races {
        local lbl: label racelbl `r'
    //     local lbl = upper(`"`lbl'"')
        local lbl = upper(substr(`"`lbl'"', 1, 1)) + substr(`"`lbl'"', 2, .)
        label define racelbl    `r'    `"`lbl'"', modify
    }
    
    label list racelbl
    If what you actually want is to convert the entire label to upper case, then un-comment the commented out -local lbl- command and comment out (or just delete) the one below it.

    If you want to do this to all of the value labels in the entire data set, you can get a list of the names of those value labels by running -label dir-, and then pick up the list of names of labels from r(names). Then run the above code inside a loop over those label names.

    Comment


    • #3
      Cylde points in the right direction. His example code, however, assumes that all values are indeed labeled and that all labeled values do indeed appear in the dataset at hand. The labvalch3 command, part of labutil2 (from SSC) offers a canned solution that essentially implements Clydes code. The syntax is very similar to rename, in this case.

      Code:
      labvalch3 * , upper
      If you want to change value label names, there is a labelrename command (SJ) but you will need to loop over value label names as Clyde suggests.

      Best
      Daniel

      Comment


      • #4
        Originally posted by daniel klein View Post
        Cylde points in the right direction. His example code, however, assumes that all values are indeed labeled and that all labeled values do indeed appear in the dataset at hand. The labvalch3 command, part of labutil2 (from SSC) offers a canned solution that essentially implements Clydes code. The syntax is very similar to rename, in this case.

        Code:
        labvalch3 * , upper
        If you want to change value label names, there is a labelrename command (SJ) but you will need to loop over value label names as Clyde suggests.

        Best
        Daniel
        Thanks Daniel for the wonderful technology. It works fine except that it capitalises all the letters.

        Comment


        • #5
          Originally posted by Sonnen Blume View Post
          It works fine except that it capitalises all the letters.
          Clyde has pointed this out and already pointed to a solution; with labvalch3 this would be

          Code:
          labvalch3 * , strfcn(upper(substr(`"@"', 1, 1))+substr(`"@"', 2, .))
          If you want the first letter of every word capitalized, the syntax is simpler

          Code:
          labvalch3 * , proper
          By the way, we prefer full real names here on Statalist.

          Best
          Daniel

          Comment


          • #6
            Originally posted by daniel klein View Post

            Clyde has pointed this out and already pointed to a solution; with labvalch3 this would be

            Code:
            labvalch3 * , strfcn(upper(substr(`"@"', 1, 1))+substr(`"@"', 2, .))
            If you want the first letter of every word capitalized, the syntax is simpler

            Code:
            labvalch3 * , proper
            By the way, we prefer full real names here on Statalist.

            Best
            Daniel
            Thanks again Daniel. I was able to use
            Code:
            labvalch3 * , upper
            as
            Code:
            labvalch3 * , proper
            is showing error message.

            Code:
            . labvalch3 * , proper
            option proper not allowed

            Comment


            • #7
              Sorry, my bad; I have never released the version of labvalch3 that supports the proper option. You can still use the function as

              Code:
              labvalch3 * , strfcn(proper(`"@"'))
              Best
              Daniel

              Comment


              • #8
                Originally posted by daniel klein View Post
                Sorry, my bad; I have never released the version of labvalch3 that supports the proper option. You can still use the function as

                Code:
                labvalch3 * , strfcn(proper(`"@"'))
                Best
                Daniel
                This one worked! Thanks so much, it made my day.

                Comment

                Working...
                X