Announcement

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

  • Summing up values in rows

    Hello,

    I have the below dataset, and I wish to obtain yearly data, so 7 rows ( for the following years 2019, 2020, 2021, 2022, 2023, 2024 , 2025). I also would like the values in cells from the columns ''Units" and "Unit cost" to be summed up according to the years. Finally, I wish to obtain a new column named "number" that includes the number of rows present for each year.


    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str8 orderdate int column1 byte units float unitcost
    "1/1/19"   2019  3     2
    "4/2/19"   2019  4  4.99
    "1/3/19"   2019  5     3
    "1/8/19"   2019 23  4.99
    "1/5/19"   2019  6     4
    "1/1/19"   2019  3  4.99
    "4/2/19"   2019  6     5
    "1/3/19"   2019  3     5
    "1/8/19"   2019 20     6
    "1/5/19"   2019  3     6
    "1/1/19"   2019  3     5
    "4/2/19"   2019  5     6
    "1/13/20"  2020  6     8
    "1/23/20"  2020  6  4.99
    "1/5/20"   2020  7    76
    "1/7/20"   2020  7  4.99
    "1/26/20"  2020  6     4
    "1/13/20"  2020  7    56
    "1/23/20"  2020  5  4.99
    "1/5/20"   2020  6  4.99
    "1/7/20"   2020 28  4.99
    "1/6/21"   2021 95  1.99
    "1/23/21"  2021 50 19.99
    "2/9/21"   2021 36  4.99
    "2/26/21"  2021 27 19.99
    "3/15/21"  2021 56  2.99
    "4/1/21"   2021 60  4.99
    "4/18/21"  2021 75  1.99
    "5/5/21"   2021 90  4.99
    "5/22/21"  2021 32  1.99
    "6/8/21"   2021 60  8.99
    "6/25/21"  2021 90  4.99
    "7/12/21"  2021 29  1.99
    "7/29/21"  2021 81 19.99
    "8/15/21"  2021 35  4.99
    "9/1/21"   2021  2   125
    "9/18/21"  2021 16 15.99
    "10/5/21"  2021 28  8.99
    "10/22/21" 2021 64  8.99
    "1/15/22"  2022 46  8.99
    "2/1/22"   2022 87    15
    "2/18/22"  2022  4  4.99
    "3/7/22"   2022  7 19.99
    "3/24/22"  2022 50  4.99
    "4/10/22"  2022 66  1.99
    "4/27/22"  2022 96  4.99
    "5/14/22"  2022 53  1.29
    "5/31/22"  2022 80  8.99
    "6/17/22"  2022  5   125
    "7/4/22"   2022 62  4.99
    "7/21/22"  2022 55 12.49
    "8/7/22"   2022 42 23.95
    "8/24/22"  2022  3   275
    "9/10/22"  2022  7  1.29
    "9/27/22"  2022 76  1.99
    "10/14/22" 2022 57 19.99
    "10/31/22" 2022 14  1.29
    "11/17/22" 2022 11  4.99
    "12/4/22"  2022 94     2
    "2/21/23"  2023  3   2.3
    "3/22/23"  2023  2  4.99
    "4/20/23"  2023 28  4.99
    "5/19/23"  2023  2  4.99
    "6/17/23"  2023  4  4.99
    "2/16/23"  2023  5  1.99
    "2/14/23"  2023  2     3
    "3/12/23"  2023  4  1.29
    "2/16/23"  2023  5  4.99
    "2/14/23"  2023  3 19.99
    "3/12/23"  2023  5   2.3
    "1/1/24"   2024 12     3
    "1/2/24"   2024 22  4.99
    "1/3/24"   2024 36     3
    "1/23/25"  2025  5  4.99
    "1/5/25"   2025  3     5
    "12/18/25" 2025  5  4.99
    "1/23/25"  2025 12     6
    "1/5/25"   2025 22     4
    "12/18/25" 2025  5    45
    end

    My aim is to get the Stata code that allows me to get the below data transformation:

    Code:
    * Example generated by -dataex-. For    more    info, type    help    dataex
    clear
    input int(year units) float unitcost    byte    number
    2019  84  56.97 12
    2020  78 168.95  9
    2021 926 263.83 18
    2022 915  544.2 20
    2023  63  55.82 11
    2024  70  10.99  3
    2025  52  69.98  6
    end

    Thank you


  • #2
    This is Statalist, so let's use Stata's terminology. Rows mean observations and columns mean variables.

    That said, see

    Code:
    help collapse
    for what you need.

    Comment


    • #3
      Thank you

      Comment

      Working...
      X