BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
jesperph
Calcite | Level 5

Hello. I'm really new to SAS Studio programming and I have a question about my code.

 

I have a dataset consisting of numbers regarding different products. I have in total 31 products with different values of the numbers stated in [box] (c1, D, cap1) etc. The thing is that I want to loop the minimalization object function Z for all 31 products so that they don't affect each other's values of the variable Q and Z.

 

Which it seems like they do now since I'm not doing a loop for one product at the time. But I just can't figure out how I'm going to do it.

 

proc import out=indata 
 DATAFILE='/home/phjesper0/Master/Minresultater1.xlsx'
 DBMS=XLSX replace;
 GETNAMES=YES;
 /*USEDATE=YES;*/

title 'Vanlige tall';
proc print data=indata;
run;

Proc optmodel;
   set <str> box;
   var Q{box} >=0;
   number   c1{box};
   number    D{box};
   number cap1{box};
   number   r1{box};
   number   r2{box};
   number   o1{box};
   number    f{box};
   number   o2{box};
   number   c2{box};
   number   p1{box};
   number   p2{box};
   number cap2{box};
   number life{box};
   number    w{box};

read data indata into box=[type] c1 D cap1 r1 r2 o1 f o2 c2 p1 p2 cap2 life w;
   print c1 D cap1 r1 r2 o1 f o2 c2 p1 p2 cap2 life w;

minimize Z = sum {i in box} (((Q[i]/2)*((c1[i]*r1[i])+(c2[i]*r2[i]*f[i])))
+(D[i]/Q[i])*(((o1[i]*p1[i]) + (o2[i]*p2[i]*f[i]))*300 + (w[i]*c1[i])));

con MinShelfLife{i in box}:(life[i]-((360)/(D[i]/Q[i]))) >= 120;
con MaxShiftSize1{i in box}: Q[i]/cap1[i] <= 2;
con MaxShiftSize2{i in box}: Q[i]/(f[i]*cap2[i]) <= 2;
con MinBatch{i in box}: Q[i] >= 0;
solve;
print Z Q;
quit;

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
RobPratt
SAS Super FREQ

If you want to solve a separate problem for each i in box, you can do the following:

Proc optmodel;
   set <str> box;
   var Q >= 0;
   number   c1{box};
   number    D{box};
   number cap1{box};
   number   r1{box};
   number   r2{box};
   number   o1{box};
   number    f{box};
   number   o2{box};
   number   c2{box};
   number   p1{box};
   number   p2{box};
   number cap2{box};
   number life{box};
   number    w{box};

   read data indata into box=[type] c1 D cap1 r1 r2 o1 f o2 c2 p1 p2 cap2 life w;
   print c1 D cap1 r1 r2 o1 f o2 c2 p1 p2 cap2 life w;

   str i_this;
   minimize Z = ((Q/2)*((c1[i_this]*r1[i_this])+(c2[i_this]*r2[i_this]*f[i_this])))
   +(D[i_this]/Q)*(((o1[i_this]*p1[i_this]) + (o2[i_this]*p2[i_this]*f[i_this]))*300 + (w[i_this]*c1[i_this]));

   con MinShelfLife:(life[i_this]-((360)/(D[i_this]/Q))) >= 120;
   con MaxShiftSize1: Q/cap1[i_this] <= 2;
   con MaxShiftSize2: Q/(f[i_this]*cap2[i_this]) <= 2;

   num Qsol {box};
   num Zsol {box};
   for {i in box} do;
      i_this = i;
      solve;
      print Q Z;
      Qsol[i] = Q;
      Zsol[i] = Z;
   end;
   print Qsol Zsol;
quit;

Note that I removed the MinBatch constraint, which is redundant because of the >= 0 in the VAR statement.

 

By the way, you can also solve these independent problems concurrently by changing FOR to COFOR.

 

For a similar example, see Efficiency Analysis: How to Use Data Envelopment Analysis to Compare Efficiencies of Garages.

View solution in original post

2 REPLIES 2
RobPratt
SAS Super FREQ

If you want to solve a separate problem for each i in box, you can do the following:

Proc optmodel;
   set <str> box;
   var Q >= 0;
   number   c1{box};
   number    D{box};
   number cap1{box};
   number   r1{box};
   number   r2{box};
   number   o1{box};
   number    f{box};
   number   o2{box};
   number   c2{box};
   number   p1{box};
   number   p2{box};
   number cap2{box};
   number life{box};
   number    w{box};

   read data indata into box=[type] c1 D cap1 r1 r2 o1 f o2 c2 p1 p2 cap2 life w;
   print c1 D cap1 r1 r2 o1 f o2 c2 p1 p2 cap2 life w;

   str i_this;
   minimize Z = ((Q/2)*((c1[i_this]*r1[i_this])+(c2[i_this]*r2[i_this]*f[i_this])))
   +(D[i_this]/Q)*(((o1[i_this]*p1[i_this]) + (o2[i_this]*p2[i_this]*f[i_this]))*300 + (w[i_this]*c1[i_this]));

   con MinShelfLife:(life[i_this]-((360)/(D[i_this]/Q))) >= 120;
   con MaxShiftSize1: Q/cap1[i_this] <= 2;
   con MaxShiftSize2: Q/(f[i_this]*cap2[i_this]) <= 2;

   num Qsol {box};
   num Zsol {box};
   for {i in box} do;
      i_this = i;
      solve;
      print Q Z;
      Qsol[i] = Q;
      Zsol[i] = Z;
   end;
   print Qsol Zsol;
quit;

Note that I removed the MinBatch constraint, which is redundant because of the >= 0 in the VAR statement.

 

By the way, you can also solve these independent problems concurrently by changing FOR to COFOR.

 

For a similar example, see Efficiency Analysis: How to Use Data Envelopment Analysis to Compare Efficiencies of Garages.

jesperph
Calcite | Level 5
Thank you so much sir!
Worked well.

That was quick and super helpful 🙂

- Jesper

sas-innovate-white.png

Missed SAS Innovate in Orlando?

Catch the best of SAS Innovate 2025 — anytime, anywhere. Stream powerful keynotes, real-world demos, and game-changing insights from the world’s leading data and AI minds.

 

Register now

Discussion stats
  • 2 replies
  • 1397 views
  • 0 likes
  • 2 in conversation