Help using Base SAS procedures

proc optmodel

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 6
Accepted Solution

proc optmodel

Can I enter proc optomodel inside a loop

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

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,

View solution in original post


All Replies
PROC Star
Posts: 768

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

##- Please type your reply above this line. Simple formatting, no
attachments. -##
PROC Star
Posts: 768

Re: proc optmodel

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

 

What do you have already?

SAS Super FREQ
Posts: 3,756

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: 3,756

Re: proc optmodel

Please post your code so that we can help you.  Also, describe the problem you are trying to solve.

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;
0109.365130
1119.630960
1008.310980
1118.820350
1119.023230
0103.192150
1007.679030
1116.142340
1118.020490
0104.965750
1119.033690
1006.774830
1117.470340
1118.561580
1008.063980
1117.474830
1117.82560
0107.572180
0107.088570
0103.892410
010011.66423
01004.30379
11100.37147
01003.57818
01004.88174
1110.375930
010011.08929
1110.444490
010020.29377
01003.13584
1118.113120
1116.638390
1113.286920
1007.489110
1119.552770
1008.573120
1119.371160
1118.124820
0105.906030
1118.056990
1119.413770
1117.662170
1119.216390
0109.095990
1118.040780
1119.422470
1119.259880
1118.537220
1117.34480
1118.672790
01000.23079
11103.21347
01004.45631
01009.34735
0102.529350
1116.108980
1114.90730
1115.174460
01001.27971
1115.091620
;;;;
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: 3,756

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.
NOTE: IML Ready
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

NOTE: IML Ready

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: 3,756

Re: proc optmodel

1. The running man icon looks like this:

Region Capture.png

 

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.

Need further help from the community? Please ask a new question.

Discussion stats
  • 11 replies
  • 323 views
  • 2 likes
  • 3 in conversation