Hi i am gaving a trouble with loading data in. This is the error log i have. Does anyone know how to make SAS read data in a correct way?
1 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
68
69 /* Create the distance matrix */
70 data distance_matrix;
71 input loc1 loc2 distance;
72 datalines;
NOTE: The data set WORK.DISTANCE_MATRIX has 36 observations and 3 variables.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
user cpu time 0.01 seconds
system cpu time 0.00 seconds
memory 778.25k
OS Memory 22180.00k
Timestamp 09/18/2024 02:11:35 PM
Step Count 258 Switch Count 2
Page Faults 0
Page Reclaims 102
Page Swaps 0
Voluntary Context Switches 9
Involuntary Context Switches 0
Block Input Operations 0
Block Output Operations 264
109 ;
110 run;
111
112 /* Create the weekly hours dataset */
113 data week_hours;
114 input loc hours;
115 datalines;
NOTE: The data set WORK.WEEK_HOURS has 9 observations and 2 variables.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
user cpu time 0.00 seconds
system cpu time 0.00 seconds
memory 778.25k
OS Memory 22180.00k
Timestamp 09/18/2024 02:11:35 PM
Step Count 259 Switch Count 2
Page Faults 0
Page Reclaims 85
Page Swaps 0
Voluntary Context Switches 9
Involuntary Context Switches 1
Block Input Operations 0
Block Output Operations 264
125 ;
126 run;
127
128 /* Create the revenue dataset */
129 data revenue;
130 input loc revenue;
131 datalines;
NOTE: The data set WORK.REVENUE has 9 observations and 2 variables.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
user cpu time 0.00 seconds
system cpu time 0.00 seconds
memory 778.25k
OS Memory 22180.00k
Timestamp 09/18/2024 02:11:35 PM
Step Count 260 Switch Count 2
Page Faults 0
Page Reclaims 87
Page Swaps 0
Voluntary Context Switches 12
Involuntary Context Switches 0
Block Input Operations 0
Block Output Operations 264
141 ;
142 run;
143
144 data staff;
145 input staff;
146 datalines;
NOTE: The data set WORK.STAFF has 8 observations and 1 variables.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
user cpu time 0.00 seconds
system cpu time 0.00 seconds
memory 778.25k
OS Memory 22180.00k
Timestamp 09/18/2024 02:11:35 PM
Step Count 261 Switch Count 2
Page Faults 0
Page Reclaims 85
Page Swaps 0
Voluntary Context Switches 12
Involuntary Context Switches 0
Block Input Operations 0
Block Output Operations 264
155 ;
156 run;
157
158
159 /* Create datasets for above and below threshold */
160 proc sql;
161 create table above_threshold as
162 select loc1, loc2, distance
163 from distance_matrix
164 where distance > 40;
NOTE: Table WORK.ABOVE_THRESHOLD created, with 5 rows and 3 columns.
165
166 create table below_threshold as
167 select loc1, loc2, distance
168 from distance_matrix
169 where distance <= 40;
NOTE: Table WORK.BELOW_THRESHOLD created, with 31 rows and 3 columns.
170 quit;
NOTE: PROCEDURE SQL used (Total process time):
real time 0.00 seconds
user cpu time 0.01 seconds
system cpu time 0.00 seconds
memory 5899.12k
OS Memory 27560.00k
Timestamp 09/18/2024 02:11:35 PM
Step Count 262 Switch Count 4
Page Faults 0
Page Reclaims 275
Page Swaps 0
Voluntary Context Switches 23
Involuntary Context Switches 0
Block Input Operations 0
Block Output Operations 528
171
172 proc optmodel;
173 /* First model for locations with distances above threshold */
174 set LOCS;
175 set STAFF;
176 number distance{LOCS, LOCS};
177 number hours{LOCS};
178 number revenue{LOCS};
179
180 /* Read the distance data */
181 read data above_threshold into LOCS=[loc1 loc2] distance=distance[loc1, loc2];
________ _
587 22
ERROR 587-782: The set element length does not match key count, 1 NE 2.
ERROR 22-322: Expecting a name.
182
183 /* Read the weekly hours and revenue data */
184 read data week_hours into LOCS=[loc] hours=hours;
NOTE: There were 9 observations read from the data set WORK.WEEK_HOURS.
185 read data revenue into LOCS=[loc] revenue=revenue;
NOTE: There were 9 observations read from the data set WORK.REVENUE.
186 read data staff into STAFF=[staff];
NOTE: There were 8 observations read from the data set WORK.STAFF.
187
188 /* Define your decision variables, objective, and constraints here */
189
190 num wage = 23.45;
191 num max_hours = 40;
192
193 /* declare decision variables */
194 var RequiredHour {staff, LOCS} >= 0;
194 ! /* Hours worked by staff i on site j */
195
196 /* declare objective */
197 max Profit = sum {j in LOCS} revenue[j]
198 - (wage * sum {i in staff, j in LOCS} RequiredHour[i,j])
199 - 0.15 * (wage * sum {i in staff, j in LOCS} RequiredHour[i,j])
200 - 0.04 * (sum {j in LOCS} revenue[j] );
201
202 /* declare constraints */
203 con MaximumHours {i in staff}: sum{j in LOCS} RequiredHour[i,j] <= 40;
204 con Totalhours {j in LOCS}: sum {i in staff} RequiredHour[i,j] = hours[j];
205
206 /* Solve the problem */
207 solve;
NOTE: Problem generation will use 2 threads.
NOTE: Previous errors might cause the problem to be resolved incorrectly.
NOTE: The problem has 72 variables (0 free, 0 fixed).
NOTE: The problem has 17 linear constraints (8 LE, 9 EQ, 0 GE, 0 range).
NOTE: The problem has 144 linear constraint coefficients.
NOTE: The problem has 0 nonlinear constraints (0 LE, 0 EQ, 0 GE, 0 range).
NOTE: The OPTMODEL presolver is disabled for linear problems.
NOTE: The problem is a pure network instance. The ALGORITHM=NETWORK option is recommended for solving problems with this structure.
NOTE: The LP presolver value AUTOMATIC is applied.
NOTE: The LP presolver time is 0.00 seconds.
NOTE: The LP presolver removed 8 variables and 1 constraints.
NOTE: The LP presolver removed 16 constraint coefficients.
NOTE: The presolved problem has 64 variables, 16 constraints, and 128 constraint coefficients.
NOTE: The LP solver is called.
NOTE: The Dual Simplex algorithm is used.
Objective
Phase Iteration Value Time
D 2 1 4.505280E+03 0
P 2 14 5.949925E+02 0
NOTE: Optimal.
NOTE: Objective = 594.9925.
NOTE: The Dual Simplex solve time is 0.00 seconds.
208
209 /* Display the results */
210 print Profit dollar10.2;
211 print 'Allocation';
212 print RequiredHour;
213
214
215 quit;
NOTE: The SAS System stopped processing this step because of errors.
NOTE: PROCEDURE OPTMODEL used (Total process time):
real time 0.04 seconds
user cpu time 0.04 seconds
system cpu time 0.00 seconds
memory 12913.73k
OS Memory 33968.00k
Timestamp 09/18/2024 02:11:35 PM
Step Count 263 Switch Count 18
Page Faults 0
Page Reclaims 385
Page Swaps 0
Voluntary Context Switches 129
Involuntary Context Switches 2
Block Input Operations 0
Block Output Operations 352
216
217 proc optmodel;
218 /* Second model for locations with distances below threshold */
219 set LOCS;
220 number distance{LOCS, LOCS};
221 number hours{LOCS};
222 number revenue{LOCS};
223
224 /* Read the distance data */
225 read data below_threshold into LOCS=[loc1 loc2] distance=distance[loc1, loc2];
________ _
587 22
ERROR 587-782: The set element length does not match key count, 1 NE 2.
ERROR 22-322: Expecting a name.
226
227 /* Read the weekly hours and revenue data */
228 read data week_hours into LOCS=[loc] hours=hours;
NOTE: There were 9 observations read from the data set WORK.WEEK_HOURS.
229 read data revenue into LOCS=[loc] revenue=revenue;
NOTE: There were 9 observations read from the data set WORK.REVENUE.
230
231 /* Define your decision variables, objective, and constraints here */
232
233 NUM n = card(LOCS);
234 NUM eps = 0.00001;
235
236 /* Define variables */
237 var x {LOCS, LOCS} binary;
238 var y {LOCS} binary;
239 var u {LOCS} >= 0;
239 ! /* dummy variable - represents the order in which each location is visited */
240 var NumStaff >= 0 integer;
240 ! /* New variable representing the number of staff */
241 /*impvar NumVan = ceil(NumEmployees/2)
242
243 /* Define flow in and out of all customer nodes */
244 impvar FlowOut {i in LOCS} = sum {j in LOCS: i ne j} x[i,j];
245 impvar FlowIn {j in LOCS} = sum {i in LOCS: i ne j} x[i,j];
246
247 /* Set "arbitrary" starting point */
248 num home = 9;
249
250 /* Declare constraints */
251
252 /* The vehicle must arrive and depart from each customer */
253 con Arrival_con {i in LOCS}: FlowIn[i] = 1;
254 con Departure_con {j in LOCS}: FlowOut[j] = 1;
255
256 /* Remove subtours */
257 con Subtour_elimination_con {i in LOCS, j in LOCS: i ne j & i ne home}:
258 u[i] - u[j] + n*x[i,j] <= n-1;
259 con u_restrict1_con {i in LOCS: i ne home}: u[i] >= 1;
260 con u_restrict2_con {i in LOCS: i ne home}: u[i] <= n - 1;
261
262
263 /* Maximum working hours constraint: actual work time + travel time cannot exceed 40 hours per employee */
264 con Max_Working_Hours_Con:
265 sum {i in LOCS} hours[i] * y[i] +
266 sum {i in LOCS, j in LOCS} (distance[i,j] /60) * x[i,j] <= 40 * NumStaff ;
267
268
269
270
271 /* Declare objective */
272 max TotalProfit =
273 sum {i in LOCS} revenue[i] * y[i]
274 /* sum of travel cost + actual work cost at location */
275 - 40 * 26 * NumStaff
276 - 0.04 * sum {i in LOCS} revenue[i] /* Cleaning Material Cost */
277 - 0.2 * sum {i in LOCS, j in LOCS} distance[i,j] * x[i,j] /* Fuel cost */
278 - 0.15 * 40 * 26 * NumStaff
279 - 20 * n
280
281 ;
282 solve;
NOTE: Problem generation will use 2 threads.
NOTE: Previous errors might cause the problem to be resolved incorrectly.
ERROR: The array element 'distance[1,1]' has no value at line 266 column 41.
NOTE: Unable to create problem instance due to previous errors.
283
284 /* Print results */
285 title 'Route';
286 print {i in LOCS, j in LOCS: x[i,j] > 0} x;
287 print u;
288 print FlowOut FlowIn;
289 print NumStaff;
289 ! /* Print number of employees used */
290
291 quit;
NOTE: The SAS System stopped processing this step because of errors.
NOTE: PROCEDURE OPTMODEL used (Total process time):
real time 0.03 seconds
user cpu time 0.03 seconds
system cpu time 0.01 seconds
memory 1168.85k
OS Memory 23212.00k
Timestamp 09/18/2024 02:11:35 PM
Step Count 264 Switch Count 4
Page Faults 0
Page Reclaims 151
Page Swaps 0
Voluntary Context Switches 56
Involuntary Context Switches 1
Block Input Operations 0
Block Output Operations 224
292
293
294
295 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
305
Please note that we previously requested you to show us the log for the PROC that has the error only. We don't need the log before that PROC.
You have declared LOCS as a set of singletons, but the first READ DATA statement specifies to populate LOCS as a set of pairs. Instead, you should first populate LOCS:
/* Read the weekly hours and revenue data */
read data week_hours into LOCS=[loc] hours=hours;
And then read the distance data:
/* Read the distance data */
read data above_threshold into [loc1 loc2] distance=distance[loc1, loc2];
You can alternatively shorten the statement as follows:
read data above_threshold into [loc1 loc2] distance;
You can find many more examples of reading various types of data in this book of examples:
SAS Help Center: SAS/OR® User's Guide: Mathematical Programming Examples
Hi.
Thank you for the advice. I have made sure to read data properly, however the program still says my distance element has no value.
264
265
266 con Max_Working_Hours_Con:
267 sum {i in LOCS} hours[i] * y[i] +
268 sum {i in LOCS, j in LOCS} (distance[i,j] /60) * x[i,j] <= 40 * NumStaff ;
269
270
271
272
273 /* Declare objective */
274 max TotalProfit =
275 sum {i in LOCS} revenue[i] * y[i]
276 /* sum of travel cost + actual work cost at location */
277 - 40 * 26 * NumStaff
278 - 0.04 * sum {i in LOCS} revenue[i] /* Cleaning Material Cost */
279 - 0.2 * sum {i in LOCS, j in LOCS} distance[i,j] * x[i,j] /* Fuel cost */
280 - 0.15 * 40 * 26 * NumStaff
281 - 20 * n
282
283 ;
284 solve;
NOTE: Problem generation will use 2 threads.
ERROR: The array element 'distance[1,1]' has no value at line 268 column 43.
NOTE: Unable to create problem instance due to previous errors.
this is my data
/* Create the distance matrix */
data distance_matrix;
input loc1 $ loc2 $ distance;
datalines;
s1 s2 42.00467
s1 s3 44.1595
s1 s4 23.43201
s1 s5 41.38763
s1 s6 56.03623
s1 s7 51.65744
s1 s8 38.74528
s1 s9 0
s2 s3 3.50019
s2 s4 18.70469
s2 s5 0.76708
s2 s6 15.58126
s2 s7 14.86843
s2 s8 4.31273
s2 s9 0
s3 s4 21.17541
s3 s5 3.58332
3s s6 15.60379
s3 s7 16.43565
s3 s8 7.74752
s3 s9 0
s4 s5 18.13469
s4 s6 32.69996
s4 s7 28.85598
s4 s8 15.31329
s4 s9 0
s5 s6 16.34423
s5 s7 15.58827
s5 s8 4.18141
s5 s9 0
s6 s7 7.08766
s6 s8 17.5934
s6 s9 0
s7 s8 15.05738
s7 s9 0
s8 s9 0
;
run;/* Create datasets for above and below threshold */
proc sql;
create table above_threshold as
select loc1, loc2, distance
from distance_matrix
where distance > 40;
create table below_threshold as
select loc1, loc2, distance
from distance_matrix
where distance <= 40;
quit;
and my read data
proc optmodel;
/* Second model for locations with distances below threshold */
set <str> LOCS;
number distance{LOCS, LOCS};
number hours{LOCS};
number revenue{LOCS};
/* Read the weekly hours and revenue data */
read data week_hours into LOCS=[loc] hours=hours;
read data revenue into LOCS=[loc] revenue=revenue;
/* Read the distance data */
read data below_threshold into [loc1 loc2] distance;You declare the distance parameter over the full Cartesian product LOCS cross LOCS but then populate it only for pairs <loc1,loc2> where the distance would be <= 40. So you get an error because the model uses all distance[i,j] values.
Are you trying to disallow using arcs whose distances exceed 40? If so, you should use a sparse index set, as in this doc example:
I see. Thank you very much for the explanation where the error comes from. I will take a look at the sparse modelling. I was just trying to allocate the pair of distance < 30 to one proc optmodel and the pair of distance > 30 to another pro optmodel code. This was just the idea.
But I think this is not the right logic or not applicable in SAS.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.