Announcement

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

  • Multiplying Macros to Generate New Variables

    Hi all and thanks in advance for your help,
    I'm just getting back into Stata and have what I hope is an easy question to answer for you veterans (I've looked around in the archives and couldn't find anything that addresses this type of situation).

    I am trying to multiply two local macros and generate new variables for each interaction. For clarity, saleq contains the quantity sold for each product and the corresponding selling value is contained in salev. I was thinking something like what I have below with a couple example variables built into the macros would do the trick, while naming each of the newly generated variables with the suffix inc added to the basename from saleq.

    Code:
    local saleq ibks ibms iirs inos
    local salev ibksv ibmsv iirsv inosv
    quietly foreach v of `saleq' {
    foreach z of `salev' {
    gen `v'inc = `v' * `z'
    }
    }
    Any insights on how to do this would be much appreciated.
    Bryan Gensits
    Royal University of Bhutan: College of Natural Resources

  • #2
    You are not really multiplying macros. You want to multiply variables whose names are held in macros. Still, that is just wording.

    Two more substantial problems arise here. First, you really have one loop, not two. Think that you want 4 results, not 4 x 4. Second, the keyword of in the foreach syntax is used wrongly.

    Something more like this should work.

    Code:
    local saleq ibks ibms iirs inos
    quietly foreach v of local saleq {
        gen `v'inc = `v' * `v'v 
    }
    This would be fine. Your example doesn't to me imply that you need to use macros at all.

    Code:
    quietly foreach v in  ibks ibms iirs inos {
        gen `v'inc = `v' * `v'v 
    }






    Comment


    • #3
      Hi Nick, thanks again for the help, your first suggestion is exactly what I was trying to do. I already had the macros for selling quantities and values built for other calculations since there are nearly 150 products and associated prices for each observation so it was easier to just keep running with them.

      A small follow up question: in the example I gave you the quantity and value variables happened to have similar names so I was able to get by with the handy gen `v'inc = `v' * `v'v solution you suggested. In general, if I have these two macros:

      Code:
      local ex1 a b c d
      local ex2 w x y z
      where the contained variables are in complimenting positions and have unrelated names (eg. b is related to x). Is there a foreach command using the macros ex1 and ex2 that I could use to generate the 4 results that the following generates manually? (you were correct in assuming the 4 x 4 was not what I was trying to achieve):

      Code:
      gen t1 = a * w
      gen t2 = b * x
      gen t3 = c * y
      gen t4 = d * z
      I really appreciate your help.
      Last edited by Bryan Gensits; 05 Jul 2018, 06:15.
      Bryan Gensits
      Royal University of Bhutan: College of Natural Resources

      Comment


      • #4
        That's programmable. No data example, so this isn't tested.

        Code:
        local j = 1
        local Y w x y z
        local X a b c d
        
        foreach x of local X {
              gettoken y Y : Y
              gen t`j' = `x' * `y'
              label var t`j' "`x' * `y'"
              local ++j
        }
        Or like this:

        Code:
        forval j = 1/4 { 
             local X : word `j' of w x y z 
             local Y: word `j' of a b c  d 
             gen t`j' = `X' * `Y' 
             label var t`j'  "`X' * `Y'" 
        }
        Last edited by Nick Cox; 05 Jul 2018, 07:17.

        Comment


        • #5
          Thank you Nick, that is exactly what I was trying to do and works perfectly with my dataset. .
          Bryan Gensits
          Royal University of Bhutan: College of Natural Resources

          Comment

          Working...
          X