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!
proc format;
value $sequip
'Sports' = 'Sports Equipment';
run;
1
PROC 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!
title1 'Total profit per year';
title2 'Separated by Product Line';
proc report data=sashelp.orsales nowd split='*';
column year product_line profit percent;
define year / group;
define product_line
/ group
f=$sequip.
'Product*Groups';
define profit / analysis
sum format=dollar15.2
'Annual*Profit';
define percent/ computed 'Product*Percentage'
format=percent10.2;
break after year/ summarize suppress skip;
rbreak after / summarize;
compute before year;
total = profit.sum;
endcomp;
compute percent;
percent = profit.sum/total;
endcomp;
compute after;
line ' ';
line @code_sas/25 Appendix_A Generating_Negative_Binomial_Data.sas Appendix_A Generating_Negative_Binomial_Data.sas 'Profits in US dollars';
endcomp;
run;
1
title1 'Total profit per year';
2
title2 'Separated by Product Line';
3
PROC REPORTDATA=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!
data one;
Mu = 30;
Kappa = 0.1;
alpha = 1 / kappa;
beta = kappa * mu;
n = 2000;
seed = 1917;
Variance = mu * ( 1 + mu*kappa);
do id=1 to n;
u = beta * rangam( seed, alpha );
Y = ranpoi( seed, u );
output;
end;
keep Mu Kappa Variance id y;
run;
1
DATA 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;
15
RUN;
4 Code Block
PROC GLIMMIX
Explanation : Statistical modeling: estimation of distribution parameters on simulated data to check consistency with input parameters.
Copied!
ods select none;
ods output ParameterEstimates=parms;
proc glimmix data=one;
model y = / dist=negbin link=identity s;
run;
ods select all;
1
ods select none;
2
ods OUTPUT ParameterEstimates=parms;
3
PROC GLIMMIXDATA=one;
4
model y = / dist=negbin link=identity s;
5
RUN;
6
ods 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!
ods html;
title "True Mu and Kappa of Y's iid Negative-binomial(Mu, Kappa)";
proc print data=one noobs;
where id = 1;
var Mu Kappa;
run;
title "Estimated Mu and Kappa using PROC GLIMMIX";
proc print data=parms noobs;
var Effect Estimate;
run;
data one;
set one;
rename Mu = Mean;
run;
title "True Mean and Variance of Y's iid Negative-binomial(Mu, Kappa)";
proc print data=one noobs;
where id = 1;
var Mean Variance;
run;
title "Estimated Mean and Variance";
proc means data=one n mean var maxdec=2;
var y;
run;
ods html close;
1
ods html;
2
title "True Mu and Kappa of Y's iid Negative-binomial(Mu, Kappa)";
3
proc print data=one noobs;
4
where id = 1;
5
var Mu Kappa;
6
run;
7
8
title "Estimated Mu and Kappa using PROC GLIMMIX";
9
proc print data=parms noobs;
10
var Effect Estimate;
11
run;
12
13
data one;
14
set one;
15
rename Mu = Mean;
16
run;
17
18
title "True Mean and Variance of Y's iid Negative-binomial(Mu, Kappa)";
19
PROC PRINTDATA=one noobs;
20
where id = 1;
21
var Mean Variance;
22
RUN;
23
24
title "Estimated Mean and Variance";
25
PROC MEANSDATA=one n mean var maxdec=2;
26
var y;
27
RUN;
28
ods 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.
SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and other countries. ® indicates USA registration. WeAreCAS is an independent community site and is not affiliated with SAS Institute Inc.
This site uses technical and analytical cookies to improve your experience.
Read more.