Announcement

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

  • Trying to replace a variable with a new generated variable

    Hello,
    I am trying to replace a variable so that when I create a graph for GDPpc it shows GDPpc below 20k and above 30k. the starting gdp variable is gdppc
    I used: 'generate code_gdppc2 = "" ' to make a string variable, then:
    replace code_gdppc2 = code_gdppc if gdppc < 20000

    However, when this is run it says 0 real changes made and I cannot for the life of me figure out why.

    or to get my desired result I run:

    'replace code_gdppc2 = code_gdppc if gdppc < 20000 & gdppc > 30000'

    and this says 'code_gdppc ambiguous abbreviation'
    what is the way around this?

    Apologies if this is classed as an elementary question, I am new here and have no other place to turn for an answer.
    Thank you.

  • #2
    A data example would be useful here - it's not really clear to me what the data looks like or what's causing the problem (particularly the code_gdppc variable). You can provide a data example by using the -dataex- command in Stata and pasting the output into the forum between [code] delimiters.

    Comment


    • #3
      I am not sure either.....
      I was told to create the new variable as 'code_gdppc' when learning

      also I am sorry but I am not sure what you mean by using the -dataex- command. stata tells me this is unrecognized.
      the mean for gdppc of 160 obs is 32365.64. I hope this can help, but I assume this is not what you mean by an example.

      sorry for the evident noobiness

      Comment


      • #4
        If you're using a version of Stata before 14.2 you need to install dataex from SSC before you can use it, which you can do by typing:

        Code:
        ssc install dataex
        dataex

        Comment


        • #5
          ok thanks, i got this for the variable gdppc:
          Code:
          * Example generated by -dataex-. To install: ssc install dataex
          clear
          input float gdppc
           31010.56
          36186.297
          37873.516
          13442.896
          19053.975
          35269.254
           38247.39
           23425.33
          37555.773
           72325.46
           22882.49
          37472.348
           49313.43
           39066.07
          39050.383
           23017.41
          16351.788
           41825.84
           32085.58
           82480.42
           44289.63
           13192.96
           23559.57
          26223.055
          34603.145
           19588.87
           29734.87
          21193.676
           49121.97
          33087.715
          24531.113
           50069.02
          34787.105
           65324.16
           56206.96
           45591.65
           39669.91
           43958.76
           35340.15
            21151.3
            51812.8
           40328.89
          34996.746
           42293.39
           27335.42
          35118.273
          25898.336
           56311.71
          17579.547
          20998.406
           45320.19
          12292.755
           27110.67
           47665.27
          29699.033
          37056.695
           29992.85
           26163.98
           25710.05
           29587.87
           30648.85
           27895.58
           36210.06
          27064.465
          24460.375
           29922.96
           39644.54
           57050.86
          11016.222
          31167.344
          34729.613
           49661.71
           34514.97
           39063.94
           45442.02
          35837.906
           39468.19
          17534.592
           33848.24
           43279.79
            26053.2
          26672.535
          27994.623
          33271.848
           37300.19
           26032.73
           24125.22
           29883.59
          14958.278
           20790.46
           22634.11
           22519.94
          11500.647
          24179.855
          34059.563
          23543.295
           59323.56
          24968.955
           19269.07
           25329.38
          end

          Comment


          • #6
            Could you also show the values of the code_gdppc variable which you're trying to replace code_gdppc2 with?

            Comment


            • #7
              Maybe this is where I am going wrong, as code_gdppc is a string and just gives rows of: " " when I run the dataex on it.
              " "

              Comment


              • #8
                The difficulty is over (abbreviation of) variable names, So, the results of


                Code:
                describe code_gdppc*
                should be given.

                https://www.statalist.org/forums/help#stata explains about data examples in general and dataex in particular.

                while https://www.statalist.org/forums/help#version explains that you should tell us about an out-of-date version.

                Comment


                • #9
                  Okay, so now your problem makes more sense. When you use the command:

                  Code:
                  replace code_gdppc2 = code_gdppc if gdppc < 20000 & gdppc > 30000
                  You're replacing one variable which is full of empty strings "" (code_gdppc2) with another variable which is full of empty strings (code_gdppc). So there are no changes made because the variables are already identical. Also, it's impossible for gdppc to be both below 20000 and above 30000 at the same time, so the condition of gdppc < 20000 & gdppc > 30000 will never be fulfilled.

                  I'm not sure what you're trying to achieve with the string variables, but if what you want is a variable which only contains gdppc when gdppc is lower than 20000 or higher than 30000, you can just do:

                  Code:
                  gen wanted = gdppc if gdppc < 20000 | gdppc > 30000
                  This variable will be missing when gdppc is between 20000 and 30000.

                  Comment


                  • #10
                    Okay this helps thank you. I have now created the desired graph but the 'wanted' mlabel results in the actual gdp number being put on the graph and not the name of the country which corresponds to that gdp at that point in time.
                    Do you know how I can mlabel the country name within these ranges and not their gdp?

                    Thanks

                    Comment


                    • #11
                      Originally posted by Nick Cox View Post
                      The difficulty is over (abbreviation of) variable names, So, the results of


                      Code:
                      describe code_gdppc*
                      should be given.

                      https://www.statalist.org/forums/help#stata explains about data examples in general and dataex in particular.

                      while https://www.statalist.org/forums/help#version explains that you should tell us about an out-of-date version.
                      Thanks Nick Cox, I will be sure to refer to these resources in future statalist posts.

                      Comment


                      • #12
                        Unless you have solved your problem showing us the results of

                        Code:
                         
                         describe code_gdppc*
                        remains a concrete suggestion for here and now.

                        Comment

                        Working...
                        X