Announcement

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

  • Super simple syntax question

    I have a variable that contains totals of advertising costs for different products from different years, so it contains the total advertising costs for products A, B, and C. My question is how do I total only the costs from specific years within that variable? So to combine the costs for all products in 2007. I know that I'm supposed to provide code, but I can barely add at this point.

  • #2
    Providing code you've tried but that didn't work out is nice, but optional. The problem is, though, that you don't provide example data, and it isn't at all clear from your post how your data is organized: the solution to your problem would depend on details of that.

    So, post back and use the -dataex- command to show us an example of your data, and then I expect you will get a timely and helpful response. If you are running version 15.1 or a fully updated version 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-.

    Comment


    • #3
      All I need is to total the values for the advertisement variable (let's call it advtotal) and total (or sum) it for whenever year = x. So what do I need to total (or sum) advtotal when year = x? Thanks for trying to help my simple mind, but I do not understand Stata in the slightest, nor any sort of coding. I just want to get a single number in return.

      Comment


      • #4
        I have no attempted code, I don't even know how to attempt it.

        Comment


        • #5
          Fine that you don't know how to attempt the code for this. The Forum is here to answer those questions. But a description of your data in words is not helpful. I explained in #2 how to use -dataex- to post an example of your data. If you don't find those instructions clear, try watching David Benson's video on code delimiters and -dataex- at https://youtu.be/bXfaRCAOPbI

          If you do that, we can make progress from there.

          Comment


          • #6
            input int year float advtotal
            2007 0
            2007 0
            2007 169770
            2007 2075025
            2007 52289


            So if I wanted to only add up the values for 2007, what would I do? The variable for the year is just "year" and the one for the other is "advtotal"

            Comment


            • #7
              If you just want to display total in the Results window (or record it in your log file):

              Code:
              summ advtotal if year == 2007
              display `r(sum)'
              If you want to create a new variable which contains each year's total:
              Code:
              by year, sort: egen yearly_total = total(advtotal)

              Comment


              • #8
                The first one is what I needed, thank you

                Comment


                • #9
                  It can't be a "super simple syntax question" if you're clueless where to start! And everyone is clueless some of the time.

                  Seriously, questions are what the forum is mostly for, but in future please use more informative titles.

                  That is in your own interests. People who answer questions are more likely to be attracted by serious and specific titles.

                  Even more important, it is in the interests of other people who may have similar questions and want to learn from existing answers.

                  "Totals for subsets of observations" would be one good title in this case.

                  Comment

                  Working...
                  X