<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Creating datasets and inserting data based on a condition during execution in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Creating-datasets-and-inserting-data-based-on-a-condition-during/m-p/608746#M177190</link>
    <description>&lt;P&gt;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&lt;BR /&gt;internet_1a,internet_1b,internet_1c,internet_1d,internet_1e,internet_1f with same structure that contains two columns ordercode and year.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have the sample sql code that generates this.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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
;

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;for rec in select * from table1 where ordertype='internet' loop&lt;BR /&gt;execute format('create table %I(ordercode varchar(15),year varchar(4))',rec.ordercode);&lt;BR /&gt;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);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Example of output table: Internet_1a is created with data&lt;/P&gt;&lt;P&gt;order_code year&lt;/P&gt;&lt;P&gt;north 2012&lt;/P&gt;&lt;P&gt;north 2013&lt;/P&gt;&lt;P&gt;north 2014&lt;/P&gt;&lt;P&gt;north 2015&lt;/P&gt;&lt;P&gt;north 2016&lt;/P&gt;&lt;P&gt;north 2017&lt;/P&gt;&lt;P&gt;north 2018&lt;/P&gt;&lt;P&gt;south 2012&lt;/P&gt;&lt;P&gt;south 2013&lt;/P&gt;&lt;P&gt;south 2014&lt;/P&gt;&lt;P&gt;south 2015&lt;/P&gt;&lt;P&gt;south 2016&lt;/P&gt;&lt;P&gt;south 2017&lt;/P&gt;&lt;P&gt;..&lt;/P&gt;&lt;P&gt;..&lt;/P&gt;&lt;P&gt;..&lt;/P&gt;</description>
    <pubDate>Mon, 02 Dec 2019 15:53:53 GMT</pubDate>
    <dc:creator>rkarr5</dc:creator>
    <dc:date>2019-12-02T15:53:53Z</dc:date>
    <item>
      <title>Creating datasets and inserting data based on a condition during execution</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-datasets-and-inserting-data-based-on-a-condition-during/m-p/608746#M177190</link>
      <description>&lt;P&gt;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&lt;BR /&gt;internet_1a,internet_1b,internet_1c,internet_1d,internet_1e,internet_1f with same structure that contains two columns ordercode and year.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have the sample sql code that generates this.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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
;

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;for rec in select * from table1 where ordertype='internet' loop&lt;BR /&gt;execute format('create table %I(ordercode varchar(15),year varchar(4))',rec.ordercode);&lt;BR /&gt;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);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Example of output table: Internet_1a is created with data&lt;/P&gt;&lt;P&gt;order_code year&lt;/P&gt;&lt;P&gt;north 2012&lt;/P&gt;&lt;P&gt;north 2013&lt;/P&gt;&lt;P&gt;north 2014&lt;/P&gt;&lt;P&gt;north 2015&lt;/P&gt;&lt;P&gt;north 2016&lt;/P&gt;&lt;P&gt;north 2017&lt;/P&gt;&lt;P&gt;north 2018&lt;/P&gt;&lt;P&gt;south 2012&lt;/P&gt;&lt;P&gt;south 2013&lt;/P&gt;&lt;P&gt;south 2014&lt;/P&gt;&lt;P&gt;south 2015&lt;/P&gt;&lt;P&gt;south 2016&lt;/P&gt;&lt;P&gt;south 2017&lt;/P&gt;&lt;P&gt;..&lt;/P&gt;&lt;P&gt;..&lt;/P&gt;&lt;P&gt;..&lt;/P&gt;</description>
      <pubDate>Mon, 02 Dec 2019 15:53:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-datasets-and-inserting-data-based-on-a-condition-during/m-p/608746#M177190</guid>
      <dc:creator>rkarr5</dc:creator>
      <dc:date>2019-12-02T15:53:53Z</dc:date>
    </item>
    <item>
      <title>Re: Creating datasets and inserting data based on a condition during execution</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-datasets-and-inserting-data-based-on-a-condition-during/m-p/608992#M177283</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/177633"&gt;@rkarr5&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can try this. It generate one dataset name according to the ordercode and containing 2 columns: year (with 1 observation per year from 2012 to 2018) and code.&lt;/P&gt;
&lt;P&gt;I am not sure if this is what you want, as your entry data are very weird: lot of duplicate records and there is no rule to link codes "north" or "south" with Internet_1a.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* List of tables to be created into macrovariables */

proc sql noprint;
	select distinct ordercode into: ordercode_1-:ordercode_999  from table1;
	select count(distinct ordercode) into: nb_ordercode from table1;
run;

/* Retrieve each couple of ordercode-code */
proc sort data=table1 out=table1_nodupkey (keep=ordercode code) nodupkey;
	by ordertype ordercode code name;
run;

/* Output 1 row / year for each couple ordercode-code - Method 1*/
data table2;
	set table1_nodupkey;
	do year=2012 to 2018;
		output;
	end;
run;

%macro ordercode_tables();
	%do i=1 %to &amp;amp;nb_ordercode;
		data &amp;amp;&amp;amp;ordercode_&amp;amp;i (drop =ordercode);
			set table2;
			where ordercode = "&amp;amp;&amp;amp;ordercode_&amp;amp;i";
		run;
	
		*title "&amp;amp;&amp;amp;ordercode_&amp;amp;i";
		*	proc print data=&amp;amp;&amp;amp;ordercode_&amp;amp;i;
		*	run;
		*title;
	%end;
%mend;

%ordercode_tables;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 03 Dec 2019 09:35:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-datasets-and-inserting-data-based-on-a-condition-during/m-p/608992#M177283</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2019-12-03T09:35:16Z</dc:date>
    </item>
  </channel>
</rss>

