Announcement

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

  • splitvallabels (SSC) for variable labels

    Colleagues,

    I came across excellent and very handy solution to automatically split up value labels for multi-line graph (splitvallabels). Splitvallabels automatically breakes long value labels and passes results in macro to the graphing command. I would like to find similar solution for automatically breaking long variable labels, not value labels. I was wondering if anyone could suggest a similar program but working with variable labels and/or graph titles.
    Kind regards,
    Konrad
    Version: Stata/IC 13.1

  • #2
    Konrad, since you don't provide a link, to the program, it is hard to say. But I doubt it is doing anything more than inserting a break every so often, which shouldn't be difficult to do yourself. Perhaps it would be more challenging if it was following the grammar rules for correct breaking of words, does it do that? Best Sergiy

    Comment


    • #3
      Konrad did mention SSC in both title and tag, so the provenance of splitvallabels is clear.

      However, I agree with Sergiy's implication: doing this for variable labels is a simpler problem. Still, that leaves plenty of scope for getting it wrong. Below is a quick hack with very little testing.

      Code:
      *! 1.0.0 NJC 8jul2014 
      *! splitvallabels.ado v1.1.2 Nicholas Winter/Ben Jann 14aug2008
      program splitvarlabels, rclass
          version 8.2
          syntax varname, [ Length(int 15) local(string) NOBreak ]
      
          local label : var label `varlist'
          if `"`label'"' == "" local label "`varlist'"         
      
          local i = 1
          local part : piece `i' `length' of `"`label'"' , `nobreak'
          while `"`part'"' != "" {
              local newlabel `"`newlabel' `"`part'"' "'
              local i = `i' + 1
              local part : piece `i' `length' of `"`label'"' , `nobreak'
          }    
      
          return local relabel `"`newlabel'"'
      
          if "`local'" != "" {
              c_local `local' `"`newlabel'"' 
          }
      end

      Comment


      • #4
        Thank for your comments. It crossed my mind that neat solution could potentially involve breaking line on a specific sign. The end-user could then clone variable and introduce special sign (;/¬) to the text. The programme then could produce text strings formatted as "part 1" "part 2" deciding on the break according to the placement of the special character.
        Kind regards,
        Konrad
        Version: Stata/IC 13.1

        Comment


        • #5
          That's not just comments you have; it's code. Did you try it?

          Are you trying to make this easier for the end user? Insert emoticons of choice.
          Last edited by Nick Cox; 09 Jul 2014, 05:16.

          Comment


          • #6
            I tried to use in the following code but the produced chart is with no title.
            Code:
            sysuse auto
            clonevar pricelong = price
            label variable pricelong "The variable corresponds to price of each car available in this data"
            
            splitvarlabels pricelong
            hist pricelong, xtitle("`(rlabel)'")
            Click image for larger version

Name:	Graph.png
Views:	1
Size:	14.2 KB
ID:	119352
            Kind regards,
            Konrad
            Version: Stata/IC 13.1

            Comment


            • #7
              You used a name that was both incorrect and illegal, but Stata forgave you. Look at the results of -return list- to see what to use.

              Comment


              • #8
                Originally posted by Nick Cox View Post
                You used a name that was both incorrect and illegal, but Stata forgave you. Look at the results of -return list- to see what to use.
                Plus: Omit the double quotes in your xtitle() option. Then everything looks as expected:
                Click image for larger version

Name:	histogram.png
Views:	1
Size:	48.4 KB
ID:	119599

                Regards
                Bela

                Comment


                • #9
                  Now works like a charm
                  Kind regards,
                  Konrad
                  Version: Stata/IC 13.1

                  Comment


                  • #10
                    Dear all,

                    this might be a stupid question but I cannot get your code running.


                    I created an .ado file named splitvarlabels.ado in the ado/plus/s directory and copy pasted Nick Cox code.

                    However, I just get

                    unexpected end of file
                    (error occurred while loading splitvarlabels.ado)
                    r(612);



                    I tried Stata 11 SE and 14 IC




                    Comment


                    • #11
                      my guess is that you did not have a carriage return at the end of the last line of code - insert a blank line at the end and re-try

                      Comment


                      • #12
                        thanks a lot!

                        Comment


                        • #13
                          I made minor edits to NJC's excellent code to generalise splitvarlabels to a varlist

                          Code:
                          
                              *! 1.0.1 NJC & KN 8mar2023
                              *! 1.0.0 NJC 8jul2014 
                              *! splitvallabels.ado v1.1.2 Nicholas Winter/Ben Jann 14aug2008
                          
                          program splitvarlabels, rclass
                          
                              version 8.2
                              
                              syntax varlist, [Length(int 15)]
                              
                              local j = 1
                              
                              foreach var of varlist     `varlist' {    
                                  
                              local label : var label `var'
                              if `"`label'"' == "" local label "`var'"  
                              local newlabel = ""
                              local i = 1
                              
                              * Making chunks
                              
                              local part : piece `i' `length' of `"`label'"' 
                              
                              while `"`part'"' != "" {
                                  local newlabel `"`newlabel' `"`part'"' "'
                                  local i = `i' + 1
                                  local part : piece `i' `length' of `"`label'"' 
                              }    
                              
                              local all_labels    `" `all_labels' `j' `"`newlabel'"' "'
                              
                              local j = `j' + 1
                              
                              }
                              
                              return local relabel `"`all_labels'"'
                              
                          end
                          You can use yvaroptions(relabel(`r(relabel)')) with graph bar or varopts(relabel(`r(relabel)')) with statplot to use the program.

                          Comment

                          Working...
                          X