Pages

Monday, April 1, 2019

SAS Example : Daily Sales Report Automation

The purpose of this post is to give an idea on automation of report generation. 


The below code generates Automated Sales Summary Email based on discrete sales figures on a product, place, cost & revenue by utilizing macros.


Create a sample sales data as below:


data sales;
input product_id place $ product_cost product_revenue ;
date=today()-1;format date date9.;
cards;
101 goa 100 200
101 punjab 100 250
101 mumbai 100 300
101 chennai 100 175
101 kerala 100 225
102 goa 200 400
102 punjab 200 450
102 mumbai 200 400
102 chennai 200 475
102 kerala 200 425
;
run;

SAS dataset:




Macro to generate sales report daily:



The SAS macro generates the sales stats and sends an email automatically without manual intervention.


%macro sales_report();

/* macro variable to capture yesterday's date*/
%let date=%sysfunc(putn(%eval(%sysfunc(today())-1),date9.));
%put &date.;

/* filter yesterday's sales data*/
data sales_fltr;
set sales;
where date="&date."d;
run;

/* Mention email id to which the report has to be generated. Modify the subject if required */

   filename temp email to="your_email@gmail.com"
            subject="Sales report - &date. "
            type="text/html";

   ods html body=temp;

   proc sql;
   select product_id,
    sum(product_cost) as total_cost,
    sum(product_revenue) as total_revenue,
    calculated total_revenue-calculated total_cost as profit
   from sales_fltr
   group by product_id;
   run;

   ods html close;

%mend;
%sales_report;

Email:



SAS output:



No comments:

Post a Comment