Online File

How to use this page


Rick Aster: Professional SAS Programming Shortcuts: Contents

Chapter 84
Program
Risk (exhaustive simulation)


data work.risk2 (keep=d_dice a_dice outcome compress=no);
   length d_dice a_dice 3 outcome $ 2;
   do d_dice = 1 to 2; * Number of defending dice;
   do a_dice = 1 to 3; * Number of attacking dice;
      res_a = 0; res_aa = 0; res_dd = 0;
      armies = a_dice min d_dice; * Number of armies decided;
      do roll_d1 = 1 to 6; * Defending dice roll;
      do roll_d2 = (d_dice = 2) to 6*(d_dice = 2); * 1-6 or 0-0;
         defend1 = largest(1, of roll_d1-roll_d2); *Defending dice, sorted;
         defend2 = largest(2, of roll_d1-roll_d2);
         do roll_a1 = 1 to 6; * Attacking dice;
         do roll_a2 = (a_dice >= 2) to 6*(a_dice >= 2);
         do roll_a3 = (a_dice >= 3) to 6*(a_dice >= 3);
            attack1 = largest(1, of roll_a1-roll_a3);
            attack2 = largest(2, of roll_a1-roll_a3);
            * Count results;
            if armies = 1 then select (attack1 > defend1);
               when (1) outcome = ' A';
               otherwise outcome = ' D';
               end;
            else select ((attack1 > defend1) + (attack2 > defend2));
               when (2) outcome = 'AA';
               when (1) outcome = 'AD';
               otherwise outcome = 'DD';
               end;
            output;
            end;
            end;
            end;
         end;
         end;
      end;
      end;
   stop;
run;

proc tabulate data=work.risk2;
   class d_dice a_dice outcome;
   table d_dice*a_dice, outcome*rowpctn*f=7.3
       / rtspace=15 box='A=Attacker D=Defender';
   keylabel rowpctn=' ';
run;

 O /\

Global
Statements

RICK ASTER

SAS

BOOKS

Tech | Dictionary

Download | Rastinate

Rick Aster

Professional SAS Programming Shortcuts

Contents/Online Files

Corrections

Catalog Page