Announcement

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

  • How to sum up all the values by group

    I use Stata 13.1 and I couldn't get the results I want.

    I want to sum up all values in the third column 'expgrp_total' by year and create a new variable filled with the summed value for that same year across the rows.

    My command is this:
    bysort round_year ( firm_id_new) : gen ind_patsubgrp_total = sum( expgrp_total)

    I was expecting in the 4th column to have the total value 75 for all 1978 rows, and 144 for all 1979 rows.
    Please let me know what I can do to change. Thank you.

    [CODE]
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int(round_year firm_id_new) float(expgrp_total ind_patsubgrp_total)
    1978 68 0 0
    1978 89 0 0
    1978 130 22 22
    1978 181 53 75
    1978 182 0 75
    1978 201 0 75
    1979 12 0 0
    1979 68 1 1
    1979 86 63 64
    1979 89 0 64
    1979 97 0 64
    1979 107 0 64
    1979 113 0 64
    1979 130 33 97
    1979 181 47 144
    1979 182 0 144
    1979 201 0 144
    1980 12 0 0
    1980 54 0 0
    1980 62 1 1
    1980 67 0 1
    1980 68 0 1
    1980 82 43 44
    1980 86 39 83
    1980 89 0 83
    1980 96 0 83
    1980 97 0 83

  • #2
    The function -sum()- creates a running sum, not a grand total. If you read -help sum()- you will see that. To get the grand total in each observation of the group, you need to use -egen-, not -gen-, and the function is -total()- not -sum()-:

    Code:
    bysort round_year ( firm_id_new) : egen ind_patsubgrp_total = total( expgrp_total)

    Comment


    • #3
      What Clyde said, or slightly simpler

      Code:
      egen ind_patsubgrp_total = total( expgrp_total), by(round_year)

      Comment


      • #4
        Hi, thank you for pointing out the commands that solve my problem.

        Comment


        • #5
          total() does not work for me. saying: unknown command total().

          Comment


          • #6
            -total- is, indeed, not a command. Nothing in this thread suggests that it is is, nor that you should try to use such a command. -total()- is a function that is used on the right side of the equals sign in an -egen- command, such as is shown in #2 and #3. If the command you are using doesn't look like that, except for the names of the variables, then you are doing something wrong. If you don't know how to conform your command to those models, do post back showing the exact code you are using and somebody will try to help you out.

            Oh, one thought. If you are using a very old version of Stata, the -egen- function -total()- was called -sum()-. I don't remember exactly when they changed the name. But I don't think that's what you're up against here, because the error message would tell you you have asked for an unknown function, not an unknown command, if the command were otherwise correct.

            Comment


            • #7
              A historical footnote to @Clyde Schechter's helpful post.

              The great renaming of egen functions was in Stata 9. That followed a challenging talk from Svend Juul at Berlin in 2004 -- visible at https://www.stata.com/meeting/2german/Juul.pdf -- which pointed out, among other things, some inconsistencies and obscurities in egen function names.

              Part of that was not StataCorp's fault.


              egen started with a rule that still is used: the code for egen function name foo() is in _gfoo.ado. Back when Stata for MS-DOS was a thing, program filenames had to follow an 8.3 length restriction of the form filename.ext. So, a name like _growtotal.ado was just too long at 10.3. Once MS-DOS was no longer supported, that length restriction did not bite, and some better names were possible.

              Comment


              • #8
                Hi everyone, I want to calculate my data which is published weekly.

                This following table represents only for one country.
                country country_code continent population indicator weekly_count year_week
                Afghanistan AFG Asia 38928341 cases 0 2020-01
                Afghanistan AFG Asia 38928341 cases 0 2020-02
                Afghanistan AFG Asia 38928341 cases 0 2020-03
                Afghanistan AFG Asia 38928341 cases 0 2020-04
                Afghanistan AFG Asia 38928341 cases 0 2020-05
                Afghanistan AFG Asia 38928341 cases 0 2020-06
                Afghanistan AFG Asia 38928341 cases 0 2020-07
                Afghanistan AFG Asia 38928341 cases 0 2020-08
                Afghanistan AFG Asia 38928341 cases 1 2020-09
                Afghanistan AFG Asia 38928341 cases 3 2020-10
                Afghanistan AFG Asia 38928341 cases 12 2020-11
                Afghanistan AFG Asia 38928341 cases 18 2020-12
                Afghanistan AFG Asia 38928341 cases 80 2020-13
                Afghanistan AFG Asia 38928341 cases 185 2020-14
                Afghanistan AFG Asia 38928341 cases 308 2020-15
                Afghanistan AFG Asia 38928341 cases 389 2020-16
                Afghanistan AFG Asia 38928341 cases 535 2020-17
                Afghanistan AFG Asia 38928341 cases 1173 2020-18
                Afghanistan AFG Asia 38928341 cases 1698 2020-19
                Afghanistan AFG Asia 38928341 cases 2262 2020-20
                Afghanistan AFG Asia 38928341 cases 3918 2020-21
                Afghanistan AFG Asia 38928341 cases 4623 2020-22
                Afghanistan AFG Asia 38928341 cases 5137 2020-23
                Afghanistan AFG Asia 38928341 cases 4424 2020-24
                Afghanistan AFG Asia 38928341 cases 4067 2020-25
                Afghanistan AFG Asia 38928341 cases 2134 2020-26
                Afghanistan AFG Asia 38928341 cases 1984 2020-27
                Afghanistan AFG Asia 38928341 cases 1500 2020-28
                Afghanistan AFG Asia 38928341 cases 1024 2020-29
                Afghanistan AFG Asia 38928341 cases 788 2020-30
                Afghanistan AFG Asia 38928341 cases 447 2020-31
                Afghanistan AFG Asia 38928341 cases 344 2020-32
                Afghanistan AFG Asia 38928341 cases 542 2020-33
                Afghanistan AFG Asia 38928341 cases 403 2020-34
                Afghanistan AFG Asia 38928341 cases 163 2020-35
                Afghanistan AFG Asia 38928341 cases 236 2020-36
                Afghanistan AFG Asia 38928341 cases 318 2020-37
                Afghanistan AFG Asia 38928341 cases 328 2020-38
                Afghanistan AFG Asia 38928341 cases 183 2020-39
                Afghanistan AFG Asia 38928341 cases 114 2020-40
                Afghanistan AFG Asia 38928341 cases 458 2020-41
                Afghanistan AFG Asia 38928341 cases 401 2020-42
                Afghanistan AFG Asia 38928341 cases 633 2020-43
                Afghanistan AFG Asia 38928341 cases 800 2020-44
                Afghanistan AFG Asia 38928341 cases 606 2020-45
                Afghanistan AFG Asia 38928341 cases 1164 2020-46
                Afghanistan AFG Asia 38928341 cases 1368 2020-47
                Afghanistan AFG Asia 38928341 cases 1073 2020-48
                Afghanistan AFG Asia 38928341 cases 1672 2020-49
                Afghanistan AFG Asia 38928341 cases 1757 2020-50
                Afghanistan AFG Asia 38928341 cases 740 2020-51
                Afghanistan AFG Asia 38928341 cases 1994 2020-52
                Afghanistan AFG Asia 38928341 cases 902 2020-53
                Afghanistan AFG Asia 38928341 cases 675 2021-01
                Afghanistan AFG Asia 38928341 cases 557 2021-02
                Afghanistan AFG Asia 38928341 cases 713 2021-03
                Afghanistan AFG Asia 38928341 cases 267 2021-04
                Afghanistan AFG Asia 38928341 cases 238 2021-05
                Afghanistan AFG Asia 38928341 cases 155 2021-06
                Afghanistan AFG Asia 38928341 cases 0 2021-07
                Afghanistan AFG Asia 38928341 cases 245 2021-08
                Afghanistan AFG Asia 38928341 cases 117 2021-09
                Afghanistan AFG Asia 38928341 cases 119 2021-10
                Afghanistan AFG Asia 38928341 cases 182 2021-11
                Afghanistan AFG Asia 38928341 cases 117 2021-12
                Afghanistan AFG Asia 38928341 cases 485 2021-13
                Afghanistan AFG Asia 38928341 cases 474 2021-14
                Afghanistan AFG Asia 38928341 cases 645 2021-15
                Afghanistan AFG Asia 38928341 cases 1117 2021-16
                Afghanistan AFG Asia 38928341 cases 1555 2021-17
                Afghanistan AFG Asia 38928341 cases 1272 2021-18
                Afghanistan AFG Asia 38928341 cases 1757 2021-19
                Afghanistan AFG Asia 38928341 cases 2676 2021-20
                Afghanistan AFG Asia 38928341 cases 6706 2021-21
                Afghanistan AFG Asia 38928341 cases 7860 2021-22
                Afghanistan AFG Asia 38928341 cases 10627 2021-23
                Afghanistan AFG Asia 38928341 cases 14281 2021-24
                Afghanistan AFG Asia 38928341 cases 10002 2021-25
                Afghanistan AFG Asia 38928341 cases 11714 2021-26
                Afghanistan AFG Asia 38928341 cases 6113 2021-27
                Afghanistan AFG Asia 38928341 cases 8836 2021-28
                Afghanistan AFG Asia 38928341 cases 2594 2021-29
                Afghanistan AFG Asia 38928341 cases 3564 2021-30
                Afghanistan AFG Asia 38928341 cases 2441 2021-31
                Afghanistan AFG Asia 38928341 cases 1350 2021-32
                Afghanistan AFG Asia 38928341 cases 220 2021-33
                Afghanistan AFG Asia 38928341 cases 637 2021-34
                Afghanistan AFG Asia 38928341 cases 203 2021-35
                Afghanistan AFG Asia 38928341 cases 757 2021-36
                Afghanistan AFG Asia 38928341 cases 532 2021-37
                Afghanistan AFG Asia 38928341 deaths 0 2020-01
                Afghanistan AFG Asia 38928341 deaths 0 2020-02
                Afghanistan AFG Asia 38928341 deaths 0 2020-03
                Afghanistan AFG Asia 38928341 deaths 0 2020-04
                Afghanistan AFG Asia 38928341 deaths 0 2020-05
                Afghanistan AFG Asia 38928341 deaths 0 2020-06
                Afghanistan AFG Asia 38928341 deaths 0 2020-07
                Afghanistan AFG Asia 38928341 deaths 0 2020-08
                Afghanistan AFG Asia 38928341 deaths 0 2020-09
                Afghanistan AFG Asia 38928341 deaths 0 2020-10
                Afghanistan AFG Asia 38928341 deaths 0 2020-11
                Afghanistan AFG Asia 38928341 deaths 0 2020-12
                Afghanistan AFG Asia 38928341 deaths 4 2020-13
                Afghanistan AFG Asia 38928341 deaths 3 2020-14
                Afghanistan AFG Asia 38928341 deaths 11 2020-15
                Afghanistan AFG Asia 38928341 deaths 15 2020-16
                Afghanistan AFG Asia 38928341 deaths 24 2020-17
                Afghanistan AFG Asia 38928341 deaths 28 2020-18
                Afghanistan AFG Asia 38928341 deaths 35 2020-19
                Afghanistan AFG Asia 38928341 deaths 49 2020-20
                Afghanistan AFG Asia 38928341 deaths 49 2020-21
                Afghanistan AFG Asia 38928341 deaths 39 2020-22
                Afghanistan AFG Asia 38928341 deaths 100 2020-23
                Afghanistan AFG Asia 38928341 deaths 114 2020-24
                Afghanistan AFG Asia 38928341 deaths 110 2020-25
                Afghanistan AFG Asia 38928341 deaths 140 2020-26
                Afghanistan AFG Asia 38928341 deaths 143 2020-27
                Afghanistan AFG Asia 38928341 deaths 146 2020-28
                Afghanistan AFG Asia 38928341 deaths 171 2020-29
                Afghanistan AFG Asia 38928341 deaths 88 2020-30
                Afghanistan AFG Asia 38928341 deaths 15 2020-31
                Afghanistan AFG Asia 38928341 deaths 28 2020-32
                Afghanistan AFG Asia 38928341 deaths 63 2020-33
                Afghanistan AFG Asia 38928341 deaths 12 2020-34
                Afghanistan AFG Asia 38928341 deaths 15 2020-35
                Afghanistan AFG Asia 38928341 deaths 10 2020-36
                Afghanistan AFG Asia 38928341 deaths 8 2020-37
                Afghanistan AFG Asia 38928341 deaths 21 2020-38
                Afghanistan AFG Asia 38928341 deaths 12 2020-39
                Afghanistan AFG Asia 38928341 deaths 9 2020-40
                Afghanistan AFG Asia 38928341 deaths 15 2020-41
                Afghanistan AFG Asia 38928341 deaths 15 2020-42
                Afghanistan AFG Asia 38928341 deaths 22 2020-43
                Afghanistan AFG Asia 38928341 deaths 27 2020-44
                Afghanistan AFG Asia 38928341 deaths 24 2020-45
                Afghanistan AFG Asia 38928341 deaths 61 2020-46
                Afghanistan AFG Asia 38928341 deaths 69 2020-47
                Afghanistan AFG Asia 38928341 deaths 68 2020-48
                Afghanistan AFG Asia 38928341 deaths 137 2020-49
                Afghanistan AFG Asia 38928341 deaths 71 2020-50
                Afghanistan AFG Asia 38928341 deaths 111 2020-51
                Afghanistan AFG Asia 38928341 deaths 88 2020-52
                Afghanistan AFG Asia 38928341 deaths 60 2020-53
                Afghanistan AFG Asia 38928341 deaths 71 2021-01
                Afghanistan AFG Asia 38928341 deaths 45 2021-02
                Afghanistan AFG Asia 38928341 deaths 43 2021-03
                Afghanistan AFG Asia 38928341 deaths 16 2021-04
                Afghanistan AFG Asia 38928341 deaths 8 2021-05
                Afghanistan AFG Asia 38928341 deaths 14 2021-06
                Afghanistan AFG Asia 38928341 deaths 0 2021-07
                Afghanistan AFG Asia 38928341 deaths 19 2021-08
                Afghanistan AFG Asia 38928341 deaths 5 2021-09
                Afghanistan AFG Asia 38928341 deaths 9 2021-10
                Afghanistan AFG Asia 38928341 deaths 6 2021-11
                Afghanistan AFG Asia 38928341 deaths 4 2021-12
                Afghanistan AFG Asia 38928341 deaths 42 2021-13
                Afghanistan AFG Asia 38928341 deaths 18 2021-14
                Afghanistan AFG Asia 38928341 deaths 16 2021-15
                Afghanistan AFG Asia 38928341 deaths 46 2021-16
                Afghanistan AFG Asia 38928341 deaths 56 2021-17
                Afghanistan AFG Asia 38928341 deaths 38 2021-18
                Afghanistan AFG Asia 38928341 deaths 68 2021-19
                Afghanistan AFG Asia 38928341 deaths 58 2021-20
                Afghanistan AFG Asia 38928341 deaths 162 2021-21
                Afghanistan AFG Asia 38928341 deaths 213 2021-22
                Afghanistan AFG Asia 38928341 deaths 425 2021-23
                Afghanistan AFG Asia 38928341 deaths 681 2021-24
                Afghanistan AFG Asia 38928341 deaths 437 2021-25
                Afghanistan AFG Asia 38928341 deaths 630 2021-26
                Afghanistan AFG Asia 38928341 deaths 364 2021-27
                Afghanistan AFG Asia 38928341 deaths 542 2021-28
                Afghanistan AFG Asia 38928341 deaths 249 2021-29
                Afghanistan AFG Asia 38928341 deaths 289 2021-30
                Afghanistan AFG Asia 38928341 deaths 157 2021-31
                Afghanistan AFG Asia 38928341 deaths 82 2021-32
                Afghanistan AFG Asia 38928341 deaths 33 2021-33
                Afghanistan AFG Asia 38928341 deaths 42 2021-34
                Afghanistan AFG Asia 38928341 deaths 14 2021-35
                Afghanistan AFG Asia 38928341 deaths 39 2021-36
                Afghanistan AFG Asia 38928341 deaths 20 2021-37

                My question is now how can firstly separate death and case as a single column for each country?
                Furthermore how could I simply sum weekly_count data to obtain monthly data?

                Thanks for your time.

                Comment


                • #9
                  #8 Taking your questions in turn:

                  1. The answer to that depends on exactly what the variables are. We need to see a data example to work that out. Please use dataex as requested.

                  2. Once the first question is solved, the answer lies above in this thread.

                  However your weekly dates are not Stata weeks, so we still need question 1 to be answered first. Do not use Stata week functions. (The telltale sign is a week 53.)

                  Comment


                  • #10
                    The first part, creating separate variables ("columns") for cases and deaths is a fairly straightforward application of -reshape-. It is made somewhat more complicated because your indicator values contain trailing blanks--those have to be removed first. I also decided to shorten weekly_count to n_, just to save on typing.

                    As for aggregating weekly data to monthly, that is quite complicated. In fact, there is no truly consistent way to do it because some weeks straddle two different months. Also, there is no consistently used definition of a week. I see that in your data, you have an observation with week = 53. Stata date weekly date functions do not work that way. Instead, in Stata, the first week begins on January 1, regardless of which day of the week that is, and every 7 days we start a new weeks, except that week 52 gets extended to include the extra day or days remaining in December. There are several other systems of defining weeks in use, and trying to harmonize data across them is a nightmare! So I'm glossing over those details in the code I show here. I have changed references to week 53 to week 52. And I assign each week's data entirely to the month in which that week begins.

                    Code:
                    * Example generated by -dataex-. For more info, type help dataex
                    clear
                    input str12 country str4 country_code str5 continent long population str7 indicator int weekly_count str7 year_week
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "      0 "2020-01"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "      0 "2020-02"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "      0 "2020-03"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "      0 "2020-04"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "      0 "2020-05"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "      0 "2020-06"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "      0 "2020-07"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "      0 "2020-08"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "      1 "2020-09"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "      3 "2020-10"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "     12 "2020-11"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "     18 "2020-12"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "     80 "2020-13"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "    185 "2020-14"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "    308 "2020-15"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "    389 "2020-16"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "    535 "2020-17"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "   1173 "2020-18"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "   1698 "2020-19"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "   2262 "2020-20"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "   3918 "2020-21"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "   4623 "2020-22"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "   5137 "2020-23"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "   4424 "2020-24"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "   4067 "2020-25"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "   2134 "2020-26"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "   1984 "2020-27"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "   1500 "2020-28"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "   1024 "2020-29"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "    788 "2020-30"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "    447 "2020-31"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "    344 "2020-32"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "    542 "2020-33"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "    403 "2020-34"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "    163 "2020-35"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "    236 "2020-36"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "    318 "2020-37"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "    328 "2020-38"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "    183 "2020-39"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "    114 "2020-40"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "    458 "2020-41"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "    401 "2020-42"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "    633 "2020-43"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "    800 "2020-44"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "    606 "2020-45"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "   1164 "2020-46"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "   1368 "2020-47"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "   1073 "2020-48"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "   1672 "2020-49"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "   1757 "2020-50"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "    740 "2020-51"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "   1994 "2020-52"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "    902 "2020-53"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "    675 "2021-01"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "    557 "2021-02"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "    713 "2021-03"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "    267 "2021-04"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "    238 "2021-05"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "    155 "2021-06"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "      0 "2021-07"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "    245 "2021-08"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "    117 "2021-09"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "    119 "2021-10"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "    182 "2021-11"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "    117 "2021-12"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "    485 "2021-13"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "    474 "2021-14"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "    645 "2021-15"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "   1117 "2021-16"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "   1555 "2021-17"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "   1272 "2021-18"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "   1757 "2021-19"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "   2676 "2021-20"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "   6706 "2021-21"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "   7860 "2021-22"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "  10627 "2021-23"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "  14281 "2021-24"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "  10002 "2021-25"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "  11714 "2021-26"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "   6113 "2021-27"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "   8836 "2021-28"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "   2594 "2021-29"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "   3564 "2021-30"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "   2441 "2021-31"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "   1350 "2021-32"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "    220 "2021-33"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "    637 "2021-34"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "    203 "2021-35"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "    757 "2021-36"
                    "Afghanistan " "AFG " "Asia " 38928341 "cases "    532 "2021-37"
                    "Afghanistan " "AFG " "Asia " 38928341 "deaths "     0 "2020-01"
                    "Afghanistan " "AFG " "Asia " 38928341 "deaths "     0 "2020-02"
                    "Afghanistan " "AFG " "Asia " 38928341 "deaths "     0 "2020-03"
                    "Afghanistan " "AFG " "Asia " 38928341 "deaths "     0 "2020-04"
                    "Afghanistan " "AFG " "Asia " 38928341 "deaths "     0 "2020-05"
                    "Afghanistan " "AFG " "Asia " 38928341 "deaths "     0 "2020-06"
                    "Afghanistan " "AFG " "Asia " 38928341 "deaths "     0 "2020-07"
                    "Afghanistan " "AFG " "Asia " 38928341 "deaths "     0 "2020-08"
                    "Afghanistan " "AFG " "Asia " 38928341 "deaths "     0 "2020-09"
                    "Afghanistan " "AFG " "Asia " 38928341 "deaths "     0 "2020-10"
                    end
                    
                    rename weekly_count n_
                    replace indicator = trim(indicator)
                    reshape wide n_, i(country year_week) j(indicator) string
                    
                    split year_week, gen(date) parse("-") destring
                    rename date1 year
                    rename date2 week
                    replace week = min(week, 52)
                    gen mdate = mofd(dofw(yw(year, week)))
                    format mdate %tm
                    
                    collapse (sum) n_cases n_deaths (first) country_code continent population, by(country mdate)
                    In the future, when showing data examples, please use the -dataex- command to do so, as I have here. If you are running version 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

                    When asking for help with code, always show example data. When showing example data, always use -dataex-.

                    Added: Crossed with #2.

                    Comment


                    • #11
                      If the weeks in #8 are epiweeks see epiweek from SSC for conversion commands.

                      Comment


                      • #12
                        Svend Juul's great teaching materials (Introduction to Stata 7 and Introduction to Stata 8) are introductory textbooks in my beginning of Stata use. In the speech presentation attached in #7, What he refered to as faults, traps and clumsiness are still widespread in Stata. These make the use of Stata be prone to err. Svend Juul's description still holds true when he said
                        I'm not so sure. People tell me that it is difficult to get started.It may be OK for a full-time researcher, but our junior doctors can't spend weeks to find out how to use it... I heard that SPSS is a lot friendlier.
                        Code:
                        log using filename                     //most often people fogot to open one beforehand
                        summarize, meanonly
                        tabulate indpevar [indepvar], summarize(depvar)  //tab1, tab2, tabulate, table, tabstat and many many community-contributed commands as regards table display and output.
                        generate y = max(x1, x2, x3)
                        egen y = rmax(x1 x2 x3)           //different syntax from generate, why one should invoke comma (,) and another don't?
                        generate y = sum(x1)
                        egen y = sum(x1)                     //same word with different meanings!
                        anycommand if age>60           //yes, missing (. or .a etc.) is > 60! we must always keep in mind to type "if age>60 & age<."
                        And I want to add: why we can use comments and blank lines in Do-file Editor, but prohibited in Command Window?

                        Comment


                        • #13
                          Dear Clyde, thanks a lot for your full information and it was really helpful.
                          Now, I need to separate my Imports countries and Export countries for the data which all are in one column.
                          Which codes I need for that? Would you please help me in this step, too?

                          By the way, regarding this command -dataex-, I will run it on Stata, but how can I share it here? I mean from which icon?

                          Comment


                          • #14
                            You don't need any icon to share -dataex- output here. Look carefully at the dataex output. It begins with a line that says "copy starting from the next line" and ends with a line that says "copy up to and including the previous line." Follow those instructions: copy everything between those lines, not including those two lines. Then paste directly into the same Forum window where you type your questions. It's as easy as that.

                            And you need to come up with a new example to answer "I need to separate my Imports countries and Export countries for the data which all are in one column.
                            Which codes I need for that?" The example you gave in #8 has no information I can see that indicates which countries are Imports and which are Exports. And I'm also not sure exactly what other variables need to be separated along with it. So please post back with a new -dataex- that illustrates the problem and a clearer explanation of what you want the result to look like.

                            One more thing: what you are asking for here is off the topic of this thread. Because these threads are read and searched by others, often searching for specific topics, it is important to keep threads on-topic. So when you post back for this, please start a new thread, rather than extending this one.

                            Comment


                            • #15
                              Hello, thanks a lot for your response regarding my first question.
                              I have now the below information, What I have mdate (as my year and month). when I want to drop period between 2021/05 to 2021/09:
                              I run this codes but it made an error:

                              drop if mdate==2021m05

                              this information is the exact variable I have. Could you please let me what should I change in this code?

                              * Example generated by -dataex-. To install: ssc install dataex
                              clear
                              input str33 country float mdate double(n_cases n_deaths) str3 country_code
                              "Afghanistan" 720 0 0 "AFG"
                              "Afghanistan" 721 1 0 "AFG"
                              "Afghanistan" 722 113 4 "AFG"
                              "Afghanistan" 723 2590 81 "AFG"
                              "Afghanistan" 724 12501 172 "AFG"
                              "Afghanistan" 725 15762 464 "AFG"
                              "Afghanistan" 726 5743 563 "AFG"
                              "Afghanistan" 727 1452 118 "AFG"
                              "Afghanistan" 728 1179 60 "AFG"
                              "Afghanistan" 729 2292 79 "AFG"
                              "Afghanistan" 730 4211 222 "AFG"
                              "Afghanistan" 731 7065 467 "AFG"
                              "Afghanistan" 732 2450 183 "AFG"
                              "Afghanistan" 733 517 38 "AFG"
                              "Afghanistan" 734 903 61 "AFG"
                              "Afghanistan" 735 5063 174 "AFG"
                              "Afghanistan" 736 18999 501 "AFG"
                              "Afghanistan" 737 46624 2173 "AFG"
                              "Afghanistan" 738 23548 1601 "AFG"
                              "Afghanistan" 739 2410 171 "AFG"
                              "Afghanistan" 740 1289 59 "AFG"
                              "Africa (total)" 720 0 0 ""
                              "Africa (total)" 721 6 0 ""
                              "Africa (total)" 722 4623 143 ""
                              "Africa (total)" 723 39806 1656 ""
                              "Africa (total)" 724 102561 2423 ""
                              "Africa (total)" 725 235734 5438 ""
                              "Africa (total)" 726 575100 10611 ""
                              "Africa (total)" 727 288355 9314 ""
                              "Africa (total)" 728 270721 6922 ""
                              "Africa (total)" 729 279842 6695 ""
                              "Africa (total)" 730 367415 8524 ""
                              "Africa (total)" 731 668590 15554 ""
                              "Africa (total)" 732 840428 27848 ""
                              "Africa (total)" 733 297189 10660 ""
                              "Africa (total)" 734 298318 8010 ""
                              "Africa (total)" 735 366131 10629 ""
                              "Africa (total)" 736 293620 7920 ""
                              "Africa (total)" 737 747646 13937 ""
                              "Africa (total)" 738 1367563 31347 ""
                              "Africa (total)" 739 891190 21652 ""
                              "Africa (total)" 740 245717 6854 ""
                              "Albania" 722 212 10 "ALB"
                              "Albania" 723 583 21 "ALB"
                              "Albania" 724 342 2 "ALB"
                              "Albania" 725 1265 22 "ALB"
                              "Albania" 726 3117 111 "ALB"
                              "Albania" 727 3861 114 "ALB"
                              "Albania" 728 4886 116 "ALB"
                              "Albania" 729 6936 122 "ALB"
                              "Albania" 730 16423 280 "ALB"
                              "Albania" 731 21813 395 "ALB"
                              "Albania" 732 25898 267 "ALB"
                              "Albania" 733 27561 479 "ALB"
                              "Albania" 734 13634 326 "ALB"
                              "Albania" 735 5192 147 "ALB"
                              "Albania" 736 656 39 "ALB"
                              "Albania" 737 156 5 "ALB"
                              "Albania" 738 1377 3 "ALB"
                              "Albania" 739 17085 56 "ALB"
                              "Albania" 740 11956 65 "ALB"
                              "Algeria" 720 0 0 "DZA"
                              "Algeria" 721 3 0 "DZA"
                              "Algeria" 722 508 31 "DZA"
                              "Algeria" 723 3963 432 "DZA"
                              "Algeria" 724 4920 190 "DZA"
                              "Algeria" 725 3879 244 "DZA"
                              "Algeria" 726 18192 334 "DZA"
                              "Algeria" 727 12681 270 "DZA"
                              "Algeria" 728 7990 259 "DZA"
                              "Algeria" 729 6136 213 "DZA"
                              "Algeria" 730 23949 437 "DZA"
                              "Algeria" 731 18187 362 "DZA"
                              "Algeria" 732 8680 142 "DZA"
                              "Algeria" 733 5294 104 "DZA"
                              "Algeria" 734 3357 90 "DZA"
                              "Algeria" 735 6365 220 "DZA"
                              "Algeria" 736 6854 190 "DZA"
                              "Algeria" 737 10513 237 "DZA"
                              "Algeria" 738 39905 795 "DZA"
                              "Algeria" 739 16628 870 "DZA"
                              "Algeria" 740 3596 274 "DZA"
                              "America (total)" 720 17 0 ""
                              "America (total)" 721 120 2 ""
                              "America (total)" 722 165815 3041 ""
                              "America (total)" 723 1319861 87393 ""
                              "America (total)" 724 1443698 89631 ""
                              "America (total)" 725 2243103 94643 ""
                              "America (total)" 726 4630298 129494 ""
                              "America (total)" 727 3623472 108691 ""
                              "America (total)" 728 3823823 111132 ""
                              "America (total)" 729 3529362 72709 ""
                              "America (total)" 730 6005330 83967 ""
                              "America (total)" 731 10302162 156619 ""
                              "America (total)" 732 10461529 236289 ""
                              "America (total)" 733 4600047 156329 ""
                              "America (total)" 734 5066572 144829 ""
                              "America (total)" 735 6967170 201575 ""
                              "America (total)" 736 4790160 132536 ""
                              "America (total)" 737 4372585 117640 ""
                              end
                              format %tm mdate
                              [/CODE]


                              Furthermore, I need to separate countries to exporters and importers, would you help me how could I do that?

                              Comment

                              Working...
                              X