Announcement

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

  • How to tab only if the percent column value is higher than a pre-specified number?

    Hi ALL!

    so using the tab command below I get the table output:

    tab code if dummyvariable==1, sort

    Code | Freq. | Percent | Cum.
    ------------+-----------------------------------
    P | 240 | 31.99 | 31.99
    D | 60 | 9.79 | 41.78
    R | 57 | 7.29 | 49.08
    Q | 53 | 6.86 | 55.93
    N | 45 | 5.98 | 61.92

    The table output I get is of course much longer

    How can I tabulate so that I only get the observations with percent higher than lets say 6%, i.e. so that only P,D,R,Q are shown?

    Many thanks!!
    Last edited by Lalo Salamanca; 15 Jun 2022, 07:40.

  • #2
    -tab- is not capable of such a request, but you can do this with a bit of dataset manipulation.

    Code:
    clear
    version 17
    set seed 17
    set obs 26
    qui gen letter = ""
    forval i = 1/26 {
      qui replace letter = "`: word `i' of `c(alpha)''" if _n==`i'
    }
    gen int n = runiformint(1, 100)
    expand n
    drop n
    
    // Start here
    preserve
    contract letter
    rename _freq freq
    gsort -freq
    gen cusum = sum(freq)
    gen percent = freq / cusum[_N] * 100
    format percent %9.2f
    list letter freq percent if percent >= 5
    restore
    Selected output

    Code:
    . list letter freq percent if percent >= 5
    
         +-------------------------+
         | letter   freq   percent |
         |-------------------------|
      1. |      o     95      6.92 |
      2. |      f     94      6.85 |
      3. |      c     90      6.55 |
      4. |      a     90      6.55 |
      5. |      d     86      6.26 |
         |-------------------------|
      6. |      w     82      5.97 |
         +-------------------------+

    Comment


    • #3
      Yet one more method, using Nick Cox's -groups- (SSC).

      Code:
      clear
      set seed 17
      set obs 26
      qui gen letter = ""
      forval i = 1/26 {
        qui replace letter = "`: word `i' of `c(alpha)''" if _n==`i'
      }
      gen int n = runiformint(1, 100)
      
      groups letter [fw=n], order(h) show(freq percent) select(percent >= 5)

      Comment


      • #4
        Thanks for the mention. groups is available from SSC, but I will flag that it is documented and distributed through the Stata Journal. I chose a common English word as command name, so many searches will yield many false positives: a tip is to

        Code:
        . search st0496, entry
        
        Search of official help files, FAQs, Examples, and Stata Journals
        
        SJ-18-1 st0496_1  . . . . . . . . . . . . . . . . . Software update for groups
                (help groups if installed)  . . . . . . . . . . . . . . . .  N. J. Cox
                Q1/18   SJ 18(1):291
                groups exited with an error message if weights were specified;
                this has been corrected
        
        SJ-17-3 st0496  . . . . .  Speaking Stata: Tables as lists: The groups command
                (help groups if installed)  . . . . . . . . . . . . . . . .  N. J. Cox
                Q3/17   SJ 17(3):760--773
                presents command for listing group frequencies and percents and
                cumulations thereof; for various subsetting and ordering by
                frequencies, percents, and so on; for reordering of columns;
                and for saving tabulated data to new datasets
        Code:
        
        



        to go to resources.

        https://www.statalist.org/forums/for...updated-on-ssc gives a quick overview.
        Last edited by Nick Cox; 16 Jun 2022, 00:51.

        Comment


        • #5
          Thanks Gents

          Comment

          Working...
          X