## proc optmodel

Solved
Occasional Contributor
Posts: 6

# proc optmodel

Can I enter proc optomodel inside a loop

Accepted Solutions
Solution
‎05-01-2017 06:40 PM
SAS Super FREQ
Posts: 4,241

## Re: proc optmodel

[ Edited ]

The DO loop in SAS/IML does not support the comma syntax, thus the statement

do i=21 to 30, 51 to 60;

is invalid.

Instead, put those values into a vector and use vectorized subscript computations:

``````idx = (21:30) || (51:60);F1 = b[,1]; F2 = b[,2];
ss = sum( F1[idx]=0  & F2[idx]=1 );
print ss;``````

The vectorized computation is faster and simpler than the equivalent computation that uses loops:

``````/* you could do this, but not efficient */F1 = b[,1]; F2 = b[,2];
ss=0;
do i=21 to 30;
If ( F1[i] =0) & (F2[i]=1) then ss=ss+1;
end;
do i=51 to 60;
If ( F1[i] =0) & (F2[i]=1) then ss=ss+1;
end;
print ss;
``````

By the way, the data you posted seems to be corrupted. In the future please paste code into the SAS Code Window by clicking the "running man icon" in the editor while posting your question,

All Replies
PROC Star
Posts: 1,283

## Re: proc optmodel

I'm guessing that you mean proc optmodel. Yes you can, but why?

Occasional Contributor
Posts: 6

## Re: proc optmodel

I want to make a simulation on the model results

attachments. -##
PROC Star
Posts: 1,283

## Re: proc optmodel

You have to be more specific than this, otherwise it is imposible to help you.

SAS Super FREQ
Posts: 4,241

## Re: proc optmodel

For simulation and optimization, I recommend SAS/IML. It is easy to write a loop that simulates data, computes or optimizes some quantity, and stores the results.  search for your relevant terms at The DO Loop blog. For example, search

optimize simulation site:blogs.sas.com/content/iml

Occasional Contributor
Posts: 6

## Re: proc optmodel

Ok. I will try it
Occasional Contributor
Posts: 6

## Re: proc optmodel

Actually i generated normal data in proc iml and simulated this data twice as an example by do loops and then i used optmodel - milp to manupilate this data . After i finished proc optmodel how i can count some statistics on the result data . I used do loops inside the proc optmodel but it counts wrongly.
do o=1 to 2;
e=e+20;
do i=1 to 10;
e=e+1;
If (I1 [e] =0) & (I2[e]=1) then ss=ss+1;
end;
end;
print ss;
SAS Super FREQ
Posts: 4,241

## Re: proc optmodel

Occasional Contributor
Posts: 6

## Re: proc optmodel

Please, why ss does not get a value?.

DATA WORK.New_Text_Document;
LENGTH
F1 8
F2 8
F3 8
F4 8
F5 8 ;
FORMAT
F1 BEST1.
F2 BEST1.
F3 BEST1.
F4 BEST7.
F5 BEST8. ;
INFORMAT
F1 BEST1.
F2 BEST1.
F3 BEST1.
F4 BEST7.
F5 BEST8. ;
INFILE DATALINES4
DLM='7F'x
MISSOVER
DSD ;
INPUT
F1 : ?? BEST1.
F2 : ?? BEST1.
F3 : ?? BEST1.
F4 : ?? COMMA7.
F5 : ?? COMMA8. ;
DATALINES4;
0109.365130
1119.630960
1008.310980
1118.820350
1119.023230
0103.192150
1007.679030
1116.142340
1118.020490
0104.965750
1119.033690
1006.774830
1117.470340
1118.561580
1008.063980
1117.474830
1117.82560
0107.572180
0107.088570
0103.892410
010011.66423
01004.30379
11100.37147
01003.57818
01004.88174
1110.375930
010011.08929
1110.444490
010020.29377
01003.13584
1118.113120
1116.638390
1113.286920
1007.489110
1119.552770
1008.573120
1119.371160
1118.124820
0105.906030
1118.056990
1119.413770
1117.662170
1119.216390
0109.095990
1118.040780
1119.422470
1119.259880
1118.537220
1117.34480
1118.672790
01000.23079
11103.21347
01004.45631
01009.34735
0102.529350
1116.108980
1114.90730
1115.174460
01001.27971
1115.091620
;;;;
proc iml;

use WORK.New_Text_Document;
read all var {F1 F2 F3 F4 F5 } into b;
print b;
i=0;
ss=0;
do i=21 to 30, 51 to 60;
If ( F1[i] =0) & (F2[i]=1) then ss=ss+1;
end;
print ss;
quit;

Solution
‎05-01-2017 06:40 PM
SAS Super FREQ
Posts: 4,241

## Re: proc optmodel

[ Edited ]

The DO loop in SAS/IML does not support the comma syntax, thus the statement

do i=21 to 30, 51 to 60;

is invalid.

Instead, put those values into a vector and use vectorized subscript computations:

``````idx = (21:30) || (51:60);F1 = b[,1]; F2 = b[,2];
ss = sum( F1[idx]=0  & F2[idx]=1 );
print ss;``````

The vectorized computation is faster and simpler than the equivalent computation that uses loops:

``````/* you could do this, but not efficient */F1 = b[,1]; F2 = b[,2];
ss=0;
do i=21 to 30;
If ( F1[i] =0) & (F2[i]=1) then ss=ss+1;
end;
do i=51 to 60;
If ( F1[i] =0) & (F2[i]=1) then ss=ss+1;
end;
print ss;
``````

By the way, the data you posted seems to be corrupted. In the future please paste code into the SAS Code Window by clicking the "running man icon" in the editor while posting your question,

Occasional Contributor
Posts: 6

## Re: proc optmodel

I am Sorry, but I don't know where is the running man icon.

I run the two methods, the first method gets error and this is the log.
128
129 use WORK.New_Text_Document;
130 read all var {F1 F2 F3 F4 F5 } into b;
131 print b;
132 i=0;
133 ss=0;
134 /*do i=21 to 30;
135 if (F1 [i] =0 & F2[i]=1 then ss=ss+1;
136 end;
137 do i=51 to 60;
138 if (F1 [i] =0 & F2[i]=1 then ss=ss+1;
139 end;
140 print ss;*/
141 i=(21:30)||(41:50);
142 ss=sum(F1 [i] =0 & F2[i]=1);
ERROR: (execution) Matrix has not been set to a value.

operation : [ at line 142 column 11
operands : F1, i

Also,The second method by do loops gets error in the log

129 use WORK.New_Text_Document;
130 read all var {F1 F2 F3 F4 F5 } into b;
131 print b;
132 ss=0;
133 do i=21 to 30;
134 if (F1 [i] =0 & F2[i]=1 then ss=ss+1;
____
79
ERROR 79-322: Expecting a ).

135 end;
ERROR: END does not occur within DO group at line=135 col=1.
The last thing, I run this program on 2 intervals of data. What if I have
large data and I want to run for many intervals of data (which are
systematic intervals). For example, after 20 observations, I choose 10
observations and so on. How I can do that?
THANKS,

Show quoted text
SAS Super FREQ
Posts: 4,241

## Re: proc optmodel

1. The running man icon looks like this:

2. Sorry, I didn't realize you had read the variables into the matrix B.  I have edited my previous answer to add the line

F1 = b[,1]; F2 = b[,2];

which extracts the first column of B into the vector F1 and the second column into the vector F2.

☑ This topic is solved.