Announcement

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

  • Create a new table with means from table

    Hi,

    I needed the mean of var 2 that is dependent on var so inputed this ' bysort var1 : sum var 2" into Stata. This gave me a table with the mean but I would now like to use the different means to create a new variable.

  • #2
    Your description is not all that clear, and you do not provide example data, but I think what you want is:

    Code:
    by var1, sort: egen wanted = mean(var2)
    If this is not it, please post back with example data, using the -dataex- command, and then show some hand-worked results to illustrate your needs.

    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.

    Comment


    • #3
      My data is confidential so I will not be able to share it .

      But you are right I did not make my self clear enough. inserting this code simply gives me a list of all the means. My intention was to create a variable that consists of the means of var2 for each value of var 1. So for example the amount of x for each week of var1 ( var1 is sorted in weeks) . I am not sure if this is even possible and I apologise if I am not being clear enough

      Comment


      • #4
        I wonder if this is what you want.
        Code:
        webuse grunfeld,clear
        collapse invest,by(company)
        
        . list
        
             +-------------------+
             | company    invest |
             |-------------------|
          1. |       1    608.02 |
          2. |       2   410.475 |
          3. |       3    102.29 |
          4. |       4   86.1235 |
          5. |       5   61.8025 |
             |-------------------|
          6. |       6    55.411 |
          7. |       7   47.5955 |
          8. |       8   42.8915 |
          9. |       9    41.889 |
         10. |      10    3.0845 |
             +-------------------+
        Best regards.

        Raymond Zhang
        Stata 17.0,MP

        Comment


        • #5
          My intention was to create a variable that consists of the means of var2 for each value of var 1.
          That is precisely what the code in #2 does. Did you try it?

          My data is confidential so I will not be able to share it .
          You don't have to share the real data--the actual values of the data are usually not important for the purposes of seeing what is going on and developing and testing code. The things that are needed are the structure and layout of the data, the storage types, labels and other metadata. When you need help with code, not sharing data decreases the chance that anybody will respond, decreases the chance that their response will be helpful if they do, and is likely to waste your time. So if your data are confidential, the thing to do is to open your data set in Stata. Then in the data editor, overwrite the real data with fake numbers (or strings, as the case may be). Just be sure that things that are the same in the real data remain the same in the fake data. Then run -dataex- on the fake data and post that.

          Comment


          • #6
            Thank you I will try and exercise this .

            Comment

            Working...
            X