A Subsetting IF Statement at the Bottom
This program calculates
six new variables before the subsetting IF statement selects only
observations whose values for Month are 12.
data profit;
retain TotalProfit TotalDiscount TotalWait Count 0;
set retail.order_fact;
MonthOfOrder=month(order_date);
WaitTime=sum(delivery_date,-order_date);
if discount gt . then
CalcProfit=sum((total_retail_price*discount),-costprice_per_unit)
*quantity;
else CalcProfit=sum(total_retail_price,-costprice_per_unit)
*quantity;
TotalProfit=sum(totalprofit,calcprofit);
TotalDiscount=sum(totaldiscount,discount);
TotalWait=sum(totalwait,waittime);
Count+1;
if monthoforder=12;
run; |
A Subsetting IF Statement near the Top
In this program, the
subsetting IF statement is positioned immediately after the value
for MonthofOrder has been calculated. If the value is not 12, then
no further statements are processed for that observation. In this
program, calculations are performed on a smaller number of observations,
which results in greater program efficiency.
data profit;
retain TotalProfit TotalDiscount TotalWait Count 0;
set retail.order_fact;
MonthOfOrder=month(order_date);
if monthoforder=12;
WaitTime=sum(delivery_date,-order_date);
if discount gt . then
CalcProfit=sum((total_retail_price*discount),-costprice_per_unit)
*quantity;
else CalcProfit=sum(total_retail_price,-costprice_per_unit)
*quantity;
TotalProfit=sum(totalprofit,calcprofit);
TotalDiscount=sum(totaldiscount,discount);
TotalWait=sum(totalwait,waittime);
Count+1;
run; |
IF-THEN/ELSE Statements
This program uses IF-THEN/ELSE
statements with DO groups to conditionally execute multiple statements
that calculate an adjusted profit. Conditions are checked in descending
order of frequency.
data retail.order_info_1; set retail.order_fact; if order_type=1 then do; /* Retail Sale */ Float=delivery_date-order_date; RevenueQuarter=qtr(order_date); AveragePrice=total_retail_price/quantity; if discount=. then NetPrice=total_retail_price; else NetPrice=total_retail_price-discount; Profit=netPrice-(quantity*costprice_per_unit)*1.05; end; else if order_type=2 then do; /* Catalog Sale */ Float=delivery_date-order_date; RevenueQuarter=qtr(order_date); AveragePrice=total_retail_price/quantity; if discount=. then NetPrice=total_retail_price; else NetPrice=total_retail_price-discount; Profit=netprice-(quantity*costprice_per_unit)*1.03; end; else do; /* Internet Sale */ Float=delivery_date-order_Date; RevenueQuarter=qtr(order_date); AveragePrice=total_retail_price/quantity; if discount=. then NetPrice=total_retail_price; else NetPrice=total_retail_price-discount; Profit=netprice-(quantity*costprice_per_unit); end; run; |
SELECT Statements
This program uses SELECT
and WHEN statements with DO groups to conditionally execute multiple
statements that calculate an adjusted profit. Conditions
are checked in descending order of frequency.
data retail.order_info_2; set retail.order_fact; select(order_type); when (1) do; /* Retail Sale */ Float=delivery_date-order_date; RevenueQuarter=qtr(order_date); AveragePrice=total_retail_price/quantity; if discount=. then NetPrice=total_retail_price; else NetPrice=total_retail_price-discount; Profit=netprice-(quantity*costprice_per_unit)*1.05; end; when (2) do; /* Catalog Sale */ Float=delivery_date-order_date; RevenueQuarter=qtr(order_date); AveragePrice=total_retail_price/quantity; if discount=. then NetPrice=total_retail_price; else NetPrice=total_retail_price-discount; Profit=netprice-(quantity*costprice_per_unit)*1.03; end; otherwise do; /* Internet Sale */ Float=delivery_date-order_date; RevenueQuarter=qtr(order_date); AveragePrice=total_retail_price/quantity; if discount=. then NetPrice=total_retail_price; else NetPrice=total_retail_price-discount; Profit=netprice-(quantity*costprice_per_unit); end; end; run; |
Parallel IF Statements
This program calls the
MONTH function 12 times. With these non-exclusive cases, each IF statement
executes for each observation that is read from Retail.Order_Fact.
This is the least efficient approach.
data retail.orders; set retail.order_fact; if month(order_date)=1 then Month='Jan'; if month(order_date)=2 then Month='Feb'; if month(order_date)=3 then Month='Mar'; if month(order_date)=6 then Month='Jun'; if month(order_date)=7 then Month='Jul'; if month(order_date)=8 then Month='Aug'; if month(order_date)=9 then Month='Sep'; if month(order_date)=10 then Month='Oct'; if month(order_date)=11 then Month='Nov'; if month(order_date)=12 then Month='Dec'; run; |
ELSE IF Statements, Many Function References
This program uses ELSE
IF statements that call the function MONTH. Once the true condition
is found, subsequent ELSE IF statements are not executed. This is
more efficient than using parallel IF statements, but the MONTH function
is executed many times.
data retail.orders; set retail.order_fact; if month(order_date)=1 then Month='Jan'; else if month(order_date)=2 then Month='Feb'; else if month(order_date)=3 then Month='Mar'; else if month(order_date)=4 then Month='Apr'; else if month(order_date)=5 then Month='May'; else if month(order_date)=6 then Month='Jun'; else if month(order_date)=7 then Month='Jul'; else if month(order_date)=10 then Month='Oct'; else if month(order_date)=11 then Month='Nov'; else if month(order_date)=12 then Month='Dec'; run; |
ELSE IF Statements, One Function Reference
This program uses the
MONTH function to find the value of Order_Date, but only once. The
MONTH function is called immediately after reading the data set and
before any IF-THEN/ELSE statements execute. This is efficient.
data retail.orders(drop=mon); set retail.order_fact; mon=month(order_date); if mon=1 then Month='Jan'; else if mon=2 then Month='Feb'; else if mon=3 then Month='Mar'; else if mon=4 then Month='Apr'; else if mon=5 then Month='May'; else if mon=6 then Month='Jun'; else if mon=7 then Month='Jul'; else if mon=8 then Month='Aug'; else if mon=9 then Month='Sep'; else if mon=10 then Month='Oct'; else if mon=11 then Month='Nov'; else if mon=12 then Month='Dec'; run; |
SELECT Group
In this program, the
SELECT statement calls the MONTH function only once, before WHEN statements
execute and assign values for Month. This is efficient.
data retail.orders; set retail.order_fact; select(month(order_date)); when (1) Month='Jan'; when (2) Month='Feb'; when (3) Month='Mar'; when (4) Month='Apr'; when (5) Month='May'; when (6) Month='Jun'; when (7) Month='Jul'; when (8) Month='Aug'; when (11) Month='Nov'; when (12) Month='Dec'; otherwise; end; run; |
data orders; set company.orders; if order_type = 1 then do; <multiple executable statements here> end; else if order_type = 2 then do; <multiple executable statements here> end; else if order_type = 3 then do; <multiple executable statements here> end; run;
data orders; set company.orders; select (order_type); when (1) do; <multiple executable statements here> end; when (2) do; <multiple executable statements here> end; when (3) do; <multiple executable statements here> end; otherwise; end; run;
inactive
, low
activity
, medium activity
,
or high activity
.
Parallel IF Statements
This program creates
a permanent SAS data set named Retail.Customers by reading the Retail.Customer_hybrid
data set. Serial IF statements are used to populate the variables
Customer_Group and Customer_Activity.
data retail.customers; length Customer_Group $ 26 Customer_Activity $ 15; set retail.customer_hybrid; if substr(put(customer_type_ID,4.),1,2)='10' then customer_group='Orion Club members'; if substr(put(customer_type_ID,4.),1,2)='20' then customer_group='Orion Club Gold members'; if substr(put(customer_type_ID,4.),1,2)='30' then customer_group='Internet/Catalog Customers'; if substr(put(customer_type_ID,4.),1,2) in ('10', '20') and substr(put(customer_type_ID,4.),3,2)='10' then customer_activity='inactive'; if substr(put(customer_type_ID,4.),1,2) in ('10', '20') and substr(put(customer_type_ID,4.),3,2)='20' then customer_activity='low activity'; if substr(put(customer_type_ID,4.),1,2) in ('10', '20') and substr(put(customer_type_ID,4.),3,2)='30' then customer_activity='medium activity'; if substr(put(customer_type_ID,4.),1,2) in ('10', '20') and substr(put(customer_type_ID,4.),3,2)='40' then customer_activity='high activity'; run; |
SELECT, IF/SELECT Statements
This program creates
a permanent SAS data set named Retail.Customers by reading the Retail.Customer_hybrid
data set. SELECT/WHEN logic and SELECT/WHEN statements in an IF/THEN
DO group populate the variables Customer_Group and Customer_Activity.
If the value of the first two digits of Customer_Type_ID is
10 , 20 ,
or 30 , then Customer_Group is populated.
If the value of the first two digits of Customer_Type_ID is 10 or 20 ,
then Customer_Activity is populated by reading the last two digits
of Customer_Type_ID.
data retail.customers; length Customer_Group $ 26 Customer_Activity $ 15; set retail.customer_hybrid; select(substr(put(customer_type_ID,4.),1,2)); when ('10') customer_group='Orion Club members'; when ('20') customer_group='Orion Club Gold members'; when ('30') customer_group='Internet/Catalog Customers'; otherwise; end; if substr(put(customer_type_ID,4.),1,2) in ('10', '20') then do; select(substr(put(customer_type_ID,4.),3,2)); when ('10') customer_activity='inactive'; when ('20') customer_activity='low activity'; when ('30') customer_activity='medium activity'; when ('40') customer_activity='high activity'; otherwise; end; end; run; |
Nested SELECT Statements
This program creates
a permanent SAS data set named Retail.Customers by reading the Retail.Customer_hybrid
data set. Nested SELECT statements are used to populate the variables
Customer_Group and Customer_Activity.
data retail.customers; length Customer_Group $ 26 Customer_Activity $ 15; set retail.customer_hybrid; select(substr(put(customer_type_ID,4.),1,2)); when ('10') do; customer_group='Orion Club members'; select(substr(put(customer_type_ID,4.),3,2)); when ('10') customer_activity='inactive'; when ('20') customer_activity='low activity'; when ('30') customer_activity='medium activity'; when ('40') customer_activity='high activity'; otherwise; end; end; when ('20') do; customer_group='Orion Club Gold members'; select(substr(put(customer_type_ID,4.),3,2)); when ('10') customer_activity='inactive'; when ('20') customer_activity='low activity'; when ('30') customer_activity='medium activity'; when ('40') customer_activity='high activity'; otherwise; end; end; when ('30') customer_group='Internet/Catalog Customers'; otherwise; end; run; |
IF-THEN/ELSE IF Statements with a Link
This program creates
a permanent SAS data set named Retail.Customers by reading the Retail.Customer_hybrid
data set. IF-THEN/ELSE IF statements are used with a link to populate
the variables Customer_Group and Customer_Activity.
data retail.customers; length Customer_Group $ 26 Customer_Activity $ 15; set retail.customer_hybrid; if substr(put(customer_type_ID,4.),1,2)='10' then do; customer_group='Orion Club members'; link activity; end; else if substr(put(customer_type_ID,4.),1,2)='20' then do; customer_group='Orion Club Gold members'; link activity; end; else if substr(put(customer_type_ID,4.),1,2)='30' then customer_group='Internet/Catalog Customers'; return; activity: if substr(put(customer_type_ID,4.),3,2)='10' then customer_activity='inactive'; else if substr(put(customer_type_ID,4.),3,2)='20' then customer_activity='low activity'; else if substr(put(customer_type_ID,4.),3,2)='30' then customer_activity='medium activity'; else if substr(put(customer_type_ID,4.),3,2)='40' then customer_activity='high activity'; return; run; |