The OPTMODEL Procedure |
This example demonstrates PROC OPTMODEL set manipulation operators. These operators are used to compute the set of primes up to a given limit. This example does not solve an optimization problem, but similar set manipulation could be used to set up an optimization model. Here is the code:
proc optmodel; number maxprime; /* largest number to consider */ set composites = union {i in 3..sqrt(maxprime) by 2} i*i..maxprime by 2*i; set primes = {2} union (3..maxprime by 2 diff composites); maxprime = 500; put primes;
The set composites contains the odd composite numbers up to the value of the parameter maxprime. The even numbers are excluded here to reduce execution time and memory requirements. The UNION aggregation operation is used in the definition to combine the sets of odd multiples of for . Any composite number less than the value of the parameter maxprime has a divisor , so the range of can be limited. The set of multiples of can also be started at since smaller multiples are found in the set of multiples for a smaller index.
You can then define the set primes. The odd primes are determined by using the DIFF operator to remove the composites from the set of odd numbers no greater than the parameter maxprime. The UNION operator adds the single even prime, 2, to the resulting set of primes.
The PUT statement produces the result in Output 6.4.1.
Output 6.4.1: PrimesNote that you were able to delay the definition of the value of the parameter maxprime until just before the PUT statement. Since the defining expressions of the SET declarations are handled symbolically, the value of maxprime is not necessary until you need the value of the set primes. Because the sets composites and primes are defined symbolically, their values reflect any changes to the parameter maxprime. You can see this update by appending the following statements to the preceding code:
maxprime = 50; put primes;
The additional statements produce the results in Output 6.4.2. The value of the set primes has been recomputed to reflect the change to the parameter maxprime.
Output 6.4.2: Primes
Copyright © 2008 by SAS Institute Inc., Cary, NC, USA. All rights reserved.