Announcement

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

  • Add country names to a loop (panel data)

    Hello,

    I'm working with a panel data (210 countries) and I'm trying to draw for each country its own name. I got some assistance working out a loop but I wanted to add the countries names on each graph. I did create 'countryid' column to assign each country a specific number but I don't how to add the names on each graph.

    Here's the command I'm using.

    Code:
    forvalues i = 1/210 {
        
        tsline cycle trd if countryid == `i' & year >= 1985 & year <= 2020, legend(off)
        
        graph save "Graph" "`i'.gph", replace
        graph export "`i'.jpg", as(jpg) name("Graph") quality(90) replace
    }
    Thank you in advance.

    Edit: added the proper layout for my code
    Last edited by Anass Wajib; 18 Mar 2024, 07:36.

  • #2
    If you have a variable containing the country names (named "countryname" below):

    Code:
    forvalues i = 1/210 {
        levelsof countryname if countryid==`i', local(name)
        tsline cycle trd if countryid == `i' & year >= 1985 & year <= 2020, legend(off) title("`name'")

    Comment


    • #3
      Andrew Musau Thank you for your input but I didn't work out for me.

      Here's my code (I replaced countryname with a country column since it has the name of the countries)

      Code:
      forvalues i = 1/210 {
          levelsof country if countryid==`i', local(name)
          tsline cycle trd if countryid == `i' & year >= 1985 & year <= 2020, legend(off) title("`name'")
      }
      plus it ives me this error r(132) that reads: too few quotes

      Comment


      • #4
        Probably you have compound double quotes because of spaces in the country names. Use the -clean- option of levelsof or exclude the double quotes in the -title()- option of tsline.

        Code:
        forvalues i = 1/210 {
            levelsof country if countryid==`i', local(name) clean
            tsline cycle trd if countryid == `i' & year >= 1985 & year <= 2020, legend(off) title("`name'")
        }

        Comment


        • #5
          Andrew Musau Thank you very much! It worked. Thank you for your time.

          Comment

          Working...
          X