<?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 Re: Dynamic SAS - Program Flow Help in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-SAS-Program-Flow-Help/m-p/302130#M64035</link>
    <description>&lt;P&gt;&lt;SPAN&gt;Hmm...I'm not certain I'm completely clear on your suggestion. In my QueryRules dataset every record is unique. &amp;nbsp;Rule_Nm, Rule, and Rule_Order are never repeated (see the attached file for a snippet of what the data looks like).&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Then I Set the QueryRules dataset 'by Rule_Order'. &amp;nbsp;So every record will then be both first.rule_order and last.rule_order? &amp;nbsp;I guess that is where I'm confused. Each CASE statement involves every record in the dataset. &amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Am I making this too hard? &amp;nbsp;What aren't I seeing?&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;		data _null_;
		   set QueryRules end=eof;
		   &lt;STRONG&gt;by Rule_Order;&lt;/STRONG&gt;
		   file '/home/ssbuechl/input_whens.sas';&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;BR /&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/13024i2C087391AB58B30C/image-size/large?v=1.0&amp;amp;px=600" border="0" alt="Capture.JPG" title="Capture.JPG" /&gt;</description>
    <pubDate>Mon, 03 Oct 2016 18:35:24 GMT</pubDate>
    <dc:creator>buechler66</dc:creator>
    <dc:date>2016-10-03T18:35:24Z</dc:date>
    <item>
      <title>Dynamic SAS - Program Flow Help</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-SAS-Program-Flow-Help/m-p/302104#M64018</link>
      <description>&lt;P&gt;Hi. I'm using dynamic sql to create a sas program with a CASE statuement containing many, many CASE WHEN statements. I use an array to produce each WHEN statement. The program flow is controlled by use of 'if _N_' and 'if EOF' statements to build the output sas file. &amp;nbsp;The 'if _N_' creates the top half of the program and the 'if EOF' creates the lower portion. &amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As it's written now the code works perfectly. Below is the code and the output (I've simplified the output to include only 2 CASE WHEN statements):&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;		data _null_;
		   set QueryRules end=eof;
		   by Rule_Order;
		   file '/home/ssbuechl/input_whens.sas';

			array rule_nm_array {1} $ 58 rule_nm;
			array rule_array {1} $ 65 rule;

		   &lt;STRONG&gt;if _n_&lt;/STRONG&gt;=1 then put 'proc sql;';
		   if _n_=1 then put 'create table QueryData&amp;amp;ZIP5 as ';
		   if _n_=1 then put 'select DISTINCT ';
		   if _n_=1 then put "     a.actual_dlvry_date, ";
		   if _n_=1 then put "     a.imb_code length = 31, ";
		   if _n_=1 then put "     a.imb_dlvry_zip_5, ";
		   if _n_=1 then put "     CASE";
			   do h = 1 to dim(rule_nm_array);
					do j = 1 to dim(rule_array);
							rule_nm=rule_nm_array{h};
							rule=rule_array{j};
						   PUT '           WHEN ('RULE') THEN trim("'RULE_NM'")';
					end;
			   end;
		   &lt;STRONG&gt;if eof&lt;/STRONG&gt; then put "     ELSE ' '";
		   if eof then put "     END as RULE_NM,";
		   if eof then put %nrstr("from QueryDataBase&amp;amp;ZIP5 as a ");
		   if eof then put %nrstr("inner join QueryDataBase&amp;amp;ZIP5 as b ");
		   if eof then put "on a.imb_code=b.imb_code ";
		   if eof then put "where a.source='A' and b.source='B'; ";
		   if eof then put "quit; ";
		run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table QueryData&amp;amp;ZIP5 as
select DISTINCT
     a.actual_dlvry_date as ad_dt,
     a.imb_code length = 31,
     a.imb_dlvry_zip_5,
     CASE
           WHEN ( A.ACTUAL_DLVRY_DATE IS NULL AND B.ACTUAL_DLVRY_DATE IS NOT NULL  ) THEN trim( "ACTUAL DELIVERY DATE MISSING IN IV " )
           WHEN ( A.ACTUAL_DLVRY_DATE &amp;gt; B.ACTUAL_DLVRY_DATE  ) THEN trim( "ACTUAL DELIVERY DATE LATER IN IV " )
     ELSE ' '
     END asRULE_NM length = 58&lt;BR /&gt;from QueryDataBase&amp;amp;ZIP5 as a
inner join QueryDataBase&amp;amp;ZIP5 as b
on a.imb_code=b.imb_code
where a.source='A' and b.source='B';
quit;                                                 &lt;/CODE&gt;&amp;nbsp;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, &lt;STRONG&gt;now I need to include an additional CASE statement&lt;/STRONG&gt; containing it's own WHEN statements. Since I only have program control using the&lt;STRONG&gt; 'if _N_'&lt;/STRONG&gt; and&lt;STRONG&gt; 'if EOF'&lt;/STRONG&gt; statements, the two CASE statements in the output sas file get all mixed together. &amp;nbsp;My &lt;U&gt;desired&lt;/U&gt; output would look like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table QueryData&amp;amp;ZIP5 as
select DISTINCT
     a.actual_dlvry_date as ad_dt,
     a.imb_code length = 31,
     a.imb_dlvry_zip_5,
     CASE
           WHEN ( A.ACTUAL_DLVRY_DATE IS NULL AND B.ACTUAL_DLVRY_DATE IS NOT NULL  ) THEN trim( "ACTUAL DELIVERY DATE MISSING IN IV " )
           WHEN ( A.ACTUAL_DLVRY_DATE &amp;gt; B.ACTUAL_DLVRY_DATE  ) THEN trim( "ACTUAL DELIVERY DATE LATER IN IV " )
     ELSE ' '
     END as RULE_NM length = 58,
     CASE
           WHEN ( A.ACTUAL_DLVRY_DATE IS NULL AND B.ACTUAL_DLVRY_DATE IS NOT NULL  ) THEN 1.0
           WHEN ( A.ACTUAL_DLVRY_DATE &amp;gt; B.ACTUAL_DLVRY_DATE  ) THEN 1.5
     ELSE .
     END as Rule_Order length = 5 format=4.1
from QueryDataBase&amp;amp;ZIP5 as a
inner join QueryDataBase&amp;amp;ZIP5 as b
on a.imb_code=b.imb_code
where a.source='A' and b.source='B';
quit; &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Is there some way form me to introduce the second CASE statement between the 'if _N_' and 'if EOF' program flow statements? Any suggestions would be greatly appreciated.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As a work-around I currently create 2 output sas files - one for the top half of the program and one for the botton half of the program. Then I 'X' out of the program and copy the two files together at an OS command line. &amp;nbsp;This is inefficient as it requires two separate passes over a very, very large dataset. I'd like to build the entire output sas file&amp;nbsp;it in a single pass if possible.&lt;/P&gt;</description>
      <pubDate>Mon, 03 Oct 2016 17:12:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-SAS-Program-Flow-Help/m-p/302104#M64018</guid>
      <dc:creator>buechler66</dc:creator>
      <dc:date>2016-10-03T17:12:45Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic SAS - Program Flow Help</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-SAS-Program-Flow-Help/m-p/302107#M64021</link>
      <description>&lt;P&gt;Your QueryRules dataset could be structed to include a variable that indicated which WHICH case statement is involved, maybe named RuleGroup&amp;nbsp; to follow your example and the Name of the variable to assign.&lt;/P&gt;
&lt;P&gt;Then the BY statement would be:&lt;/P&gt;
&lt;P&gt;By RuleGroup RuleOrder;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And use First.RuleGroup to start the group processing which would look something like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if First.RuleGroup then do:
   put "     CASE";
   do h = 1 to dim(rule_nm_array);
      do j = 1 to dim(rule_array);
         rule_nm=rule_nm_array{h};
         rule=rule_array{j};
         PUT '           WHEN ('RULE') THEN trim("'RULE_NM'")';
      end;
   end;
   put "     ELSE ' '";
   put "     END as " RULEVar ",";
end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;where RuleVar is the name of the target variable for the "rules" involved&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 03 Oct 2016 17:24:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-SAS-Program-Flow-Help/m-p/302107#M64021</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-10-03T17:24:16Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic SAS - Program Flow Help</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-SAS-Program-Flow-Help/m-p/302130#M64035</link>
      <description>&lt;P&gt;&lt;SPAN&gt;Hmm...I'm not certain I'm completely clear on your suggestion. In my QueryRules dataset every record is unique. &amp;nbsp;Rule_Nm, Rule, and Rule_Order are never repeated (see the attached file for a snippet of what the data looks like).&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Then I Set the QueryRules dataset 'by Rule_Order'. &amp;nbsp;So every record will then be both first.rule_order and last.rule_order? &amp;nbsp;I guess that is where I'm confused. Each CASE statement involves every record in the dataset. &amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Am I making this too hard? &amp;nbsp;What aren't I seeing?&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;		data _null_;
		   set QueryRules end=eof;
		   &lt;STRONG&gt;by Rule_Order;&lt;/STRONG&gt;
		   file '/home/ssbuechl/input_whens.sas';&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;BR /&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/13024i2C087391AB58B30C/image-size/large?v=1.0&amp;amp;px=600" border="0" alt="Capture.JPG" title="Capture.JPG" /&gt;</description>
      <pubDate>Mon, 03 Oct 2016 18:35:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-SAS-Program-Flow-Help/m-p/302130#M64035</guid>
      <dc:creator>buechler66</dc:creator>
      <dc:date>2016-10-03T18:35:24Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic SAS - Program Flow Help</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-SAS-Program-Flow-Help/m-p/302134#M64039</link>
      <description>&lt;P&gt;Do you mean doing something like this?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;		data _null_;
		   set QueryRules end=eof;
		   by Rule_Order;
		   file '/home/ssbuechl/input_whens.sas';

			array rule_array {1} $ 65 rule;
			array rule_order_array {1} rule_order; array rule_nm_array {1} $ 58 rule_nm;

		   if _n_=1 then put "     CASE";
				&lt;STRONG&gt;if first.rule_order = 1 then do;&lt;/STRONG&gt;
					do h = 1 to dim(rule_nm_array);
							do j = 1 to dim(rule_array);
									rule_nm=rule_nm_array{h};
									rule=rule_array{j};
								   PUT '           WHEN ('RULE') THEN trim("'RULE_NM'")';
							end;
					end;
				end;

		   if eof then put "     CASE";
				&lt;STRONG&gt;if first.rule_order = 0 then do;&lt;/STRONG&gt;
					do j = 1 to dim(rule_array);
						do k = 1 to dim(rule_order_array);
							rule=rule_array{j};
							rule_order=rule_order_array{k};
					   	   PUT '           WHEN ('RULE') THEN 'RULE_ORDER;
						end;
			   		end;
				end;

		   if eof then put "     ELSE .";
		   if eof then put "     END as Rule_Order";
		   if eof then put %nrstr("from QueryDataBase&amp;amp;ZIP5 as a ");
		   if eof then put %nrstr("inner join QueryDataBase&amp;amp;ZIP5 as b ");
		   if eof then put "on a.imb_code=b.imb_code ";
		   if eof then put "where a.source='A' and b.source='B'; ";
		   if eof then put "quit; ";
		run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 03 Oct 2016 18:51:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-SAS-Program-Flow-Help/m-p/302134#M64039</guid>
      <dc:creator>buechler66</dc:creator>
      <dc:date>2016-10-03T18:51:55Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic SAS - Program Flow Help</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-SAS-Program-Flow-Help/m-p/302153#M64049</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/79805"&gt;@buechler66&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;&lt;SPAN&gt;Hmm...I'm not certain I'm completely clear on your suggestion. In my QueryRules dataset every record is unique. &amp;nbsp;Rule_Nm, Rule, and Rule_Order are never repeated (see the attached file for a snippet of what the data looks like).&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Then I Set the QueryRules dataset 'by Rule_Order'. &amp;nbsp;So every record will then be both first.rule_order and last.rule_order? &amp;nbsp;I guess that is where I'm confused. Each CASE statement involves every record in the dataset. &amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;And you'll have to clarify what "Each CASE statement involves every record in the dataset.&amp;nbsp;&amp;nbsp;" In your QueryRules control data set or the target result of the query?&lt;/P&gt;
&lt;P&gt;If you are appling the SAME rule to multiple variables in a data set then maybe you should just move to a data step instead of a very convolute SQL approach.&lt;/P&gt;
&lt;P&gt;It might also help to provide what some of these "rules" are. If they are a bunch of recoding statments then look ups or formats might be a better approach to the core problem.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 03 Oct 2016 19:25:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-SAS-Program-Flow-Help/m-p/302153#M64049</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-10-03T19:25:29Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic SAS - Program Flow Help</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-SAS-Program-Flow-Help/m-p/302154#M64050</link>
      <description>&lt;P&gt;Does your control table have all the names of the variables that will be in the "As variablename" for the target of the CASE statement?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then have a separate array to contain those and loop over them with the body of the case inside.&lt;/P&gt;
&lt;P&gt;do j=1 to dim(outputvariablesarray);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; put "Case";&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; do i=1 to dim(rules);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; put "when" Rules [i] "then " value ;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;put "else . ";&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; put "end as " outputvariablesarray [ j ] " , ";&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You'll likely need to have an end of j loop test about whether to have the last one end with a comma unless there is other code you aren't showing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But lots of guessing without an example dataset.&lt;/P&gt;</description>
      <pubDate>Mon, 03 Oct 2016 19:40:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-SAS-Program-Flow-Help/m-p/302154#M64050</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-10-03T19:40:07Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic SAS - Program Flow Help</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-SAS-Program-Flow-Help/m-p/302183#M64069</link>
      <description>Let me work on something like this. Ty very much.</description>
      <pubDate>Mon, 03 Oct 2016 22:09:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-SAS-Program-Flow-Help/m-p/302183#M64069</guid>
      <dc:creator>buechler66</dc:creator>
      <dc:date>2016-10-03T22:09:07Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic SAS - Program Flow Help</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-SAS-Program-Flow-Help/m-p/302202#M64073</link>
      <description>&lt;P&gt;Below&amp;nbsp;a fully working code sample which demonstrates how you could approach this.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data QueryRules;
  Rule_Order=1;
  rule_nm='ACTUAL DELIVERY DATE MISSING IN IV';
  rule='A.ACTUAL_DLVRY_DATE IS NULL AND B.ACTUAL_DLVRY_DATE IS NOT NULL';
  output;
  rule_nm='ACTUAL DELIVERY DATE LATER IN IV';
  rule='A.ACTUAL_DLVRY_DATE &amp;gt; B.ACTUAL_DLVRY_DATE';
  output;
run;

filename header temp;
filename footer temp;
filename case1 temp;
filename case2 temp;

data _null_;
  set QueryRules end=eof;
  by Rule_Order;

  /*  file '/home/ssbuechl/input_whens.sas';*/
  array rule_nm_array {1} $ 58 rule_nm;
  array rule_array {1} $ 65 rule;

  /*** Header ***/
  if _n_=1 then
    do;
      file header;
      put 'proc sql;';
      put 'create table QueryData&amp;amp;ZIP5 as ';
      put 'select DISTINCT ';
      put "     a.actual_dlvry_date, ";
      put "     a.imb_code length = 31, ";
      put "     a.imb_dlvry_zip_5, ";
    end;

  /*** Cases ***/

  /* Case 1 */
  file case1 mod;
  if _n_=1 then
    do;
      put "     CASE";
    end;
  do h = 1 to dim(rule_nm_array);
    do j = 1 to dim(rule_array);
      rule_nm=rule_nm_array{h};
      rule=rule_array{j};
      PUT '           WHEN (' RULE ') THEN trim("' RULE_NM '")';
    end;
  end;
  if eof then
    do;
      put "     ELSE ' '";
      put "     END as RULE_NM_1,";
  end;

  /* Case 2 */
  file case2 mod;
  if _n_=1 then
    do;
      put "     CASE";
    end;
  do h = 1 to dim(rule_nm_array);
    do j = 1 to dim(rule_array);
      rule_nm=rule_nm_array{h};
      rule=rule_array{j};
      PUT '           WHEN (' RULE ') THEN trim("' RULE_NM '")';
    end;
  end;
  if eof then
    do;
      put "     ELSE ' '";
      put "     END as RULE_NM_2";
  end;

  /*** Footer ***/
  if eof then
    do;
      file footer;
      put %nrstr("from QueryDataBase&amp;amp;ZIP5 as a ");
      put %nrstr("inner join QueryDataBase&amp;amp;ZIP5 as b ");
      put "on a.imb_code=b.imb_code ";
      put "where a.source='A' and b.source='B'; ";
      put "quit; ";
    end;
run;


filename all temp;
/*filename all '/home/ssbuechl/input_whens.sas';*/
data _null_;
  file all;
  /* header */
  last=0;
  do until(last);
    infile header end=last;
    input;
    put _infile_;
  end;

  /* case 1 */
  last=0;
  do until(last);
    infile case1 end=last;
    input;
    put _infile_;
  end;

  /* case 2 */
  last=0;
  do until(last);
    infile case2 end=last;
    input;
    put _infile_;
  end;

  /* footer */
  last=0;
  do until(last);
    infile footer end=last;
    input;
    put _infile_;
  end;
  stop;
run;

data _null_;
  file print;
  infile all;
  input;
  put _infile_;
run;

filename header clear;
filename footer clear;
filename case1 clear;
filename case2 clear;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 04 Oct 2016 00:32:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-SAS-Program-Flow-Help/m-p/302202#M64073</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2016-10-04T00:32:11Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic SAS - Program Flow Help</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-SAS-Program-Flow-Help/m-p/302340#M64137</link>
      <description>&lt;P&gt;Wow! Thank you so much for this code example. With only slight alterations I was able to get exactly what I was after. And I learned a lot too! Thanks so much for taking the time to help. I really do appreciate it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Slightly modified code:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data QueryRules;
  Rule_Order=1;
  rule_nm='ACTUAL DELIVERY DATE MISSING IN IV';
  rule='A.ACTUAL_DLVRY_DATE IS NULL AND B.ACTUAL_DLVRY_DATE IS NOT NULL';
  output;
  &lt;STRONG&gt;Rule_Order=1.5;&lt;/STRONG&gt;
  rule_nm='ACTUAL DELIVERY DATE LATER IN IV';
  rule='A.ACTUAL_DLVRY_DATE &amp;gt; B.ACTUAL_DLVRY_DATE';
  output;
run;

filename header temp;
filename footer temp;
filename case1 temp;
filename case2 temp;

data _null_;
  set QueryRules end=eof;
  by Rule_Order;

  /*  file '/home/ssbuechl/input_whens.sas';*/
  array rule_nm_array {1} $ 58 rule_nm;
  array rule_array {1} $ 65 rule;
  &lt;STRONG&gt;array rule_order_array {1} rule_order;&lt;/STRONG&gt;

  /*** Header ***/
  if _n_=1 then
    do;
      file header;
      put 'proc sql;';
      put 'create table QueryData&amp;amp;ZIP5 as ';
      put 'select DISTINCT ';
      put "     a.actual_dlvry_date, ";
      put "     a.imb_code length = 31, ";
      put "     a.imb_dlvry_zip_5, ";
    end;

  /*** Cases ***/

  /* Case 1 */
  file case1 mod;
  if _n_=1 then
    do;
      put "     CASE";
    end;
  do h = 1 to dim(rule_nm_array);
    do j = 1 to dim(rule_array);
      rule_nm=rule_nm_array{h};
      rule=rule_array{j};
      PUT '           WHEN (' RULE ') THEN trim("' RULE_NM '")';
    end;
  end;
  if eof then
    do;
      put "     ELSE ' '";
      put "     END as RULE_NM,";
  end;

  /* Case 2 */
  file case2 mod;
  if _n_=1 then
    do;
      put "     CASE";
    end;
  &lt;STRONG&gt;do x = 1 to dim(rule_order_array);&lt;/STRONG&gt;
    do y = 1 to dim(rule_array);
      rule_nm=rule_nm_array{x};
      rule=rule_array{y};
      PUT '           WHEN (' RULE ') THEN trim("' &lt;STRONG&gt;RULE_ORDER&lt;/STRONG&gt; '")';
    end;
  end;
  if eof then
    do;
      put "     ELSE ' '";
      put "     END as &lt;STRONG&gt;RULE_ORDER&lt;/STRONG&gt;";
  end;

  /*** Footer ***/
  if eof then
    do;
      file footer;
      put %nrstr("from QueryDataBase&amp;amp;ZIP5 as a ");
      put %nrstr("inner join QueryDataBase&amp;amp;ZIP5 as b ");
      put "on a.imb_code=b.imb_code ";
      put "where a.source='A' and b.source='B'; ";
      put "quit; ";
    end;
run;


filename all temp;
/*filename all '/home/ssbuechl/input_whens.sas';*/
data _null_;
&lt;STRONG&gt;/*  file all;*/
FILE 'C:\TESTME.SAS';&lt;/STRONG&gt;

  /* header */
  last=0;
  do until(last);
    infile header end=last;
    input;
    put _infile_;
  end;

  /* case 1 */
  last=0;
  do until(last);
    infile case1 end=last;
    input;
    put _infile_;
  end;

  /* case 2 */
  last=0;
  do until(last);
    infile case2 end=last;
    input;
    put _infile_;
  end;

  /* footer */
  last=0;
  do until(last);
    infile footer end=last;
    input;
    put _infile_;
  end;
  stop;
run;


/*data _null_;*/
/*  file print;*/
/*  infile all;*/
/*  input;*/
/*  put _infile_;*/
/*run;*/

filename header clear;
filename footer clear;
filename case1 clear;
filename case2 clear;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Output TestMe.sas:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table QueryData&amp;amp;ZIP5 as 
select DISTINCT 
     a.actual_dlvry_date, 
     a.imb_code length = 31, 
     a.imb_dlvry_zip_5, 
     CASE
           WHEN (A.ACTUAL_DLVRY_DATE IS NULL AND B.ACTUAL_DLVRY_DATE IS NOT NULL ) THEN trim("ACTUAL DELIVERY DATE MISSING IN IV ")
           WHEN (A.ACTUAL_DLVRY_DATE &amp;gt; B.ACTUAL_DLVRY_DATE ) THEN trim("ACTUAL DELIVERY DATE LATER IN IV ")
     ELSE ' '
     END as RULE_NM,
     CASE
           WHEN (A.ACTUAL_DLVRY_DATE IS NULL AND B.ACTUAL_DLVRY_DATE IS NOT NULL ) THEN trim("1 ")
           WHEN (A.ACTUAL_DLVRY_DATE &amp;gt; B.ACTUAL_DLVRY_DATE ) THEN trim("1.5 ")
     ELSE ' '
     END as RULE_ORDER
from QueryDataBase&amp;amp;ZIP5 as a 
inner join QueryDataBase&amp;amp;ZIP5 as b 
on a.imb_code=b.imb_code 
where a.source='A' and b.source='B'; 
quit; &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 04 Oct 2016 13:52:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-SAS-Program-Flow-Help/m-p/302340#M64137</guid>
      <dc:creator>buechler66</dc:creator>
      <dc:date>2016-10-04T13:52:30Z</dc:date>
    </item>
  </channel>
</rss>

