Announcement

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

  • Using results output from table or tabulate commands for subsequent calculations or commands


    Regarding table or tabulate outputs that print to the results window--

    This may involve the -command- option embedded within the table feature, but I do not know how to use properly. Examples I've seen only describe adding regressions using this feature. Or perhaps there is a different way to approach:

    How does one take data populated from a table, visible in the results window, and apply an operation to it? Is that data stored or storable somewhere for use?

    Say I have a table that I generated using command
    Code:
    table (widget) (broken_status)
    Widget Broken (1) Not Broken (0) Total Annual Cost
    W1 200.00 800.00 1000.00
    W2 1000.00 1000.00 2000.00
    W3 300.00 200.00 500.00
    W4 50.00 50.00 100.0

    How could I apply simple arithmetic to the OUTPUT of the table to create an additional table column that shows "cost per month of broken widgets", which would == broken/12:

    Widget Broken (1) Not Broken (0) Total Annual Cost Monthly Broken Cost
    W1 200.00 800.00 1000.00 16.666
    W2 1000.00 1000.00 2000.00 83.333
    W3 300.00 200.00 500.00 25
    W4 50.00 50.00 100.0 4.166

    Please advise and thanks!


  • #2
    Greetings Leonard, first I'd advise you follow the documentation advise provided here, particularly with regards to a reproducible example, which would increase the number of responses. It would seem your query would be solved by generating the variable 'Monthly Broken Cost' prior to using the table command. Unless I am misunderstanding the nature of your inquiry?

    Comment


    • #3
      I do not think that you can work with the output of -table- because it does not leave anything behind. I always thought that this is a bad feature of Stata tabulations, that they do not return the tabulation in a matrix.

      Now whether you can trick the advanced new -table- to do what you want, is an interesting question. The -command- option/specification seems the way to go, but here I hit on another thing I have always disliked about Stata - the command -display- does not leave anything behind either, so it is not clear to me how we can do simple operations like you want to do with the -command- option/specification.

      Comment


      • #4
        Originally posted by Eric Makela View Post
        Greetings Leonard, first I'd advise you follow the documentation advise provided here, particularly with regards to a reproducible example, which would increase the number of responses. It would seem your query would be solved by generating the variable 'Monthly Broken Cost' prior to using the table command. Unless I am misunderstanding the nature of your inquiry?
        Thank you Eric Makela. I do recognize the importance of offering real data examples.
        I am providing a simpler table concept here (using smaller numbers and scale) with hopes of seeing if there are thoughts. Say I have factory site and broken_status of the machines.

        Code:
        clear
        input float(site broken_status)
        1 1
        1 1
        1 0
        1 0
        1 0
        1 1
        2 0
        2 0
        2 1
        2 1
        3 1
        3 0
        3 0
        3 0
        4 0
        4 1
        4 0
        4 1
        4 0
        end
        If I use the command
        Code:
        table site broken_status
        I'll get an output that shows me count of brokens (1) and non-brokens (2) by site.
        Let's say these are monthly counts (instead of annual that I mentioned before).
        I'd like to quickly use the table output from the results window to write an equation that gives me the approximated weekly occurence.
        So I would take broken_status from the "1" column" / 4, and perhaps generate a new column of my table called "weekly count" that yields the result by site.

        Joro Kolev , you mentioned in #3 you do not think this is possible, which I fear may be the case?

        Appreciate everyone's help once again if you could advise, or if I can manipulate the original dataset to do this proximal to the table command itself.

        Comment


        • #5
          But without knowing the number of days in the month, it's difficult to know whether to scale the monthly counts to weekly counts by 7/31, 7/30, 7/28, or 7/29. Or perhaps scale them all by 7/(365/12) (ignoring the occasional leap day).

          Comment


          • #6
            Greetings Leonard. If you want an Excel file that displays the number of broken widgets per month for each site, here's a possibility:
            Code:
            clear
            input float(site broken_status)
            1 1
            1 1
            1 0
            1 0
            1 0
            1 1
            2 0
            2 0
            2 1
            2 1
            3 1
            3 0
            3 0
            3 0
            4 0
            4 1
            4 0
            4 1
            4 0
            end
            gen broken=(broken_status==1)
            collapse (sum) broken, by(site)
            gen permonth=broken/(365/12)
            export excel using "broken_widgets", firstrow(variables)
            I assume you'd probably do a bit better with the monthly calculation (substituting the (365/12)) above, but you get the idea. Collapse is a byable command that is quite versatile for creating tables. If this isn't workable, you might also investigate egen, also byable, which might be helpful if you also included some variable with the number of days.

            Comment

            Working...
            X