Announcement

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

  • How to reduce empty space in the cell

    Hi, I try to reduce or remove empty space in each cell of string variables because it really hinders me from eyeballing the dataset, using the following code: https://www.statalist.org/forums/for...tching-purpose.

    However, it turns out that most cells still take too much space before its values.

    Code:
    local varlist firmid ksic1 ksic2 ksic3        
    foreach v of varlist `r(varlist)'{
       local vformat: format `v'
       local vformat: subinstr local format "%" "%-"
       format `vformat' `v'
    }

    I got:

    Code:
     variable name  display format
      -----------------------------
      firmid         %9s
      -----------------------------
    
      variable name  display format
      -----------------------------
      ksic1          %58s
      -----------------------------
    
      variable name  display format
      -----------------------------
      ksic2          %66s
      -----------------------------
    
      variable name  display format
      -----------------------------
      ksic3          %87s
      -----------------------------
    Could there be any possible solution? Thank you.
    Last edited by Chul-Kyoo Jung; 01 Jan 2022, 09:29.

  • #2
    Code:
    local varlist firmid ksic1 ksic2 ksic3        
    foreach v of varlist `varlist' {
       local vformat: format `v'
       local vformat: subinstr local vformat "%" "%-"
       format `vformat' `v'
    }
    Added in edit: The code given in the post you referenced was
    Code:
    ds, has(type string)
    foreach v of varlist `r(varlist)' {
        local vformat: format `v'
        local vformat: subinstr local vformat "%" "%-"
        format `vformat' `v'
    }
    Note that r(varlist) was created as a list of all string variables in your dataset by the ds command that this code starts with. Rather than change all string variables to be left aligned, which is what this code did, you chose to give a list of four variables. So then ds has not been run and r(varlist) has not been created, and you need to change the foreach command appropriately. On the subinstr you did not correctly copy the original code.
    Last edited by William Lisowski; 01 Jan 2022, 10:39.

    Comment


    • #3
      William Lisowski Thanks so much. Can I have just one more question?
      After running the code, I still got long unwanted trailing space to the right in each cell.
      Is there any way to remove those spaces?
      Last edited by Chul-Kyoo Jung; 01 Jan 2022, 11:31.

      Comment


      • #4
        When "eyeballing the dataset" do you mean you are looking at it in interactively in Stata's Data Viewer or Data Editor window? In that case, just resize the width of column containing the variable using the column by dragging the dividing line following the variable name at the top of the column to the left, just as you would in Excel. You don't even need to change it to be left justified for that to work.

        If not, tell us precisely how you are "eyeballing the dataset".

        Comment


        • #5
          William Lisowski , I just edited the post before I saw your reply. Yes, I have such a long list of variables, so I just want to view as many variables as I could see when I look at Data Viewer. I also just tried to drag the column in both Editor and Viewer, but the width won't change. Do I miss anything here in dragging way? Thank you.
          Last edited by Chul-Kyoo Jung; 01 Jan 2022, 11:55.

          Comment


          • #6
            I really don't know what to say. Columns resize just like they do in Excel, by clicking in the row of variable names and moving the cursor until the resize arrows show, and then clicking and dragging right or left. I have attached a screenshot of what you should expect to see. There is a menu item to reset selected column widths, but the only way to change them is in the Editor or Viewer.

            Click image for larger version

Name:	resize.png
Views:	1
Size:	182.3 KB
ID:	1643260

            Comment


            • #7
              Maybe the following helps, setting the width to the maximum lenght of the str var
              Code:
              ds, has(type string)
              
              foreach v of varlist `r(varlist)' {
                
                  replace `v' = ustrtrim(`v') // MAYBE will possibly change data
                  mata : st_local("maxlen", strofreal(max(strlen(st_sdata(.,"`v'"))),"%10.0f"))
                  format %-`maxlen's `v'  // set display width to max of varlength
              }
              Last edited by Bjarte Aagnes; 01 Jan 2022, 12:54.

              Comment


              • #8
                William Lisowski and Bjarte Aagnes Thank you again both of you. Strangely, my Stata (ver.15) doesn't allow me to drag the column (or I am still missing something); otherwise, it would be fantastic way to do so. Bjarte's suggestion also works great for me. Could you specify what you mean by "MAYBE will possibly chane the data"? Still can I apply your code without taking a risk?

                Comment


                • #9
                  ustrtrim() removes leading and trailing whitespace characters and blanks. If you want to keep leading/trailing space chars skip the trimming.

                  Comment


                  • #10
                    The ability to resize columns by dragging was added in Stata 16.

                    Please keep in mind for future topics that the Statalist FAQ asks that you state the version of Stata you are using if it is not the current version.

                    If your string variables are substantially larger than they need to be, given the values they contain, you might find the compress command useful.
                    Code:
                    . generate str60 longtext = "this is 20 characters"
                    
                    . describe longtext
                    
                    Variable      Storage   Display    Value
                        name         type    format    label      Variable label
                    ------------------------------------------------------------------------------------------------
                    longtext        str60   %60s                  
                    
                    . compress longtext
                      variable longtext was str60 now str21
                      (39 bytes saved)
                    
                    . describe longtext
                    
                    Variable      Storage   Display    Value
                        name         type    format    label      Variable label
                    ------------------------------------------------------------------------------------------------
                    longtext        str21   %21s

                    Comment


                    • #11
                      William Lisowski , Great, I will make sure of that. Thanks for all your comments and helps.

                      Comment

                      Working...
                      X