Announcement

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

  • Loop gen command with *suffix

    Hi, I want to create a loop command to generate quotients (number of cases divided by number at risk) for multiple variables ending in "cases" and "at_risk". If I only had one set of variables, I would use:

    Code:
    gen quotient_countryA= countryA_cases / countryA_at_risk
    However, the data will be continuously updated with many more countries so an automated method is preferable. Is it possible to create a loop command that essentially does the following?

    Code:
    gen quotient_country*= *country_cases / *country_at_risk

    Thanks in advance!


  • #2
    Code:
    // create some sill example data
    clear all
    set obs 100
    foreach c in ROU RUS RWA SAU SDN SEN SGP {
        gen country`c'_cases = rpoisson(10)
        gen country`c'_at_risk = rpoisson(20)
    }
    desc
    
    // try to find the country names from only the dataset
    // that way you don't have to manually enter that later on
    
    // first step: get a list of all variable names starting with "country"
    // end ending with "_cases"
    unab countries : country*_cases
    di `"`countries'"'
    
    // second step: from each variable name in the list remove "country"
    local countries : subinstr local countries "country" "", all
    di `"`countries'"'
    
    // third step: from each variable name in the list remove "_cases"
    local countries : subinstr local countries "_cases" "", all
    di `"`countries'"'
    
    // Now you can loop over the countries and create your quotient
    foreach c of local countries {
        gen quotient_country`c' = country`c'_cases / country`c'_at_risk
    }
    Note, that this solution makes heavy use of local macros, so you need to run this in one go. Running it line by line will not work, as at the end the local macro that is need for the next line will disappear.
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      dryrun and r of rename are born for serving this issue.
      Code:
      ren (country*_cases) (*), dryrun r
      foreach c in `r(newnames)' {
      gen quotient_country`c' = country`c'_cases / country`c'_at_risk
      }

      Comment


      • #4
        I'm unclear as to what the `c' stands for. In my data set, the variable names look like - russia_cases -, - canada_cases -, etc. I'm hoping to be able to place the wildcard (*) before "cases" so the code matches each country's cases and number at risk to generate the quotient. For example:

        gen quotient_* = *_cases / *_at risk


        Sorry if I was unclear before. Thanks for your help!

        Comment


        • #5
          Like in your other post, a data example would really be very helpful here. So please post one now that it has been explained to you how to do this. One benefit is that code given by the people responding here will fit you data exactly. Romalpa's suggestion, for example, works with the variable names as you described them.

          Secondly, any loop in Stata will have a reference to values to loop over as for example
          Code:
          foreach element in somelist {
          gen `element' = 123
          }
          Please read following resource for more understanding of how loops function:
          https://www.theanalysisfactor.com/lo...g-coding-easy/
          https://www.stata.com/support/faqs/d...-with-foreach/
          https://www.stata.com/manuals/pforeach.pdf

          Comment

          Working...
          X