Announcement

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

  • Extracting range of a variable as 2 values instead of 1

    Hi,

    I want to extract variables range as 2 values, i.e. if variables range is 1-5 I want that the table will contain the text "1-5".

    MWE:

    Code:
    clear
    sysuse auto
    tabstat mpg, statistics(range)
    su mpg
    The attached screenshot shows that the range of the variable "mpg" is 29, even though its values range between 12-41.

    Click image for larger version

Name:	Screen Shot 2020-06-10 at 18.13.34.png
Views:	1
Size:	87.7 KB
ID:	1558033
    Stata/MP 15.1

  • #2
    Perhaps the-min- and -max- choices of the -statistics- option of -tabstat- (see -help tabstat-) would suit your purposes. If not, please explain further.

    Comment


    • #3
      I think I didn't get it quite clear. If you want to create a table, you may look at some user-written programs, such as -tabout. If you just want to create a variable with the ranges, I believe this can calculated in a loop, then a new variable would have categories of these ranges. But mixing a string ("1-5") with the remaining ranges would make it difficult to any further processing.
      Best regards,

      Marcos

      Comment


      • #4
        Thanks Mike. I’m aware of the -tabstat- -min- -max- options. However, I’m want to be able to have a single table column which specifies each variables range (for instance [12-41]). The tabstat command provides 2 separate column tables.
        Stata/MP 15.1

        Comment


        • #5
          I am using esttab and the like. If I’ll be able to find a way to extract the variable range I will be able to do the rest by myself. I know how do the output export, my issue is that I don’t know where to find a command which produces a range in the format I’m looking for.
          Stata/MP 15.1

          Comment


          • #6
            My guess is that you need to write the code yourself. This is how all community-contributed commands, in response to an itch that official Stata evidently does not support.

            Some small tricks below.

            Code:
            sysuse auto, clear
            
            foreach v in mean min max { 
                gen `v' = . 
            }
            
            gen which = _n 
            
            local i = 0
            
            quietly foreach v of var price-foreign {
                su `v', meanonly
                local ++i
                label def which `i' "`v'", modify 
                replace mean = r(mean) in `i'
                replace max = r(max) in `i'
                replace min = r(min) in `i'
            }
            
            label val which which 
            
            gen range = "[" + strofreal(min) + "-" + strofreal(max) + "]"
            
            format mean %8.3g 
            
            label var which "`=uchar(160)'"
            
            tabdisp which if mean < ., c(mean range)
            
            -----------------------------------------
                         |         mean         range
            -------------+---------------------------
                   price |         6165  [3291-15906]
                     mpg |         21.3       [12-41]
                   rep78 |         3.41         [1-5]
                headroom |         2.99       [1.5-5]
                   trunk |         13.8        [5-23]
                  weight |         3019   [1760-4840]
                  length |          188     [142-233]
                    turn |         39.6       [31-51]
            displacement |          197      [79-425]
              gear_ratio |         3.01   [2.19-3.89]
                 foreign |         .297         [0-1]
            -----------------------------------------

            Comment


            • #7
              Dear Nick! Thanks a bunch. This is perfect.
              Stata/MP 15.1

              Comment


              • #8
                @nick - even though the solution is perfect I was not able to export it in any way from Stata. I know you don't use -esttab- and the like.
                Is there a way to export -tabdisp- as a table? I searched everywhere for a solution but could not find one.

                I don't mind if it is exported as .txt -

                I found this reply:

                Originally posted by Nick Cox View Post
                I don't know of any title options. I just use display.

                You don't need anything special to save it as a .txt file: just use log.
                https://www.statalist.org/forums/for...orting-tabdisp

                but couldn't figure out what you meant by that.

                Stata/MP 15.1

                Comment


                • #9
                  Copy and paste, or use the log command.

                  But, but, but: what is shown by tabdisp is just data you have in your dataset! So you can

                  Code:
                  list which  mean range if mean < , 
                  preserve 
                  keep if mean < . 
                  keep which mean range 
                  * export where you want [no idea] how you want [no idea either]
                  restore

                  Comment


                  • #10
                    Thanks Nick!

                    I tried exporting it as a dataset to a matrix (and from there I think I know what to do) but it Stata won't let me convert to a matrix because it contains strings.

                    I can't work with copy and paste, since I repeat these commands numerous times before I publish.

                    Thanks anyway for the effort.
                    Stata/MP 15.1

                    Comment


                    • #11
                      I didn't recommend export as a matrix, and as you say that won't work. My thought as said was export as data.

                      What you want is programmable, but the program depends on exactly what you want, and in this case I am not volunteering.

                      Pleased to get good marks for effort.

                      Comment

                      Working...
                      X