Announcement

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

  • How to Left-align data: Using string values that contain spaces

    Hello there,

    I am using a dataset that contains string values with spaces after the values eg.:
    "90041"
    "9004 "
    "900 "
    "0032 "

    The creators of the dataset advise that it be used only if the data is left-aligned. This takes into account the spaces after the numbers. However, in the Stata browser, they are right-aligned (or at least they appear so). Is there a way of making data values left-aligned? Or does this apply to Stata?

    If so, please share the command.

    Thanks.

  • #2
    sounds like yet *another* user of the ICD-9. Regardless, if they are string, don't worry about alignment in Stata. But *do* keep them as strings, at least at the first stages, since the leading 0s do matter.

    Comment


    • #3
      Oh great! Thanks.

      Comment


      • #4
        And, if you would like them to appear to be left aligned you can do that by changing the format from %xs to %-xs (whatever x is).

        Code:
        local fmt: format myvar
        local fmt: subinstr local fmt "%" "%-"
        format myvar `fmt'
        Remember: all this does is change the display format so they will appear to be left aligned. It doesn't actually change the content of the variable in any way, and it has no consequences at all for any analysis you do. But it might be better aesthetically and give you some peace of mind in light of the instructions you were given.

        Comment


        • #5
          I'm surprised that the subject of the alignment of strings does not come up more often. I have always found that the default of right-justified for strings is not the best choice when browsing or listing data. Clyde's solution for changing the format is spot on but quite tedious. So a long time ago I wrote a little program to automate this task. I always wanted to upgrade it to include variables that use value labels. While I was at it, I added the option of changing the alignment of all variables. Here's my current version

          Code:
          *! version 2.0.0, 07sep2014, Robert Picard, [email protected]      
          program define leftalign
          
              version 9.2
              
              syntax [varlist] , [Label All]
              
              foreach v of varlist `varlist' {
              
                  local f : format `v'
                  
                  local ff = regexr("`f'", "%~?\-?","")
                  
                  if regexm("`f'", "%.+s") local cmd cap format %-`ff' `v'
                  
                  if "`: value label `v''" != "" & "`label'" != "" ///
                      local cmd cap format %-`ff' `v'
                  
                  if "`all'" != "" local cmd cap format %-`ff' `v'
                  
                  `cmd'
                  
                  local ff : format `v'
                  if "`ff'" != "`f'" local vlist `vlist' `v'
          
              }
              
              if "`vlist'" != "" des `vlist'
                  
          end
          Here are some examples

          Code:
          . sysuse auto, clear
          (1978 Automobile Data)
          
          . 
          . * make is already left-justified so nothing to change
          . leftalign
          
          . 
          . * the default format for strings is right-justified
          . gen s = make
          
          . 
          . * the default format is right-justified for labels also
          . encode make, gen(sl)
          
          . 
          . list make s sl foreign in 50/54
          
               +----------------------------------------------------------+
               | make                        s              sl    foreign |
               |----------------------------------------------------------|
           50. | Pont. Le Mans   Pont. Le Mans   Pont. Le Mans   Domestic |
           51. | Pont. Phoenix   Pont. Phoenix   Pont. Phoenix   Domestic |
           52. | Pont. Sunbird   Pont. Sunbird   Pont. Sunbird   Domestic |
           53. | Audi 5000           Audi 5000       Audi 5000    Foreign |
           54. | Audi Fox             Audi Fox        Audi Fox    Foreign |
               +----------------------------------------------------------+
          
          . 
          . * leftalign only targets string variables
          . leftalign
          
                        storage  display     value
          variable name   type   format      label      variable label
          -------------------------------------------------------------------------------
          s               str17  %-17s                  
          
          . 
          . * unless the label option is used
          . leftalign, label
          
                        storage  display     value
          variable name   type   format      label      variable label
          -------------------------------------------------------------------------------
          foreign         byte   %-8.0g      origin     Car type
          sl              long   %-17.0g     sl         Make and Model
          
          . 
          . list make s sl foreign in 50/54
          
               +----------------------------------------------------------+
               | make            s               sl              foreign  |
               |----------------------------------------------------------|
           50. | Pont. Le Mans   Pont. Le Mans   Pont. Le Mans   Domestic |
           51. | Pont. Phoenix   Pont. Phoenix   Pont. Phoenix   Domestic |
           52. | Pont. Sunbird   Pont. Sunbird   Pont. Sunbird   Domestic |
           53. | Audi 5000       Audi 5000       Audi 5000       Foreign  |
           54. | Audi Fox        Audi Fox        Audi Fox        Foreign  |
               +----------------------------------------------------------+
          
          . 
          . * to left align a numeric variable
          . leftalign price, all
          
                        storage  display     value
          variable name   type   format      label      variable label
          -------------------------------------------------------------------------------
          price           int    %-8.0gc                Price
          
          . list make price in 25/29
          
               +----------------------------+
               | make                price  |
               |----------------------------|
           25. | Ford Mustang        4,187  |
           26. | Linc. Continental   11,497 |
           27. | Linc. Mark V        13,594 |
           28. | Linc. Versailles    13,466 |
           29. | Merc. Bobcat        3,829  |
               +----------------------------+
          Comments and suggestions are welcome. I'll whip up a help file and probably send it to SSC.
          Last edited by Robert Picard; 08 Sep 2014, 18:08.

          Comment


          • #6
            Thanks both. This is quite helpful.

            Comment


            • #7
              This must have been discussed in 1997, when I posted ljs to SSC. That program is well and truly superseded by Robert's leftalign. I think it predates left-justified formats.

              Comment


              • #8
                I'm sorry to bump an old thread, but this seems like the moist appropriate place for this:
                1. leftalign is exactly what I was looking for, thanks!

                2. I wonder why stata doesn't allow to set the default aligning for string variables, so that when they're created they could be left aligned.

                3. I wonder if anyone finds right-aligned strings (if the strings are in English or any other LTR language) more comfortable to work with (browsing data, etc.) than left-aligned... which leaves me baffled as to why the default is right-alignment...

                Comment


                • #9
                  Ariel, seems like your second point could be converted to a suggestion on the Wishlist for Stata 15 thread.

                  Comment


                  • #10
                    Responded to Ariel Karlinsky here before noticing that he followed Robert Picard's advice to post to the Wishlist for Stata 15 thread. I have cut my response from here and pasted it there, specifically at http://www.statalist.org/forums/foru...stata-15/page6.
                    Last edited by Clyde Schechter; 15 May 2016, 11:03.

                    Comment


                    • #11
                      Thank you for writing this program, I find it very useful and wish I had found it sooner.

                      Comment

                      Working...
                      X