Announcement

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

  • Creating and Saving graphs using loop

    Dear Statalist Members,
    I am working on a panel data set. I want to create graphs and save graphs by the country name.

    If I use the following loop, it is creating graphs for all countries in my panel dataset. But it saves graphs by id such as 1.gph, 2.gph..... and so on.

    levels country, local(countries)
    local i = 1
    foreach c of local countries {
    twoway (line x y year if country=="`c'", title("`c'") ytitle(% of GDP) xtitle(Year))
    graph save `i'.gph, replace
    local i = `i' + 1
    }
    *

    However, I WOULD LIKE TO SAVE GRAPHS BY COUNTRY NAME.

    levels country, local(countries)
    local i = 1
    foreach c of local countries {
    twoway (line x y year if country=="`c'", title("`c'") ytitle(% of GDP) xtitle(Year))
    graph save `i'.gph, replace
    local i = `i' + 1
    }
    *

    This loop generates graphs and saves graphs IF THE COUNTRY NAME IS A SINGLE WORD. If the country name is India, the loop is fine. The loop stops working if the country name is MORE THAN 1 WORD such as United Kingdom or United States of America.

    Any help will be greatly appreciated.

    Thank you in advance.

  • #2
    Would -
    ​​​
    Code:
    graph save '=strtoname("'c'")'.gph, replace
    solve your problem?

    Comment


    • #3
      Yes . Thank you for your help.

      Comment


      • #4
        I believe you meant to write in your second loop
        Code:
        graph save `c'.gph, replace
        so that the file would be the country name. And every filename that contains embedded spaces needs to be surrounded in quotation marks, so you can solve your problem with
        Code:
        graph save "`c'.gph", replace
        or better yet, with compound quotes
        Code:
        graph save `"`c'.gph"', replace
        For example
        Code:
        . local c Virgin Islands
        
        . save `c'.dta, replace
        invalid 'Islands.dta' 
        r(198);
        
        . save "`c'.dta", replace
        file Virgin Islands.dta saved
        
        . save `"`c'.dta"', replace
        file Virgin Islands.dta saved

        Comment


        • #5
          Thank you, Sir. Both the codes worked. Thank you a lot

          Comment


          • #6
            Sir,
            Inspired by your example, I was wondering if it is possible to create country-wise data files from a panel data set.

            levels country, local(countries)
            local i = 1
            foreach c of local countries {
            keep if state=="`c'"
            save `c'.dta, replace
            local i = `i' + 1
            }
            *
            The loops runs successfully. But it is not creating data files for each country.
            I know that the line
            keep if state=="`c'"
            has the problem
            Kindly help.

            Comment


            • #7
              Building on Lisowski's suggestion, perhaps the problem is in the save-command. Have you tried:
              Code:
              save `"`c'.dta"', replace
              What happens when you run the loop? Does it not remove the irrelevant states, or does it not save the dataset?

              Comment


              • #8
                Thank you for your reply.
                Yes, I have already tried that.
                But it is saving the original panel data set with the country names. The key in that loop is how to create a new data set for each country. Saving the new country-wise data set is not a problem since you and William Lisowski have suggested the way.

                Comment


                • #9
                  I found a way out. It is correctly creating a data sheet for each state. Here's the code:

                  Code:
                  levels state, local(states)
                  local i = 1
                  Code:
                  foreach c of local states {
                    keep if state=="`c'"
                    save "`c'.dta", replace
                    use x.dta, clear
                    local i = `i' + 1
                  }
                  *
                  


                  The code may be simplified further.

                  Comment

                  Working...
                  X