Announcement

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

  • Multiplying of several variables with same prefix

    Dear statalists,

    I would really appreciate your help. I'm currently analysing data for my master thesis and I've experienced above mentioned problem.

    A small part of my sample looks as following:
    produkt_hilfe1 produkt_hilfe2 produkt_hilfe3 produkt_hilfe4 produkt_hilfe5 produkt_hilfe6
    1.048171 1.066638 1.049848 1.027627 1.076792 1.096686
    1.048171 1.066638 1.049848 1.027627 1.076792 1.096686
    1.048171 1.066638 1.049848 1.027627 1.076792 1.096686
    1.048171 1.066638 1.049848 1.027627 1.076792 1.096686
    1.048171 1.066638 1.049848 1.027627 1.076792 1.096686

    What I want to do is multypling each observation with corresponding observation of the other variables like this:
    gen varibale = produkt_hilfe1 * produkt_hilfe2 * produkt_hilfe3
    gen variable2 = produkt_hilfe2 * produkt_hilfe3 * produkt_hilfe4

    and so on...

    For this small part it's no problem to do this manually, but my dataset consists of 360 of such variables.
    Unfortunately I can't found any solution for this problem on this page, in the pre_april 2014 archievs or somewhere else in the net. Furthermore my many tries using different loop-variations didn't work out.

    So to subsume my problem, I would like to multiply observations of a particular range of variables.

    Like I wrote I would really appreciate your advice and thanks in advance for any help.

    Kind regards,
    Martin

  • #2
    Martin,

    assuming that your variables range from 1-360 and do not change the prefix, try this:

    Code:
    forvalues i = 0/357 {
    local j1 = `i' + 1
    local j2 = `i' + 2
    local j3 = `i' + 3
    gen variable`i' = produkt_hilfe`j1' * produkt_hilfe`j2' * produkt_hilfe`j3'
    }

    Comment


    • #3
      Thanks a lot Sebastian, this works great for me. Seems like this solution is to easy that I could'nt find anything corresponding. Nevertheless it is a huge help.
      Thanks again.

      Comment

      Working...
      X