Announcement

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

  • Displaying country names in the loop

    Hi..
    I have a loop that works fine. The loop runs over several countries. Since the estimation results are displayed one by one country, it is difficult to know which set of results belongs to which country. So, I want to add a line that will display the country name.

    The code is the following:

    Code:
    foreach i of numlist 1/100 {
    di  "id=`i'"
    local country_name = country[1]
    di "country name = `country_name'"
    dfuller x, drift lags(3) regress, if id==`i'
    }
    *
    The local used in the above code is not working properly. It displays the first country's name (in my dataset) for all countries. There's is a 1:1 correspondence between the country name and id.

    Kindly help.

  • #2
    The code fails because you are always referring to country[1], regardless of the value of `i'.

    While this can be done with native Stata commands, it is somewhat roundabout to do this because country is a string variable. In this case, it is simpler to do it with -runby-, which breaks the data set up into pieces, so that only one country's data are in memory at any given time. I assume there is a 1-1 correspondence between id and country in your data.

    Code:
    capture progam drop one_country
    program define one_country
        local country_name = country[1]
        local id = id[1]
        di "id = `id', country name = `country_name'"
        dfuller x, drift lags(3) regress
        exit
    end
    
    runby one_country, by(country) verbose
    -runby- is written by Robert Picard and me, and is available from SSC. In this case the -verbose- option is mandatory because -runby- will otherwise suppress output. -runby- is usually run with programs that save their results by modifying the data set, rather than writing to the output (screen or log file).



    Comment


    • #3
      Another way to do it

      Code:
      gen long ID = _n  
      forval i = 1/100 {    
          su ID if id == `i', meanonly      
          local country_name = country[r(min)]    
          di "country name = `country_name'"    
          dfuller x if id == `i', drift lags(3) regress
      }
      and another

      Code:
      egen countryid = group(country), label  
      su countryid, meanonly  
      
      forval i = 1/`r(max)' {          
           local country_name : label (countryid) `i'
           di "country name = `country_name'"      
           dfuller x if countryid == `i', drift lags(3) regress  
      }

      Last edited by Nick Cox; 05 Feb 2020, 12:28.

      Comment


      • #4
        Clyde Schechter Thank you for the solution. I am familiar with "runby". Thank you for that powerful command. But I did not know how to display output/results in the Stata.
        With regard to the solution, I have one doubt. Your code runs the program for all countries. Suppose, i want to run the command for a subset of countries, say 1 to 10, or say 15 to 25. How to modify the code that will run the code for those subset of countries?

        Comment


        • #5
          Nick Cox Thank you Sir. I have a question on your Code 2. That code runs the program for all countries. However, if I want to run the command for a subset of countries, say 1 to 10, or say 15 to 25, how to modify the code that will run the code for those subsets of countries?

          Comment


          • #6
            Well -runby- doesn't accept -if- or -in- options, so before -runby- you would need to -drop- observations whose id's are not in the desired range. (If you need to get them back later, -preserve- before that and -restore- after you get your results.)

            Comment


            • #7

              Okay. Thank you for this clarification.

              Comment


              • #8
                #5 Adjust the loop limits accordingly, say

                Code:
                 
                 forval i = 15/25

                Comment

                Working...
                X