Announcement

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

  • Is there a way to access the values generated by the 'table' command?

    Is there a way to access the values generated by the 'table' command and store it as a scalar for use in calculations.

    For instance I want to use the means generated by this two-way table command which generates the mean of a third value (ie, apple_price):

    Code:
    table country state, c(mean apple_price n apple_price) format(%14.2fc)
    A way to do this by creating a new variable is by calling:
    Code:
    bysort country state: egen groupmean_price= mean(apple_price)
    ..but then again, this requires creating a new variable.
    Last edited by Ernestina delPiero; 15 Jul 2019, 09:31.

  • #2
    Unfortunately, the -table- command does not leave its results behind anywhere in memory. You have already uncovered a workaround using -egen, mean()-. To avoid creating new variables you could also do this:

    Code:
    levelsof country, local(countries)
    foreach c of local countries {
        levelsof state if country == "`c'", local(states)
        foreach s of local states {
            summ apple_price if country == "`c'" & state == "`s'", meanonly
            scalar mean_`c'_`s' = r(mean)
            scalar n_`c'_`x' = r(N)
        }
    }
    Note: Assumes country and state are string variables. If not, modify accordingly.

    Comment


    • #3
      Another workaround is using -xtable- command with the -noput- option, which will store the output in r(xtable) (see: https://www.statalist.org/forums/for...utput-to-excel). You can then access the values with matrix indices:

      Code:
      xtable country state, c(mean apple_price n apple_price) format(%14.2fc) noput
      
      mat list r(xtable)
      mat results = r(xtable)
      di results[1,1]
      Weverthon Machado
      weverthonmachado.github.io

      Comment


      • #4
        Ernestina delPiero , see option replace of the table command.

        Comment


        • #5
          Another workaround is temporary variables:
          Code:
          tempvar groupmean_price
          bysort country state: egen `groupmean_price' = mean(apple_price)
          David Radwin
          Senior Researcher, California Competes
          californiacompetes.org
          Pronouns: He/Him

          Comment

          Working...
          X