Announcement

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

  • divide numbers in stata

    Hi,
    I have a panel data set where i have variables "location"(where number 1 means one location, 2 another one ans so on) , "sales" numbers in dollar, and year.

    i want this for example to work: total sales if location ==1 / total sales
    what do i do wrong here? stata does not want to devide things if it is not variables and total sales is one value generated by summarizing all the sales and total sales if location ==1 is an estracted value(total). i want to do this for all locations.
    how can i come around this?


    it would be helpful if you know how to solve this problem in a table if it is possible as i would want to do it on all locations and se how big the percentage sales of each location is on the total sales of the whole dataset

  • #2
    You ask what you did wrong but you don't give a data example, or show what is obviously a command, or tell us precisely what Stata did or did not do. Also, your example defines a proportion but you also talk about percents.

    This reproducible example should be modifiable for your problem, but if not then please read and act on https://www.statalist.org/forums/help#stata


    Code:
    . webuse grunfeld, clear
    
    . egen pc = pc(invest), by(year)
    
    
    . format invest pc %3.2f
    
    . l company invest pc if year == 1935
    
         +--------------------------+
         | company   invest      pc |
         |--------------------------|
      1. |       1   317.60   43.66 |
     21. |       2   209.90   28.85 |
     41. |       3    33.10    4.55 |
     61. |       4    40.29    5.54 |
     81. |       5    39.68    5.45 |
         |--------------------------|
    101. |       6    20.36    2.80 |
    121. |       7    24.43    3.36 |
    141. |       8    12.93    1.78 |
    161. |       9    26.63    3.66 |
    181. |      10     2.54    0.35 |
         +--------------------------+
    Last edited by Nick Cox; 22 Sep 2020, 05:33.

    Comment


    • #3
      ok it was helpful. sorry for being unclear. i am quite new at this. i seem to have a problem in general and that is when i want to divide spesific numbers extraacted from the dataset. it could be interesting to for example look at how big the sales in one location is compared to all sales in the data set. and hence take what i would find locical to do the command : total sales if location == 1 / total sales (if i was unclear earlier this is what i would write as a command) this answer will hence become a percentage number but stata does not like to divide like this. total sales is the command i would use to sum up all the individual sales. i have serched all over and i can do those kind of calculations in stata. do you understand my problem?

      Comment


      • #4
        I think you want

        Code:
        summ sales if location == 1
        sca Salesat1 = r(sum)
        summ sales
        
        dis Salesat1/r(sum)
        but if not, provide an example of your data using -dataex-, and explain on it what the desired outcome is .

        Originally posted by sam james View Post
        ok it was helpful. sorry for being unclear. i am quite new at this. i seem to have a problem in general and that is when i want to divide spesific numbers extraacted from the dataset. it could be interesting to for example look at how big the sales in one location is compared to all sales in the data set. and hence take what i would find locical to do the command : total sales if location == 1 / total sales (if i was unclear earlier this is what i would write as a command) this answer will hence become a percentage number but stata does not like to divide like this. total sales is the command i would use to sum up all the individual sales. i have serched all over and i can do those kind of calculations in stata. do you understand my problem?

        Comment


        • #5
          Thank you! may be but as sales is the price of one individual sale the sum function counts number of sales and does not find the total sales in dollars. hence, i thought i had to use "total" instead of "sum"

          Comment


          • #6
            #2 was already an answer to #3 as well as to #1,

            Code:
            total sales if location == 1 / total sales
            would indeed be quite illegal as Stata syntax.

            Otherwise it is not correct that any sum() function counts number of items while any total() function calculates totals. As far as egen is concerned sum() and total() are different names for the same code. The Stata function sum() is different and calculates cumulative sums. The total command is different. The summarize command is quite different, but as @Joro Kolev's example shows it calculates sums as well as other results.

            We have asked twice now for a data example using dataex. I don't think we can help further without one.

            Comment


            • #7
              Dear all,

              I want to divide weekly data for all the states by their previous week's observations. What I am essentially trying to do is calculate the basic reproduction number (R0) .

              So in the case of entire India between week 3129 and 3130 would 83/57 =1.456



              Code:
              * Example generated by -dataex-. To install: ssc install dataex
              clear
              input str40 state float date_weekly double(confirmed_delta recovered_delta deceased_delta tested_delta)
              "India"                       3124    2   0  0     0
              "Kerala"                      3124    2   0  0     0
              "India"                       3126    0   3  0     0
              "Kerala"                      3126    0   3  0     0
              "Delhi"                       3128    0   0  0     0
              "India"                       3128    3   0  0     0
              "Kerala"                      3128    0   0  0     0
              "Rajasthan"                   3128    0   0  0     0
              "Telangana"                   3128    0   0  0     0
              "Delhi"                       3129    3   0  0     0
              "Haryana"                     3129    0   0  0     0
              "India"                       3129   57   0  0     0
              "Jammu and Kashmir"           3129    0   0  0     0
              "Karnataka"                   3129    3   0  0     0
              "Kerala"                      3129   14   0  0     0
              "Ladakh"                      3129    0   0  0     0
              "Maharashtra"                 3129    3   0  0     0
              "Punjab"                      3129    0   0  0     0
              "Rajasthan"                   3129    2   0  0     0
              "Tamil Nadu"                  3129    0   0  0     0
              "Telangana"                   3129    0   0  0     0
              "Uttar Pradesh"               3129    3   0  0     0
              "Andhra Pradesh"              3130    0   0  0     0
              "Delhi"                       3130    6   0  0     0
              "Haryana"                     3130    1   0  0     0
              "Himachal Pradesh"            3130    0   0  0     0
              "India"                       3130   83   0  1     0
              "Jammu and Kashmir"           3130    2   0  0     0
              "Karnataka"                   3130    7   0  1     0
              "Kerala"                      3130   10   0  0     0
              "Ladakh"                      3130    6   0  0     0
              "Maharashtra"                 3130   36   0  0     0
              "Odisha"                      3130    0   0  0     0
              "Puducherry"                  3130    0   0  0     0
              "Punjab"                      3130    0   0  0     0
              "Rajasthan"                   3130    1   0  0     0
              "Tamil Nadu"                  3130    0   0  0     0
              "Telangana"                   3130    4   0  0     0
              "Uttar Pradesh"               3130    5   0  0     0
              "Uttarakhand"                 3130    0   0  0     0
              "West Bengal"                 3130    0   0  0     0
              "Andhra Pradesh"              3131    7   0  0     0
              "Bihar"                       3131    1   0  0     0
              "Chandigarh"                  3131    6   0  0     0
              "Chhattisgarh"                3131    0   0  0     0
              "Delhi"                       3131   20   0  0     0
              "Gujarat"                     3131   34   0  0     0
              "Haryana"                     3131   15   0  0     0
              "Himachal Pradesh"            3131    3   0  0     0
              "India"                       3131  425   3  0 16194
              "Jammu and Kashmir"           3131    3   0  0     0
              "Karnataka"                   3131   30   3  0     0
              "Kerala"                      3131   82   0  0     0
              "Ladakh"                      3131    5   0  0     0
              "Madhya Pradesh"              3131    3   0  0     0
              "Maharashtra"                 3131   66   0  0     0
              "Manipur"                     3131    0   0  0     0
              "Odisha"                      3131    1   0  0     0
              "Puducherry"                  3131    0   0  0     0
              "Punjab"                      3131   28   0  0     0
              "Rajasthan"                   3131   28   0  0     0
              "Tamil Nadu"                  3131   17   0  0     0
              "Telangana"                   3131   34   0  0     0
              "Uttar Pradesh"               3131   20   0  0     0
              "Uttarakhand"                 3131    3   0  0     0
              "West Bengal"                 3131    8   0  0     0
              "Andaman and Nicobar Islands" 3132    9   0  0     0
              "Andhra Pradesh"              3132   36   1  0     0
              "Assam"                       3132    0   0  0     0
              "Bihar"                       3132   18   0  1     0
              "Chandigarh"                  3132    8   0  0     0
              "Chhattisgarh"                3132    8   2  0     0
              "Delhi"                       3132   90   6  2     0
              "Goa"                         3132    2   0  0     0
              "Gujarat"                     3132   38   5  6     0
              "Haryana"                     3132   13  24  0     0
              "Himachal Pradesh"            3132    0   1  1     0
              "India"                       3132 1064 154 46 20094
              "Jammu and Kashmir"           3132   49   1  2     0
              "Jharkhand"                   3132    0   0  0     0
              "Karnataka"                   3132   60   5  2     0
              "Kerala"                      3132  132  21  2     0
              "Ladakh"                      3132    0   3  0     0
              "Madhya Pradesh"              3132   59   0  5     0
              "Maharashtra"                 3132  195  39 11     0
              "Manipur"                     3132    0   0  0     0
              "Mizoram"                     3132    0   0  0     0
              "Odisha"                      3132    2   0  0     0
              "Puducherry"                  3132    0   0  0     0
              "Punjab"                      3132   13   1  4     0
              "Rajasthan"                   3132   61   3  0     0
              "Tamil Nadu"                  3132  106   6  1     0
              "Telangana"                   3132   58  14  6     0
              "Uttar Pradesh"               3132   69  17  0     0
              "Uttarakhand"                 3132    3   2  0     0
              "West Bengal"                 3132   28   3  3     0
              "Andaman and Nicobar Islands" 3133    0   0  0     0
              "Andhra Pradesh"              3133  270   4  4     0
              "Arunachal Pradesh"           3133    0   0  0     0
              "Assam"                       3133   27   0  0     0
              end
              Following up on up earlier question, I did the following

              Code:
              encode state, generate(state_new)
              xtset state_new  date_weekly
              bysort state_new ( date_weekly) : gen confirmed_delta_lag=l.confirmed_delta
              gen cases_r0= confirmed_delta / confirmed_delta_lag

              I was wondering this is the right way to go?
              Best,
              Anustup
              Last edited by Anustup Kundu; 11 Jun 2021, 09:25.

              Comment

              Working...
              X