Announcement

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

  • Setting column width for all variables

    I wish to set column width to all variables in my dataset
    MWE:
    Code:
    foreach v of varlist * {
        format %20s `v'
      }
    Since some are numeric I get this error message:
    Code:
    numeric %fmt required for numeric variables
    Any idea on how can I set the column width for all variables in one command?





    Stata/MP 15.1

  • #2
    Column width is usually a matter for a display directive or a tabulation command. Display format is mostly a different thing.

    Note that with numeric variables you could specify a numeric format such as %20.3f but it's a matter of context whether that affects what you see.

    Comment


    • #3
      Nick's answer in post #2 suggests that what you ask for may not give the results you seek. With that said, the following example demonstrates code building on the ds command that seems to do what you ask for. You'll have to see whether that gives the results you seek in your application.
      Code:
      sysuse auto, clear
      ds, has(type numeric)
      local nums `r(varlist)'
      ds, has(type string)
      local strs `r(varlist)'
      format %20s `strs'
      format %20.0g `nums'
      describe *
      Code:
      . describe *
      
      Variable      Storage   Display    Value
          name         type    format    label      Variable label
      ------------------------------------------------------------------------------------------------
      make            str18   %20s                  Make and model
      price           int     %20.0g                Price
      mpg             int     %20.0g                Mileage (mpg)
      rep78           int     %20.0g                Repair record 1978
      headroom        float   %20.0g                Headroom (in.)
      trunk           int     %20.0g                Trunk space (cu. ft.)
      weight          int     %20.0g                Weight (lbs.)
      length          int     %20.0g                Length (in.)
      turn            int     %20.0g                Turn circle (ft.)
      displacement    int     %20.0g                Displacement (cu. in.)
      gear_ratio      float   %20.0g                Gear ratio
      foreign         byte    %20.0g     origin     Car origin
      Last edited by William Lisowski; 15 Feb 2022, 09:19.

      Comment

      Working...
      X