sasproc-sqlenterprise-guidedo-loops

Adding additional steps to a SAS DO loop based on the iteration value


I using a DO loop to run Proc SQL queries iterating through a list of variables. I am wondering if there is a way to add steps to that DO loop when i = only certain variables, and skip those steps for the rest? ABRV and PLAN_IDS are combinations of strings from the variable list that I am looping through. The reason I would like to do this as part of the same macro is the program runs on a schedule through an automation system. I want to create 7 tables, 6 using only the first query and a 7th that uses the variables for i=6 only (in bold)

%macro Create_Tables;
%do i =1 %to 6;
proc sql;
create table BASELINE_UTILIZATION&&ABRV&i as
select 
t1.PLAN_NAME,
t1.STATE_CODE,
t1."PLAN ID",
SUM(t1.Rx#) As BL_Rx,
SUM(t1.Qty) As BL_Qty
from report_table_1 t1 
where t1.State_Code = &&ABRV&i
AND t1."PLAN ID" IN (&&PLAN_IDS&i)
AND t1.Paiddate in ('202401') 
GROUP BY t1.PLAN_NAME, t1.STATE_CODE, t1."PLAN ID"
);
**IF i=6 THEN
create table EXTRA_UTILIZATION&&ABRV&i
proc sql;
select
t1.PLAN_NAME,
t1.STATE_CODE,
t1."PLAN ID",
SUM(t1.Rx#) *4 As BL_Rx_extra,
SUM(t1.Qty) *4 As BL_Qty_extra
from report_table_1 t1
where t1.State_Code = &&ABRV&i
AND t1."PLAN ID" IN (&&PLAN_IDS&i)
AND t1.Paiddate in ('202401') 
GROUP BY t1.PLAN_NAME, t1.STATE_CODE, t1."PLAN ID"
);**
%end;
%mend Create_Tables;
%Create_Tables

Solution

  • With Tom's help, this is the basic structure of the code I was able to run correctly:

    %macro Create_Tables;
    %do i=1 to i=6;
    * Proc SQL Query 1
    %if &i=6 %then% do;
    * Proc SQL Query 2
    %end;
    %end;
    %mend Create_Tables;
    %Create_Tables;