The OPTMODEL Procedure

Example 9.2 Reading From and Creating a Data Set

This example demonstrates how to use the READ DATA statement to read parameters from a SAS data set. The objective is the Bard function, which is the following least squares problem with upper I equals StartSet 1 comma 2 comma ellipsis comma 15 EndSet:

f left-parenthesis x right-parenthesis equals one-half sigma-summation Underscript k element-of upper I Endscripts left-bracket y Subscript k Baseline minus left-parenthesis x 1 plus StartFraction k Over v Subscript k Baseline x 2 plus w Subscript k Baseline x 3 EndFraction right-parenthesis right-bracket squared
x equals left-parenthesis x 1 comma x 2 comma x 3 right-parenthesis comma y equals left-parenthesis y 1 comma y 2 comma ellipsis comma y 15 right-parenthesis

where v Subscript k Baseline equals 16 minus k, w Subscript k Baseline equals min left-parenthesis k comma v Subscript k Baseline right-parenthesis (k element-of upper I), and

y equals left-parenthesis 0.14 comma 0.18 comma 0.22 comma 0.25 comma 0.29 comma 0.32 comma 0.35 comma 0.39 comma 0.37 comma 0.58 comma 0.73 comma 0.96 comma 1.34 comma 2.10 comma 4.39 right-parenthesis

The minimum function value f left-parenthesis x Superscript asterisk Baseline right-parenthesis equals 4.107E–3 is at the point left-parenthesis 0.08 comma 1.13 comma 2.34 right-parenthesis. The starting point x Superscript 0 Baseline equals left-parenthesis 1 comma 1 comma 1 right-parenthesis is used. This problem is identical to the example "Using the DATA= Option" in Chapter 6, "The NLP Procedure" (SAS/OR User's Guide: Mathematical Programming Legacy Procedures). The following statements use the READ DATA statement to input parameter values and the CREATE DATA statement to save the solution in a SAS data set:

data bard;
   input y @@;
   datalines;
.14  .18  .22  .25  .29  .32  .35  .39
.37  .58  .73  .96 1.34 2.10 4.39
;
proc optmodel;
   set I = 1..15;
   number y{I};
   read data bard into [_n_] y;
   number v{k in I} = 16 - k;
   number w{k in I} = min(k, v[k]);
   var x{1..3} init 1;
   min f = 0.5*
      sum{k in I}
         (y[k] - (x[1] + k /
                  (v[k]*x[2] + w[k]*x[3])))**2;
   solve;
   print x;
   create data xdata from [i] xd=x;

In these statements the values for parameter y are read from the BARD data set. The set I indexes the terms of the objective in addition to the y array.

The preceding statements define two utility parameters that contain coefficients used in the objective function. These coefficients could have been defined in the expression for the objective, f, but it was convenient to give them names and simplify the objective expression.

The result is shown in Output 9.2.1.

Output 9.2.1: Bard Function Solution

[1]x
10.082411
21.133036
32.343696


The final CREATE DATA statement saves the solution values determined by the solver into the data set XDATA. The data set contains an observation for each x index. Each observation contains two variables. The output variable i contains the index, while xd contains the value for the indexed entry in the array x. The resulting data can be seen by using the PRINT procedure as follows:

proc print data=xdata;
run;

The output from PROC PRINT is shown in Output 9.2.2.

Output 9.2.2: Output Data Set Contents

Obsixd
110.08241
221.13304
332.34370


Last updated: June 22, 2026