Announcement

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

  • Dividing variables

    Hello everybody.

    I am trying to find a way to divide variables in a sequenced/ordered way.
    incomeusa incomechina incomecanada incomefrance limitusa limitchina limitcanada limitfrance
    6000 1265 3776 0 15 23 2 13
    4356 6785 3577 10089 18 4 34 11
    7643 4456 1322 678 9 5 5 24
    1087 365 0 778 8 8 14 11
    4456 44 5789 676 6 6 17 1
    I have to divide limit variables and income variables per country.

    I tried 3 different commands but none of them worked properly.

    1.Command: Don't create any new variable.

    foreach var of varlist incomeusa-incomefrance {
    foreach y of varlist limitusa-limitfrance {
    generate `var'dif = `y' / `var'
    }
    }

    incomeusadif already defined
    r(110);
    end of do-file

    2. Command: Create variables but all income variables are using just limitusa as numerator

    foreach var of varlist incomeusa-incomefrance {
    foreach y of varlist limitusa-limitfrance {
    generate `y'dif = `y' / `var'
    }
    }

    limitusadif already defined
    r(110);
    end of do-file


    3. Command: Tried to use the collon before the divison signal, but didnt worked either.

    foreach var of varlist incomeusa-incomefrance {
    foreach y of varlist limitusa-limitfrance {
    generate `y'dif = `y' :/ `var'
    }
    }

    limitusa: invalid name
    r(198);
    end of do-file

    Anyone has an idea how to perfom division between variables in a sequenced way?

    Thank you,
    Ricardo.





  • #2
    I think you want to retrieve the country name each iteration, and use that:

    Code:
    foreach var of varlist incomeusa-incomefrance {
            local country=subinstr("`var'","income","",1)
            gen diff`country'=limit`country'/income`country'
    }
    hth,
    Jeph

    Comment


    • #3
      It worked.
      Thank you!
      Ricardo.

      Comment


      • #4
        Hi, I have a slightly similar problem with this. I have variables Jan_13 to Mar_21, referring to months January 2013 to March 2021. I need to create new variables containing the results of current month/previous month. Ex. Feb_13_PR = Feb_13/Jan_13.

        How can I automate this computation for all months through looping? Thank you.

        Comment


        • #5
          #4 was repeated at https://www.statalist.org/forums/for...hrough-looping

          Comment

          Working...
          X