/****************************************************************/ /* S A S S A M P L E C O D E */ /* */ /* NAME: TS722C */ /* TITLE: Experimental Design, Efficiency, Coding, and Choice*/ /* PRODUCT: STAT */ /* SYSTEM: ALL */ /* KEYS: marketing research */ /* PROCS: IML */ /* DATA: */ /* */ /* SUPPORT: saswfk UPDATE: 01Jan2005 */ /* REF: TS-722C Experimental Design, ... */ /* MISC: This file contains all of the sample code for */ /* the "Experimental Design, Efficiency, Coding, */ /* and Choice Designs" report, */ /* the 01Jan2005 edition, for SAS 9.1. This code */ /* should work fine in SAS 8.2 and SAS 9.0 as well. */ /* */ /* You must install the following macros from */ /* http://support.sas.com/techsup/tnote/tnote_stat.html#market */ /* */ /* ChoicEff Efficient Choice Designs */ /* MktAllo Process a Choice Allocation Study Data Set */ /* MktBal Balanced Experimental Design */ /* MktBlock Block an Experimental Design */ /* MktDes Efficient Experimental Designs */ /* MktDups Eliminate Duplicate Runs or Choice Sets */ /* MktEval Evaluate an Experimental Design */ /* MktEx Efficient Experimental Designs */ /* MktKey Aid Making the MktRoll KEY= Data Set */ /* MktLab Change Names, Levels in a Design */ /* MktMerge Merge a Choice Design with Choice Data */ /* MktOrth List Orthogonal Designs MktEx Can Make */ /* MktPPro Optimal Partial Profile Designs */ /* MktRoll Roll Out a Design Into a Choice Design */ /* MktRuns Experimental Design Sizes */ /* PhChoice Customize PROC PHREG for Choice Models */ /* */ /****************************************************************/ proc format; value price 1 = $2.89 2 = $2.99 3 = $3.09 4 = $3.19 5 = $3.29 6 = $2.89 7 = $3.09 8 = $3.29; run; *********** Begin Orthogonal Coding Example Code ************; options ls=80 ps=60 nonumber nodate; title; proc iml; /* orthogonal coding, levels must be 1, 2, ..., m */ reset fuzz; start orthogcode(x); levels = max(x); xstar = shape(x, levels - 1, nrow(x))`; j = shape(1 : (levels - 1), nrow(x), levels - 1); r = sqrt(levels # (x / (x + 1))) # (j = xstar) - sqrt(levels / (j # (j + 1))) # (j > xstar | xstar = levels); return(r); finish; design = (1:2)` @ j(6, 1, 1) || {1, 1} @ (1:6)`; x = j(12, 1, 1) || orthogcode(design[,1]) || orthogcode(design[,2]); print design[format=1.] ' ' x[format=5.2 colname={'Int' 'Two' 'Six'}]; xpx = x` * x; print xpx[format=best5.]; inv = inv(xpx); print inv[format=best5.]; d_eff = 100 / (nrow(x) # det(inv) ## (1 / ncol(inv))); a_eff = 100 / (nrow(x) # trace(inv) / ncol(inv)); print 'D-efficiency =' d_eff[format=6.2] ' A-efficiency =' a_eff[format=6.2]; design = design[1:10,]; x = j(10, 1, 1) || orthogcode(design[,1]) || orthogcode(design[,2]); inv = inv(x` * x); d_eff = 100 / (nrow(x) # det(inv) ## (1 / ncol(inv))); a_eff = 100 / (nrow(x) # trace(inv) / ncol(inv)); print 'D-efficiency =' d_eff[format=6.2] ' A-efficiency =' a_eff[format=6.2]; quit; title 'Cereal Bars'; %mktruns( 4 2 4 2 4 2 ) %mktex( 4 2 4 2 4 2, n=16, seed=17 ) title2 'Examine Correlations and Frequencies'; %mkteval; title2 'Examine Design'; proc print data=randomized; run; %mktkey(3 2) title2 'Create the Choice Design Key'; data key; input Brand $ 1-12 Price $ Count $; datalines; Branolicious x1 x2 Brantopia x3 x4 Brantasia x5 x6 None . . ; title2 'Create Choice Design from Linear Design'; %mktroll( design=randomized, key=key, alt=brand, out=cerealdes ) proc print; id set; by set; run; title2 'Final Choice Design'; proc format; value price 1 = $2.89 2 = $2.99 3 = $3.09 4 = $3.19 . = ' '; value count 1 = 'Six Bars' 2 = 'Eight Bars' . = ' '; run; data sasuser.cerealdes; set cerealdes; format price price. count count.; run; proc print data=sasuser.cerealdes(obs=16); by set; id set; run; title2 'Evaluate Design'; %choiceff(model=class(brand price count), /* model, expand to dummy vars */ nalts=4, /* number of alternatives */ nsets=16, /* number of choice sets */ beta=zero, /* assumed beta vector, Ho: b=0 */ intiter=0, /* no internal iterations just */ /* evaluate the input design */ data=sasuser.cerealdes, /* the input design to evaluate */ init=sasuser.cerealdes(keep=set))/* choice set number from design*/ title2 'Read Data'; data results; input Subject (r1-r16) (1.); datalines; 1 1331132331312213 2 3231322131312233 3 1233332111132233 4 1211232111313233 5 1233122111312233 6 3231323131212313 7 3231232131332333 8 3233332131322233 9 1223332111333233 10 1332132111233233 11 1233222211312333 12 1221332111213233 13 1231332131133233 14 3211333211313233 15 3313332111122233 16 3321123231331223 17 3223332231312233 18 3211223311112233 19 1232332111132233 20 1213233111312413 21 1333232131212233 22 3321322111122231 23 3231122131312133 24 1232132111311333 25 3113332431213233 26 3213132141331233 27 3221132111312233 28 3222333131313231 29 1221332131312231 30 3233332111212233 31 1221332111342233 32 2233232111111211 33 2332332131211231 34 2221132211312411 35 1232233111332233 36 1231333131322333 37 1231332111331333 38 1223132211233331 39 1321232131211231 40 1223132331321233 ; title2 'Merge Data and Design'; %mktmerge(design=sasuser.cerealdes, /* input design */ data=results, /* input data set */ out=res2, /* output data set with design and data */ nsets=16, /* number of choice sets */ nalts=4, /* number of alternatives */ setvars=r1-r16) /* variables with the chosen alt nums */ title2 'Design and Data Both'; proc print data=res2(obs=16); by set subject; id set subject; run; title2 'Code the Independent Variables'; proc transreg design norestoremissing data=res2; model class(brand price count); id subject set c; output out=coded(drop=_type_ _name_ intercept) lprefix=0; run; proc print data=coded(obs=16) label; title3 'ID Information and the Dependent Variable'; format price price. count count.; var Brand Price Count Subject Set c; by set subject; id set subject; run; proc print data=coded(obs=16) label; title3 'ID Information and the Coding of Brand'; format price price. count count.; var brandbranolicious brandbrantasia brandbrantopia brand; by set subject; id set subject; run; proc print data=coded(obs=16) label; title3 'ID Information and the Coding of Price and Count'; format price price. count count.; var Price_2_89 Price_2_99 Price_3_09 CountSix_Bars Price Count; by set subject; id set subject; run; %phchoice( on ) title2 'Multinomial Logit Discrete Choice Model'; proc phreg data=coded brief; model c*c(2) = &_trgind / ties=breslow; strata subject set; run; %phchoice( off ) %mktorth(maxn=100, options=parent); data x; set mktdeslev; array x[50]; c = 0; one = 0; q = 0; do i = 1 to 50; c + (x[i] > 0); /* how many differing numbers of levels */ if x[i] > 1 then do; m = i; q = x[i]; end; /* m^q */ if x[i] = 1 then do; one + 1; p = i; end; /* p^1 */ end; if c = 2 and one = 1 and p >= q and q > 2 and p * m = n; design = compbl(left(design)); run; proc print; var n design; run; %let p = 6; %let m = 3; %let q = &p; %mktex(&p &m ** &q, n=&p * &m) %mktlab(data=design, vars=Set x1-x&q) proc print; id set; by set; run; %choiceff(data=final, init=final(keep=set), model=class(x1-x&q), nsets=&p, nalts=&m, beta=zero)