Dear forum users,

I have a programming problem when setting up a macro and would like to ask your help. Let's assume I have a macro with a set up variables:

Code:
local vlist var1 var2 var3 var4
I would like to set up a macro that creates all possible 2nd, 3rd order polynomials and all possible interactions (one way, two-way, third-way,...) between these variables. In the particular example above I would like to get a macro:

Code:
local list var1#var1 var2#var2 var3#var3 var4#var4 // 2nd order poly
var1#var1#var1 var2#var2#var2 var3#var3#var3  var4#var4#var4    // 3rd order poly 
var1#var2  var1#var3  var1#var4 var2#var3  var2#var4 var3#var4  // two-way inter
var1#var2#var3  var1#var2#var4  var2#var3#var4  var1#var3#var4  // three-way inter
Later I am going to use this macro in lasso regressions to make a selection. The list of variables is quite long (and could potentially change in the future), therefore I want to find the most efficient and automated way to create this macro without generating hundreds of new variables. I started writing this code but was unable to get all interactions, maybe it will be a good head start.

Code:
local vlist "var1 var2 var3 var4"
local count: word count `vlist'  

* create 2nd & 3rd order polynomials    
local series1 = 1
local series2 = 2
foreach var in `vlist' {
    local order1 : di _dup(`series1') "#`var'"
    local poly1     "`poly1' `var'`order1'"
    
    local order2 : di _dup(`series2') "#`var'"
    local poly2     "`poly2' `var'`order2'"
    }
    
* create all possible interactions    
forval i = 1/`count' {
    gettoken `i' vlist : vlist
    foreach j of local vlist {
        local inter "`inter' ``i''#`j'"
    }
}
di "POLYNOMIALS:"
di "`poly1' `poly2'"

di "INTERACTIONS"
di "`inter'"

* the macro of interest:
global ilist `poly1' `poly2' `inter'

Could someone help me creating this macro?

Thank you very much in advance for your time and your interest in my problem.

Wishes,

Eva

P.S. I use Stata 16 version.