Announcement

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

  • Changing graph title in the loop

    Dear Stata users,

    I want the value label (country name) to be the title of the graph, and I use the following code. Unfortunately it does not work. I get invalid syntax error.
    Any help is appreciated

    Variable "country" takes values 1-10

    label list
    country:
    1 Austria
    2 Belarus
    3 Switzerland
    4 Czech
    5 Denmark
    6 Germany
    7 France
    8 Poland
    9 Slovakia
    10 UK


    forval x = 1(1)10 {
    local t : label (country) `x' ///
    twoway (sc coef species if (zmienna1==1 & country==`x'), lwidth(thick) mcolor(navy) lcolor(navy) connect(direct)) ///
    (rcap ci_low ci_up species if (zmienna1==1 & country==`x'), ///
    lcolor(maroon)), legend(lab(1 "WTT in km") lab(2 "WTT 95% CI")) ///
    title ("`t'") ///
    xlabel( 2 "2 species" 3 "3 species" 4 "4 species")
    graph save C:\Users\marek\Desktop\mygraph1\c_`x'.gph, replace
    }

    Thanks

  • #2
    It's not obvious what's going wrong, but here's one thought. The construction -:label (country) `x'- refers to whatever label is attached to the variable named country. It does not necessarily refer to the label named country. Have you checked that your label country is actually attached to variable country? It is easy to forget to do that. Just run -des country- to see if it shows a value label called country. If not, -label values country country- should fix your problem.

    Comment


    • #3
      Thank you for your answer, but it looks that the label has been attached correctly


      . des country

      storage display value
      variable name type format label variable label
      --------------------------------------------------------------------------------------------------------------------------------------------------------------
      country byte %11.0g country country

      Comment


      • #4
        des country
        storage display value
        variable name type format label variable label
        ---------------------------------------------------------------------------------------------------------------------------------------------
        country byte %11.0g country country

        Comment


        • #5
          Please post a sample of your data, with at least a couple of different countries included. Use the -dataex- command to do that. I'll experiment with the code and see if I can troubleshoot it.

          If you are running version 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


          • #6
            Here it is. Thank you!

            input int lp byte(zmienna1 species) float(coef sterr) byte country float(ci_low ci_up)
            17 0 0 30.9505 .4484 1 30.07 31.83
            18 0 0 -5.1097 .9531 1 -6.98 -3.24
            19 0 0 2.0894 .6859 1 .74 3.43
            20 0 0 -.362 1.3389 1 -2.99 2.26
            21 1 2 3.3525 1.0492 1 1.3 5.41
            22 1 3 12.7824 .8671 1 11.08 14.48
            23 1 4 7.5015 .7276 1 6.08 8.93
            24 0 0 .8979 .0617 1 .78 1.02
            25 0 0 2.0783 .6954 1 .72 3.44
            26 0 0 9.948 .7145 1 8.55 11.35
            27 0 0 5.0353 .4806 1 4.09 5.98
            28 0 0 3.7806 .476 1 2.85 4.71
            29 0 0 -4.0638 .6737 1 -5.38 -2.74
            30 0 0 4.1828 .5555 1 3.09 5.27
            31 0 0 7.2155 .5865 1 6.07 8.37
            32 0 0 16.8438 1.315 1 14.27 19.42
            33 0 0 -15.8535 .2257 2 -16.3 -15.41
            34 0 0 4.2698 .3623 2 3.56 4.98
            35 0 0 4.3644 .5832 2 3.22 5.51
            36 0 0 7.1525 .6363 2 5.91 8.4
            37 1 2 5.9523 .409 2 5.15 6.75
            38 1 3 2.7512 .3467 2 2.07 3.43
            39 1 4 3.8216 .5483 2 2.75 4.9
            40 0 0 .3696 .0465 2 .28 .46
            41 0 0 -3.3817 .4485 2 -4.26 -2.5
            42 0 0 .812 .5203 2 -.21 1.83
            43 0 0 5.7224 .5665 2 4.61 6.83
            44 0 0 .4733 .4627 2 -.43 1.38
            45 0 0 11.1266 .4035 2 10.34 11.92
            46 0 0 9.3683 .5184 2 8.35 10.38
            47 0 0 13.5515 .5116 2 12.55 14.55
            48 0 0 17.0845 1.4778 2 14.19 19.98
            49 0 0 11.7345 .0545 3 11.63 11.84
            50 0 0 -2.0149 .1745 3 -2.36 -1.67
            51 0 0 -5.6052 .1585 3 -5.92 -5.29
            52 0 0 2.4657 .1712 3 2.13 2.8
            53 1 2 -.0891 .1515 3 -.39 .21
            54 1 3 2.7293 .1528 3 2.43 3.03
            55 1 4 4.1471 .1181 3 3.92 4.38
            56 0 0 .8494 .0135 3 .82 .88
            57 0 0 2.4246 .0621 3 2.3 2.55
            58 0 0 3.782 .1375 3 3.51 4.05
            59 0 0 2.5693 .1257 3 2.32 2.82
            60 0 0 2.3593 .1303 3 2.1 2.61
            61 0 0 2.1666 .1349 3 1.9 2.43
            62 0 0 .3167 .1245 3 .07 .56
            63 0 0 1.449 .1467 3 1.16 1.74
            64 0 0 33.0227 1.7229 3 29.65 36.4
            65 0 0 36.2518 .8659 4 34.55 37.95
            66 0 0 -6.9411 1.384 4 -9.65 -4.23
            67 0 0 9.1338 .9859 4 7.2 11.07
            68 0 0 -8.2048 1.3365 4 -10.82 -5.59
            69 1 2 7.0836 .8213 4 5.47 8.69
            70 1 3 6.9487 .8371 4 5.31 8.59
            71 1 4 14.4235 .8656 4 12.73 16.12
            72 0 0 1.5413 .1008 4 1.34 1.74
            73 0 0 .438 .8975 4 -1.32 2.2
            74 0 0 .4692 .9282 4 -1.35 2.29
            75 0 0 7.1495 .5461 4 6.08 8.22
            76 0 0 -.4084 .7034 4 -1.79 .97
            77 0 0 -7.3531 .8639 4 -9.05 -5.66
            78 0 0 4.9139 .7966 4 3.35 6.48
            79 0 0 5.701 .7994 4 4.13 7.27
            80 0 0 10.2357 1.1671 4 7.95 12.52
            81 0 0 25.6455 .3427 5 24.97 26.32
            82 0 0 .5306 .496 5 -.44 1.5
            83 0 0 -1.1378 .4819 5 -2.08 -.19
            84 0 0 .9874 .5803 5 -.15 2.12
            85 1 2 5.8331 .4459 5 4.96 6.71
            86 1 3 7.519 .4887 5 6.56 8.48
            87 1 4 5.9442 .5126 5 4.94 6.95
            88 0 0 .9249 .0377 5 .85 1
            89 0 0 5.9216 .3796 5 5.18 6.67
            90 0 0 8.5413 .3112 5 7.93 9.15
            91 0 0 2.8466 .4107 5 2.04 3.65
            92 0 0 2.4053 .3584 5 1.7 3.11
            93 0 0 -.5306 .5251 5 -1.56 .5
            94 0 0 8.6429 .3701 5 7.92 9.37
            95 0 0 7.2376 .4047 5 6.44 8.03
            96 0 0 23.7395 1.5922 5 20.62 26.86
            97 0 0 21.1218 .2768 6 20.58 21.66
            98 0 0 1.9697 .4231 6 1.14 2.8
            99 0 0 2.2916 .6802 6 .96 3.62
            100 0 0 1.8428 .4895 6 .88 2.8
            101 1 2 7.3051 .4629 6 6.4 8.21
            102 1 3 8.1994 .4241 6 7.37 9.03
            103 1 4 6.5612 .4613 6 5.66 7.47
            104 0 0 .7099 .0292 6 .65 .77
            105 0 0 -1.0722 .367 6 -1.79 -.35
            106 0 0 5.1471 .2858 6 4.59 5.71
            107 0 0 4.7838 .2937 6 4.21 5.36
            108 0 0 8.0451 .2717 6 7.51 8.58
            109 0 0 2.2927 .352 6 1.6 2.98
            110 0 0 1.1469 .3274 6 .51 1.79
            111 0 0 7.7278 .371 6 7 8.45
            112 0 0 24.3821 1.5487 6 21.35 27.42
            113 0 0 31.6055 .6948 7 30.24 32.97
            114 0 0 -2.8482 1.2045 7 -5.21 -.49
            115 0 0 3.312 1.5314 7 .31 6.31
            116 0 0 2.9656 1.3045 7 .41 5.52
            end
            label values country country
            label def country 1 "Austria", modify
            label def country 2 "Belarus", modify
            label def country 3 "Switzerland", modify
            label def country 4 "Czech", modify
            label def country 5 "France", modify

            Comment


            • #7
              The first command defining the local should be separate. It is incorrectly connected to the next command with slashes.

              Comment


              • #8
                OK, there were several errors in the code. The first is that there should not be a /// after the -local t : …- command, as that leads Stata to think that what follows is a continuation of that command, and therefore Stata throws a syntax error. I should have spotted that one before--that was, in retrospect, obvious.

                Next, within the -twoway- command there were some misplaced parentheses that resulted in a (")" configuration--as a result of which the closing quote was not recognized, leading to another Stata error. Things like this are harder to spot by eye. I debugged it by taking things out and putting them back in one at a time to see what the problems were. The final version of the graph command that works is:
                Code:
                    twoway (sc coef species if (zmienna1==1 & country==`x'), lwidth(thick) mcolor(navy) lcolor(navy) connect(direct)) ///
                    (rcap ci_low ci_up species if (zmienna1==1 & country==`x'), ///
                    lcolor(maroon)), legend(lab(1 "WTT in km") lab(2 "WTT 95% CI")) ///
                    xlabel( 2 "2 species" 3 "3 species" 4 "4 species") title (`t')
                Added: Crossed with #7 which identifies one of the problems.

                Comment


                • #9
                  Thank you. Now I am getting "unmatched quote" error message.

                  forval x = 1(1)10 {
                  2. local t : label (country) `x'
                  3. twoway (sc coef species if (zmienna1==1 & country==`x'), lwidth(thick) mcolor(navy) lcolor(navy) connect(direct)) ///
                  > (rcap ci_low ci_up species if (zmienna1==1 & country==`x'), ///
                  > lcolor(maroon)), legend(lab(1 "WTT in km") lab(2 "WTT 95% CI")) ///
                  > title ("`t'") ///
                  > xlabel( 2 "2 species" 3 "3 species" 4 "4 species")
                  4. graph save C:\Users\marek\Desktop\mygraph1\c_`x'.gph, replace
                  5. }
                  unmatched quote
                  r(198);




                  Comment


                  • #10
                    You didn't use the code I suggested in #8. For reasons I do not understand, Stata will not accept -title("`t'")-. It will accept -title(`t')- with only the inner macro quotes. This may well be a bug.

                    Comment


                    • #11
                      Many thanks! Now it works. Thank you!

                      Comment

                      Working...
                      X