This is my test data and for every order type I want to create different tables with same structure and insert data into it from other tables. For example, for 'Internet' order type I want to create different tables for order code internet_1a,internet_1b,internet_1c,internet_1d,internet_1e,internet_1f with same structure that contains two columns ordercode and year. I have the sample sql code that generates this. data table1;
input ordertype $ ordercode $ code $ name $;
datalines;
internet internet_1a google_ad google
internet internet_1a google_ad google
internet internet_1b website target
internet internet_1b website target
internet internet_1c facebook facebook
internet internet_1c facebook facebook
internet internet_1d twitter twitter
internet internet_1d twitter twitter
internet internet_1e instagram instagram
internet internet_1e instagram instagram
internet internet_1f other other
app app_2a north north
app app_2a north north
app app_2b south south
app app_2b south south
app app_2c east east
app app_2c east east
app app_2d west west
app app_2d west west
app app_2e central central
app app_2e central central
app app_2f other other
app app_2f other other
;
data test1;
input year $;
datalines;
2012
2012
2013
2014
2015
2015
2016
2017
2018
;
for rec in select * from table1 where ordertype='internet' loop execute format('create table %I(ordercode varchar(15),year varchar(4))',rec.ordercode); execute format('insert into %I(ordercode,year) select b.ordercode,e.year from(select * from table1 where ordertype='app') b, (select distinct year from test1) e',rec.ordercode); Example of output table: Internet_1a is created with data order_code year north 2012 north 2013 north 2014 north 2015 north 2016 north 2017 north 2018 south 2012 south 2013 south 2014 south 2015 south 2016 south 2017 .. .. ..
... View more