Announcement

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

  • Extending the number of string characters when reshaping from wide to long and applying variable labels

    Hi Stata Team,

    Here is my command:


    foreach var of varlist Tech* {
    local Tech_nr: subinstr local var "Tech" ""
    label define Tech_label `Tech_nr' "`:variable label `var''", add
    }

    The issue is this. After reshaping to long, the labels have been cut because the string allowance is not long enough. How do I increase the number of string characters in the command above?

    Thanks in advance!

    Kind regards,
    Beth

  • #2
    The maximum allowable length of the name of a value label is 32 characters. That is set out in -help limits- and you cannot change it.

    So the question is why you want to do this in the first place. If you spell out your purpose, and provide examples of some of the Tech* data where it runs into this problem, perhaps somebody can find a workaround that will accomplish your purpose that uses some other mechanism.

    Comment


    • #3
      The context is wanting to save the variable labels to be used later as value labels after reshape. As some would be too long, that is frustrated.

      A data example would have helped here, but I think I can guess at the core of the problem. One alternative is to use a string variable instead, which is repetitive. Another is to use notes. Here my examples have numbering like 01 or 02 because I wanted to show code that could cope with such numbering, but the code should work for any numbering.

      Code:
      clear 
      input id Tech01 Tech02 
      42 123 789
      end 
      
      label var Tech01 "Too long to be acceptable as a value label, unfortunately"
      label var Tech02 "Concise"
      
      unab myvars : Tech*
      local mysuffixes : subinstr local myvars "Tech" "", all 
      
      foreach s of local mysuffixes { 
          local label`s' "`: var label Tech`s''"
          if strlen("`label`s''") > 32 local problem `problem' "Tech`s'"
          notes : `s' "`label`s''"
      }
      
      di "`problem'"
      
      reshape long Tech, i(id) j(which) string 
      
      gen what = "" 
      
      foreach s of local mysuffixes { 
          replace what = "`label`s''" if which == "`s'"
      }
      
      list 
      
      notes
      Code:
       
      . list 
      
           +-------------------------------------------------------------------------------+
           | id   which   Tech                                                        what |
           |-------------------------------------------------------------------------------|
        1. | 42      01    123   Too long to be acceptable as a value label, unfortunately |
        2. | 42      02    789                                                     Concise |
           +-------------------------------------------------------------------------------+
      
      . 
      . notes 
      
      _dta:
        1.  01 "Too long to be acceptable as a value label, unfortunately"
        2.  02 "Concise"
      Yet another alternative, perhaps best of all, is to edit the labels down to fit, as you still have the problem of how you would show the longer labels in tables or graphs. Note that my code yields a list of problematic variables as a side-effect.

      The code would not be robust to quotation marks embedded in the variable labels, but that could be attended to.

      Comment


      • #4
        I don't get the problem either. Variable labels are (still) limited to 80 characters. Value labels -- not value label names -- can be up to 32,000 bytes (at least around 8,000 characters). There is no way the label define command would "cut" variable labels when used as value labels. What might bite is the limit of 65,536 integer-to-text mappings that are allowed in a value label.


        Edit to make my post more actionable: please provide a data example illustrating the problem.
        Last edited by daniel klein; 21 Feb 2025, 03:59.

        Comment


        • #5
          daniel klein is correct -- and my answer #3 is confused and should be ignored. Sorry. I am going to hold on suggesting better code until the real problem is explained, and if someone does that first, well and good.

          Comment


          • #6
            Good morning,

            I apologize in advance for the lack the lack of clarity. That was not my intention. From the discussion, it looks like Nick Cox describes it best. However, I will provide a data example to illustrate the output I get after running the code I provided in the first post.

            Using dataex, here is an example. Each ChemicalOperator has a label. It is a task that a Chemical Tech performs. Below you do not see the labels. I add those.

            Code:
            * Example generated by -dataex-. For more info, type help dataex
            clear
            input byte(Respondent ChemicalOperator1 ChemicalOperator2 ChemicalOperator3 ChemicalOperator4)
            1 4 4 3 3
            2 4 4 4 3
            end
            Here are the labels:

            label variable ChemicalOperator1 "Move control settings to make necessary adjustments on equipment units affecting speeds of chemical reactions, quality, or yields."
            label variable ChemicalOperator2 " Monitor recording instruments, flowmeters, panel lights, or other indicators and listen for warning signals to verify conformity of process conditions. "
            label variable ChemicalOperator3 " Control or operate chemical processes or systems of machines, using panelboards, control boards, or semi-automatic equipment."
            label variable ChemicalOperator4 " Record operating data, such as process conditions, test results, or instrument readings."

            I realized that notes might make sense, but I don't know how to adjust the command below.

            foreach var of varlist ChemicalOperator* {
            local ChemicalOperator_nr: subinstr local var "ChemicalOperator" ""
            label define ChemicalOperator_label `ChemicalOperator_nr' "`:variable label `var''", add
            }

            reshape long ChemicalOperator, i(Respondent) j(Task)
            label values Task ChemicalOperator_label
            rename ChemicalOperator LikenessRating

            Below is the output:
            Code:
            * Example generated by -dataex-. For more info, type help dataex
            clear
            input byte(Respondent Task ChemicalOperator)
            1  1 4
            1  2 4
            1  3 3
            1  4 3
            1  5 3
            1  6 3
            1  7 3
            1  8 3
            1  9 1
            1 10 1
            1 11 2
            1 12 2
            1 13 2
            1 14 2
            1 15 1
            1 16 2
            2  1 4
            2  2 4
            2  3 4
            2  4 3
            2  5 5
            2  6 3
            2  7 5
            2  8 4
            2  9 3
            2 10 3
            2 11 5
            2 12 5
            2 13 4
            2 14 4
            2 15 2
            2 16 4
            end
            label values Task ChemicalOperator_label
            label def ChemicalOperator_label 1 "Move control settings to make necessary adjustments on equipment units af", modify
            label def ChemicalOperator_label 2 "Monitor recording instruments, flowmeters, panel lights, or other indicat", modify
            label def ChemicalOperator_label 3 "Control or operate chemical processes or systems of machines, using panel", modify
            label def ChemicalOperator_label 4 "Record operating data, such as process conditions, test results, or instr", modify
            label def ChemicalOperator_label 5 "Confer with technical and supervisory personnel to report or resolve cond", modify
            label def ChemicalOperator_label 6 "Draw samples of products and conduct quality control tests to monitor pro", modify
            label def ChemicalOperator_label 7 "Regulate or shut down equipment during emergency situations, as directed", modify
            label def ChemicalOperator_label 8 "Start pumps to wash and rinse reactor vessels, to exhaust gases or vapors", modify
            label def ChemicalOperator_label 9 "Interpret chemical reactions visible through sight glasses or on televisi", modify
            label def ChemicalOperator_label 10 "Patrol work areas to ensure that solutions in tanks or troughs are not in", modify
            label def ChemicalOperator_label 11 "Notify maintenance, stationary engineering, or other auxiliary personnel", modify
            label def ChemicalOperator_label 12 "Inspect operating units, such as towers, soap-spray storage tanks, scrubb", modify
            label def ChemicalOperator_label 13 "Direct workers engaged in operating machinery that regulates the flow of", modify
            label def ChemicalOperator_label 14 "Turn valves to regulate flow of products or byproducts through agitator t", modify
            label def ChemicalOperator_label 15 "Gauge tank levels, using calibrated rods.", modify
            label def ChemicalOperator_label 16 "Repair or replace damaged equipment.", modify
            To summarize, I'm attaching long labels/notes to my variables. After reshaping, they are truncated.

            Comment


            • #7
              OK. I clearly misunderstood what you were trying to do in #1. The place where you are hitting the truncation of the labels is not in your -foreach- loop, nor in your -reshape- command. The labels are already truncated by your -label variable- commands. Variable labels are restricted to 80 characters (see -help limits-) and you cannot change that.

              Instead, you can store those "labels" in -characteristics- instead, and then you can use them to create value labels: the content of value labels is capped at 32,000 characters, and you are no where near encroaching on that limit.

              Code:
              * Example generated by -dataex-. For more info, type help dataex
              clear
              input byte(Respondent ChemicalOperator1 ChemicalOperator2 ChemicalOperator3 ChemicalOperator4)
              1 4 4 3 3
              2 4 4 4 3
              end
              
              
              char ChemicalOperator1[lbl] "Move control settings to make necessary adjustments on equipment units affecting speeds of chemical reactions, quality, or yields."
              char ChemicalOperator2[lbl] " Monitor recording instruments, flowmeters, panel lights, or other indicators and listen for warning signals to verify conformity of process conditions. "
              char ChemicalOperator3[lbl] " Control or operate chemical processes or systems of machines, using panelboards, control boards, or semi-automatic equipment."
              char ChemicalOperator4[lbl] " Record operating data, such as process conditions, test results, or instrument readings."
              
              foreach var of varlist ChemicalOperator* {
                  local ChemicalOperator_nr: subinstr local var "ChemicalOperator" ""
                  label define ChemicalOperator_label `ChemicalOperator_nr' "`:char `var'[lbl]'", add
              }
              
              reshape long ChemicalOperator, i(Respondent) j(Task)
              label values Task ChemicalOperator_label
              rename ChemicalOperator LikenessRating

              Comment


              • #8
                Originally posted by Elizabeth Anne Unger View Post
                To summarize, I'm attaching long labels/notes to my variables. After reshaping, they are truncated.
                No; this has nothing to do with reshape. The labels are truncated to 80 characters the moment you feed them to label variable. And, Stata tells you about that:
                Code:
                . * Example generated by -dataex-. For more info, type help dataex
                . clear
                
                . input byte(Respondent ChemicalOperator1 ChemicalOperator2 ChemicalOperator3 ChemicalOperator4)
                
                     Respon~t  Chemic~1  Chemic~2  Chemic~3  Chemic~4
                  1. 1 4 4 3 3
                  2. 2 4 4 4 3
                  3. end
                
                .
                . label variable ChemicalOperator1 "Move control settings to make necessary adjustments on equipment units affecting speeds of chemical reactions, quality, or yields."
                note: label truncated to 80 characters
                
                . label variable ChemicalOperator2 "Monitor recording instruments, flowmeters, panel lights, or other indicators and listen for warning signals to verify conformity of process conditions. "
                note: label truncated to 80 characters
                
                . label variable ChemicalOperator3 "Control or operate chemical processes or systems of machines, using panelboards, control boards, or semi-automatic equipment."
                note: label truncated to 80 characters
                
                . label variable ChemicalOperator4 "Record operating data, such as process conditions, test results, or instrument readings."
                note: label truncated to 80 characters
                
                .
                .
                . desc
                
                Contains data
                 Observations:             2                  
                    Variables:             5                  
                -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
                Variable      Storage   Display    Value
                    name         type    format    label      Variable label
                -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
                Respondent      byte    %8.0g                
                ChemicalOpera~1 byte    %8.0g                 Move control settings to make necessary adjustments on equipment units affecting
                ChemicalOpera~2 byte    %8.0g                 Monitor recording instruments, flowmeters, panel lights, or other indicators and
                ChemicalOpera~3 byte    %8.0g                 Control or operate chemical processes or systems of machines, using panelboards,
                ChemicalOpera~4 byte    %8.0g                 Record operating data, such as process conditions, test results, or instrument r
                -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
                Sorted by:
                     Note: Dataset has changed since last saved.

                Edit: Crossed with Clyde Schechter's response in #7. While characteristics are generally useful, they add an unnecessary step for this problem. Why not define the desired value label directly?
                Last edited by daniel klein; 21 Feb 2025, 12:04.

                Comment


                • #9
                  We are back with questions that can be answered. I will just add that I've never seen the point of really long value labels, as how are you going to display them well on graphs or in tables, and that aside what use are they otherwise? So given that I note Clyde Schechter on characteristics because if the intent is to store metadata with data, they are a good way to do it.

                  Comment


                  • #10
                    Hi Nick,

                    I typically agree with you. It's a project for workforce development that involves mapping learning outcomes to tasks. In this case, I need to be able to read it in its entirety. I'm going to run Clyde's script to see the output. It will be super exciting if it works.

                    Have a wonderful day and thanks again for the help.

                    Beth

                    Comment

                    Working...
                    X