## Syntax Examples (omod0c)

```/***************************************************************/
/*                                                             */
/*          S A S   S A M P L E   L I B R A R Y                */
/*                                                             */
/*    NAME: omod0c                                             */
/*   TITLE: Syntax Examples (omod0c)                           */
/* PRODUCT: OR                                                 */
/*  SYSTEM: ALL                                                */
/*    KEYS: OR                                                 */
/*   PROCS: OPTMODEL                                           */
/*    DATA:                                                    */
/*                                                             */
/* SUPPORT:                             UPDATE:                */
/*     REF:                                                    */
/*    MISC: Examples from the Syntax section of the            */
/*          OPTMODEL chapter of Mathematical Programming.      */
/*                                                             */
/***************************************************************/

/****************************************************************
*                                                              *
*  Declaration Statements                                      *
*                                                              *
****************************************************************/

/*  NUMBER, STRING, and SET Parameters  */
proc optmodel;
num a{i in 1..2} init i**2;
a[1] = 2;
put a[*]=;
quit;

/*  NUMBER, STRING, and SET Parameters: Initializing Arrays  */
proc optmodel;
number a{1..3} = [5 4 7];
number b{1..3} INIT [5 4 7];
put a[*]=;
b[1] = 1;
put b[*]=;
quit;

/****************************************************************
*                                                              *
*  Programming Statements                                      *
*                                                              *
****************************************************************/

/*  Create Statement: Columns  */
proc optmodel;
set<string> alph = {'a', 'b', 'c'};
var x{1..3, alph} init 2;
create data example from [i]=(1..3)
{j in alph}<col("x"||j)=x[i,j]>;
quit;

proc optmodel;
number m = 7, n = 5;
create data example from m n ratio=(m/n) col("s"||n)=(m+n);
quit;

proc print;
run;

proc optmodel;
num sq{i in 1..10} = i*i;
create data squares from [i/format=hex2./length=3] sq/format=6.2;
quit;

proc print;
run;

/*  Create Statement: Key Columns  */
proc optmodel;
number m{1..5, 1..3} = [[1 1] 1 [4 1] 1];
create data example
from [i j] = {setof{i in 1..2}<i**2>, {1, 2}} m;
quit;

proc print data=example noobs;
run;

proc optmodel;
number x{1..2} init 2;
var y{2..3} init 3;
create data exdata from [keycol] x y;
quit;

proc print;
run;

proc optmodel;
num m = 3;  /* number of rows/observations */
num n = 4;  /* number of columns in a */
num a{i in 1..m, j in 1..n} = i*j;  /* compute a */
num b{i in 1..m} = i**2;  /* compute b */
set<num> subset = 2..m;  /* used to omit first row */
create data out
from [i]=subset {j in 1..n}<col("a"||j)=a[i,j]> b;
quit;

/*  DO Statement: DO Statement, Iterative  */
proc optmodel;
number i;
do i=1,3,5;
put i;
end;
quit;

proc optmodel;
number i;
do i=1 to 5 by 2;
put 'value of i assigned by the DO loop = ' i;
i=i**2;
put 'value of i assigned in the body of the loop = ' i;
end;
quit;

proc optmodel;
set s = {1,3,5};
number i;
do i = s;
put i;
end;
quit;

proc optmodel;
set s = {1,2,3,4,5};
number i;
do i = s while(i NE 4);
put i;
end;
quit;

/*  DO UNTIL Statement  */
proc optmodel;
number i;
i = 1;
do until (i=3);
put i;
i=i+1;
end;
quit;

/*  DO WHILE Statement  */
proc optmodel;
number i;
i = 1;
do while (i<3);
put i;
i=i+1;
end;
quit;

/*  DROP Statement  */
proc optmodel;
var x{1..10};
con c1: x[1] + x[2] <= 3;
con disp{i in 1..9}: x[i+1] >= x[i] + 0.1;

drop c1;        /* drops the c1 constraint */
drop disp[5];   /* drops just disp[5] */
drop disp;      /* drops all disp constraints */
quit;

/*  EXPAND Statement  */
proc optmodel;
number n=2;
var x{1..n};
min z1=sum{i in 1..n}(x[i]-i)**2;
max z2=sum{i in 1..n}(i-x[i])**3;
con c{i in 1..n}: x[i]>=0;
fix x[2]=3;
expand;
quit;

proc optmodel;
number n=2;
var x{1..n};
min z1=sum{i in 1..n}(x[i]-i)**2;
max z2=sum{i in 1..n}(i-x[i])**3;
con c{i in 1..n}: x[i]>=0;
fix x[2]=3;
expand / solve;
quit;

/*  FOR Statement  */
proc optmodel;
for {i in 1..2, j in {'a', 'b'}} put i= j=;
quit;

/*  LEAVE Statement  */
proc optmodel;
number i, j;
do i = 1..5;
do j = 1..4;
if i >= 3 and j = 2 then leave;
end;
print i j;
end;
quit;

/*  PRINT Statement  */
proc optmodel;
num x = 4.3;
var y{j in 1..4} init j*3.68;
print y; /* identifier-expression */
print (x * .265) dollar6.2; /* (expression) [format] */
print {i in 2..4} y; /* {index-set} identifier-expression */
print {i in 1..3}(i + i*.2345692) best7.;
/* {index-set} (expression) [format] */
print "Line 1"; /* string */
quit;

proc optmodel;
num a{i in 1..3} = i*i;
num b{i in 3..5} = 4*i;
print a b;
quit;

proc optmodel;
print {i in 1..6, j in i..6} (i*10+j);
quit;

proc optmodel;
num a{i in 1..6, i..i} = i;
num b{i in 1..3, j in 1..3} = i*j;
print a;
print b;
reset options pmatrix=3;
print a;
reset options pmatrix=0.5;
print b;
quit;

/*  READ Statement: Key Columns  */
data invdata;
input item \$ invcount;
datalines;
table 100
sofa  250
chair 80
;

proc optmodel;
set<string> Items;
number invcount{Items};
read data invdata into Items=[item] invcount;
print invcount;
quit;

/*  READ Statement: Columns  */
data exdata;
input column1 column2;
datalines;
1 2
3 4
;

proc optmodel;
number n init 2;
set<num> indx;
number p{indx}, q{indx};
read data exdata into
indx=[_N_] p=column1 q=col("column"||n);
print p q;

data dmnd;
input loc \$ day1 day2 day3 day4 day5;
datalines;
East 1.1 2.3 1.3 3.6 4.7
West 7.0 2.1 6.1 5.8 3.2
;

proc optmodel;
set DOW = 1..5;  /* days of week, 1=Monday, 5=Friday */
set<string> LOCS; /* locations */
number demand{LOCS, DOW};
read data dmnd
into LOCS=[loc]
{d in DOW} < demand[loc, d]=col("day"||d) >;
print demand;
quit;

/*  SAVE MPS Statement  */
proc optmodel;
var x >= 0, y >= 0;
con c: x >= y;
con bx: x <= 2;
con by: y <= 1;
min obj=0.5*x-y;
save mps MPSData;
quit;

proc optlp data=MPSData pout=PrimalOut dout=DualOut;
run;

/*  SAVE QPS Statement  */
proc optmodel;
var x{1..2} >= 0;
min z = 2*x[1] + 3 * x[2] + x[1]**2 + 10*x[2]**2
+ 2.5*x[1]*x[2];
con c1: x[1] - x[2] <= 1;
con c2: x[1] + 2*x[2] >= 100;
save qps QPSData;
quit;

proc optqp data=QPSData pout=PrimalOut dout=DualOut;
run;

/*  STOP Statement  */
proc optmodel;
number i, j;
do i = 1..5;
do j = 1..4;
if i = 3 and j = 2 then stop;
end;
end;
print i j;
quit;

/* SUBMIT Statement */
proc optmodel;
set<str> NODES;
num priority{NODES};

/* set up priority data... */
NODES = setof{i in 1..26} substr('ABCDEFGHIJKLMNOPQRSTUVWXYZ',i,1);
for {id in NODES} priority[id] = 100*ranuni(12345);

/* sort nodes by descending priority */
create data temppri from [id] priority;
submit;
proc sort;
by descending priority;
run;
endsubmit;

/* load nodes by priority */
str nodesByPri{i in 1..card(NODES)};
read data temppri into [_n_] nodesByPri=id;

/* use the sorted list... */
print nodesByPri {i in 1..card(NODES)} priority[nodesByPri[i]];
quit;

/* SUBMIT Statement Error Handling */
proc optmodel;
num success, syscc;
submit / OK = success out syscc;
data example;
set notfound;
j = i*i;
run;
endsubmit;
print success syscc;
quit;

```