Announcement

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

  • A tale of two 'by's

    Kindly help me include by as an option in the following ado file. How may I add a % sign after the value of coefficient of variation please?
    Code:
    capture drop cvs
    program define cvs, rclass byable(recall) 
     version 13.2
     syntax varlist(numeric) [if] [in]
     display
     display as text "            Variable        N      mean        sd        cv"
     display as text "            -----------------------------------------------"
     marksample touse
     foreach var of local varlist {
     if "`if'" ~= "" | "`in'" ~= "" {
      quietly keep `if' `in'
     }
     quietly summarize `var' if `touse', detail
     local mn = r(mean)
     local s = r(sd)
     local n = r(N)
     local cv = round(`s'/`mn'*100, .1)
     display as result %20s "`var'" %9.0f `n'  %10.2f `mn'  %10.2f `s' %10.2f `cv'
     }
    end
    Output:
    Code:
    . sysuse auto
    (1978 Automobile Data)
    
    . cvs price-rep78
    
                Variable        N      mean        sd        cv
                -----------------------------------------------
                   price       69   6146.04   2912.44     47.40
                     mpg       69     21.29      5.87     27.60
                   rep78       69      3.41      0.99     29.10
    
    . bysort foreign: cvs price-rep78
    
    -------------------------------------------------------------
    -> foreign = Domestic
    
                Variable        N      mean        sd        cv
                -----------------------------------------------
                   price       48   6179.25   3188.97     51.60
                     mpg       48     19.54      4.75     24.30
                   rep78       48      3.02      0.84     27.70
    
    -------------------------------------------------------------
    -> foreign = Foreign
    
                Variable        N      mean        sd        cv
                -----------------------------------------------
                   price       21   6070.14   2220.98     36.60
                     mpg       21     25.29      6.31     25.00
                   rep78       21      4.29      0.72     16.70

  • #2
    Got the % sign after the cv value working already.

    Comment


    • #3
      I think the title should have been "including by as an option". Apologies for this unintentional non-compliance with the forum rules.

      Comment


      • #4
        We don't have any rules except the unwritten ones of not spamming and not being offensive. We have lots of requests and suggestions!

        Called with if or in your program changes the data by dropping the observations not selected. That strikes me as bad design even if you document it.

        Also, using the detail option of summarize is not necessary.

        Otherwise a by() option to the program would be implemented how? You might want to look at moments (SSC) which allows two syntaxes, one for one or more variables and the other for one or more groups of a variable.

        Comment


        • #5
          Oops! Will fix that.
          Thanks.

          Comment


          • #6
            Extremely helpful. I am not a religious person as such but Dr. Nicholas, Sir, God bless you.

            Comment


            • #7
              Dr. Nicholas,
              I suspect I have understood most of the logic in moments.ado after reading it for the last 4 or 5 days.
              May I adopt some of its logic regarding program flow and error checks for writing a program of my own please?
              Of course, I'll acknowledge it fully.
              Regards.

              Comment


              • #8
                Sure. You can copy whatever is useful so long as it's acknowledged.

                Comment


                • #9
                  Much obliged Sir.

                  Comment

                  Working...
                  X