I use almost the same idea as Jim, but I like separating the group collection and creation into their own loops
Names Default To Here(1);
dt1 = New Table("Table1",
Add Rows(3),
New Column("A", Numeric, "Continuous", Format("Best", 12), Set Values([35, 10, 88])),
New Column("B", Numeric, "Continuous", Format("Best", 12), Set Values([2, 3, 2])),
New Column("C", Numeric, "Continuous", Format("Best", 12), Set Values([1, 3, 1])),
New Column("D", Numeric, "Continuous", Format("Best", 12), Set Values([2, 2, 2]))
);
dt2 = New Table("Table2",
Add Rows(4),
New Column("Name", Character, "Nominal", Set Values({"A", "B", "C", "D"})),
New Column("GroupName", Character, "Nominal", Set Values({"G1", "G1", "G2", "G2"}))
);
aa_groups = Associative Array(Column(dt2, "GroupName"));
For Each({key}, aa_groups << get keys,
aa_groups[key] = dt2 << Get Rows Where(:GroupName == key);
);
For Each({{name, cols}}, aa_groups,
dt1 << Group Columns(name, cols);
);
-Jarmo