fcmpact addRoutines

Standard Use Case: Tiered Customer Discount Calculation

Scénario de test & Cas d'usage

Business Context

A retail company needs to deploy a custom function to calculate tiered discounts for customers based on their loyalty status and purchase amount. This function must be made available in the CAS environment for marketing campaign analysis.
About the Set : fcmpact

Execution of SAS FCMP functions within the CAS environment.

Discover all actions of fcmpact
Data Preparation

Create a sample table of customer purchases with varying loyalty statuses and amounts to test the different discount tiers.

Copied!
1DATA casuser.customer_purchases(promote=yes);
2 LENGTH customerId $ 10 loyaltyStatus $ 8;
3 INFILE DATALINES dsd;
4 INPUT customerId $ loyaltyStatus $ purchaseAmount;
5 DATALINES;
6CUST001,Gold,550
7CUST002,Silver,120
8CUST003,Bronze,75
9CUST004,Gold,80
10CUST005,Silver,250
11;
12RUN;

Étapes de réalisation

1
Compile and store the custom discount function 'calculate_discount' in a new function table named 'marketing_funcs'.
Copied!
1PROC CAS;
2 SESSION casauto;
3 fcmpact.addRoutines
4 routineCode={ 'function calculate_discount(status $, amount);
5 length calculated_discount 8;
6 if upcase(status) = "GOLD" and amount > 500 then calculated_discount = amount * 0.20;
7 else if upcase(status) = "GOLD" then calculated_discount = amount * 0.15;
8 else if upcase(status) = "SILVER" then calculated_discount = amount * 0.10;
9 else calculated_discount = amount * 0.05;
10 return(calculated_discount);
11 endsub;' },
12 package='marketing.discounts',
13 funcTable={name='marketing_funcs', caslib='casuser', replace=true};
14RUN;
2
Apply the newly created FCMP function to the customer data using a CAS DATA step to verify its logic.
Copied!
1PROC CAS;
2 SESSION casauto;
3 DATA casuser.customer_offers(promote=yes);
4 method RUN();
5 SET casuser.customer_purchases;
6 discount = calculate_discount(loyaltyStatus, purchaseAmount);
7 END;
8 dcl package fcmpact('marketing.discounts') p;
9 RUN;
10 
11 TABLE.fetch / TABLE={caslib='casuser', name='customer_offers'};
12RUN;

Expected Result


The 'marketing_funcs' table is created in the 'casuser' caslib. The subsequent DATA step successfully uses the 'calculate_discount' function. The final 'customer_offers' table contains a 'discount' column with correctly calculated values: 110 for CUST001, 12 for CUST002, 3.75 for CUST003, 12 for CUST004, and 25 for CUST005.