Published on :
Mixed MIXTE

Sales Report and Negative Binomial Simulation

This code is also available in: Deutsch Español Français
Awaiting validation
The code has two distinct parts. The first generates a financial report from the 'sashelp.orsales' table, calculating profits by year and product line with percentages. The second part simulates a dataset of 2000 observations following a negative binomial distribution, then uses PROC GLIMMIX to estimate the parameters and validate the simulation against theoretical values.
Data Analysis

Type : MIXTE


Uses SASHELP.ORSALES for reporting. Generates internal data (table 'one') via random functions (rangam, ranpoi) for the statistical part.

1 Code Block
PROC FORMAT
Explanation :
Definition of a user-defined format for displaying product line labels.
Copied!
1PROC FORMAT;
2 value $sequip
3 'Sports' = 'Sports Equipment';
4 RUN;
2 Code Block
PROC REPORT
Explanation :
Creation of a summary report with percentage calculations via a COMPUTE block. Note: The line 'line @code_sas/...' contains a file reference that could generate a syntax error if not commented out or corrected.
Copied!
1title1 'Total profit per year';
2title2 'Separated by Product Line';
3PROC REPORT DATA=sashelp.orsales nowd split='*';
4 column year product_line profit percent;
5 define year / group;
6 define product_line
7 / group
8 f=$sequip.
9 'Product*Groups';
10 define profit / analysis
11 sum FORMAT=dollar15.2
12 'Annual*Profit';
13 define percent/ computed 'Product*Percentage'
14 FORMAT=percent10.2;
15 
16 break after year/ summarize suppress skip;
17 rbreak after / summarize;
18 
19 compute before year;
20 total = profit.sum;
21 endcomp;
22 compute percent;
23 percent = profit.sum/total;
24 endcomp;
25 compute after;
26 line ' ';
27 line @code_sas/25 Appendix_A Generating_Negative_Binomial_Data.sas Appendix_A Generating_Negative_Binomial_Data.sas 'Profits in US dollars';
28 endcomp;
29 RUN;
3 Code Block
DATA STEP Data
Explanation :
Data simulation: creation of a 'one' table containing random variables following a negative binomial distribution.
Copied!
1DATA one;
2 Mu = 30;
3 Kappa = 0.1;
4 alpha = 1 / kappa;
5 beta = kappa * mu;
6 n = 2000;
7 seed = 1917;
8 Variance = mu * ( 1 + mu*kappa);
9 DO id=1 to n;
10 u = beta * rangam( seed, alpha );
11 Y = ranpoi( seed, u );
12 OUTPUT;
13 END;
14 keep Mu Kappa Variance id y;
15RUN;
4 Code Block
PROC GLIMMIX
Explanation :
Statistical modeling: estimation of distribution parameters on simulated data to check consistency with input parameters.
Copied!
1ods select none;
2ods OUTPUT ParameterEstimates=parms;
3PROC GLIMMIX DATA=one;
4 model y = / dist=negbin link=identity s;
5RUN;
6ods select all;
5 Code Block
PROC PRINT / PROC MEANS
Explanation :
Results validation: comparative display of theoretical parameters (Mu, Kappa) and observed statistics on generated data.
Copied!
1ods html;
2title "True Mu and Kappa of Y's iid Negative-binomial(Mu, Kappa)";
3proc print data=one noobs;
4 where id = 1;
5 var Mu Kappa;
6run;
7 
8title "Estimated Mu and Kappa using PROC GLIMMIX";
9proc print data=parms noobs;
10 var Effect Estimate;
11run;
12 
13data one;
14 set one;
15 rename Mu = Mean;
16run;
17 
18title "True Mean and Variance of Y's iid Negative-binomial(Mu, Kappa)";
19PROC PRINT DATA=one noobs;
20 where id = 1;
21 var Mean Variance;
22RUN;
23 
24title "Estimated Mean and Variance";
25PROC MEANS DATA=one n mean var maxdec=2;
26 var y;
27RUN;
28ods html close;
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.