Announcement

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

  • updating data in stata

    I want to add a value for instance '45' for country 'BE' in year '2004' (the fist row of 2004) under variable 'goal'. What's the command?

  • #2
    Using Statalist is just like making wild mushroom risotto. If I asked you to make my favorite version of risotto, you'd likely need to know what the SPECIFIC ingredients are and the steps you do to make said dish.
    So far you've given neither, you're asking us for help to make a dish, and you've not given the recipe (the code) or ingredients (the data).

    Precisely, you must give your dataset using dataex and your code that you've tried (formatted accordingly). No dataset, no code=no real assistance.

    Comment


    • #3
      Possibly - just possibly - you mean you have an observation for where the string variable country has the value "BE" and the numeric variable year has the value 2004 for which you want to replace the value of the numeric variable goal with 45.

      If that is so, then
      Code:
      replace goal = 45 if country=="BE" and year==2004
      might do what you want.

      But if I've gotten any of the ingredients wrong, or have misunderstood what you expect the outcome to be, or if this was an oversimplified description of a more complex problem, you'll have to be more precise, as discussed in post #2.

      The basic approach to replacing values in existing data is the replace command, see the output of
      Code:
      help replace
      Another approach more suited to mass updates is the merge, update command.

      Comment


      • #4
        Thank you so much

        Comment


        • #5
          somehow it's giving me an error:

          replace tw = 48.453854 if country=="AT" and year==2006
          type mismatch
          r(109);

          Comment


          • #6
            there appear to be 2 errors here but without a -dataex- example, I can only point directly to 1: do not use "and" - you need "&" instead; other than that, it appears that either the variable country is actually numeric or the variable year is actually a string variable

            Comment


            • #7
              Charles Ngiendo you're not listening to me. I asked you specifically for a data example using dataex.

              William and Richard could only offer guesswork "Well, maybe it could be like this possibly" because you haven't supplied a data example such that we can actually inspect the problem. We need to see how your data looks (correctly formatted, using dataex), which you can do if you have anything above Stata 14.2 (I think).

              So I ask again, please, show us what your data look like. I promise this'll go so much quicker if you do that.

              Comment


              • #8
                My thanks to Rich for catching the error in my code, which was untested because no example data had been provided.

                As I wrote also in that post

                But if I've gotten any of the ingredients wrong, or have misunderstood what you expect the outcome to be, or if this was an oversimplified description of a more complex problem, you'll have to be more precise, as discussed in post #2.
                So if you haven't been able to make the replace command work for you using Rich's hints in post #6, do follow the advice and post a sample of your data using the dataex command.

                Descriptions of data are well-meant but insufficient to help those who want to help you. Even the best descriptions of data are no substitute for an actual example of the data. There are many ways your data might be organized that are consistent with your description, and each would require a somewhat different approach. In order to get a helpful response, you need to show some example data.

                Be sure to use the dataex command to do this. 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 and 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


                • #9
                  Code:
                  * Example generated by -dataex-. For more info, type help dataex
                  clear
                  input double(tw gdp)
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  48.266145 242348.3
                  end
                  label values tw tw2
                  label values gdp gdp1
                  ------------------ copy up to and including the previous line ------------------

                  Listed 100 out of 799858 observations
                  Use the count() option to list more

                  Comment


                  • #10
                    @ Jared Greathouse, I have followed your advice. I'm trying to insert only on the first year of the variables tw and gdp for country for example SE for a specific year ie 2007

                    Comment


                    • #11
                      is this enough?

                      Comment


                      • #12
                        Code:
                        * Example generated by -dataex-. For more info, type help dataex
                        clear
                        input int year long countries double(tw gdp)
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        2004 1 48.266145 242348.3
                        end
                        label values countries cnt
                        label def cnt 1 "AT", modify
                        label values tw tw2
                        label values gdp gdp1
                        ------------------ copy up to and including the previous line ------------------

                        Listed 100 out of 799858 observations
                        Use the count() option to list more

                        Comment


                        • #13
                          it's a huge data of millions of observations, 24 countries, years running from 2004 to 2019. tw and gdp are required so manually inserting them then populating them per the years for each country.

                          Comment


                          • #14
                            @William Lisowski, is it now clearer?

                            Comment


                            • #15
                              Notice from post #12 that your countries variable has numeric values, and is assigned the value label cnt to display these numbers as country codes rather than as their numeric value. So where you currently have
                              Code:
                              replace tw = 48.453854 if country=="AT" and year==2006
                              you should instead have
                              Code:
                              replace tw = 48.453854 if country==1 and year==2006
                              Or alternatively, and more reliably than doing it manually,
                              Code:
                              replace tw = 48.453854 if country=="AT":cnt and year==2006

                              Comment

                              Working...
                              X