BookmarkSubscribeRSS Feed
yus03590
Calcite | Level 5

Hi!  I'm new to SAS OR.  I've having trouble reading data from a two dimensional array in the read data statement in proc optmodel.  

Here is the data:

data Preferences;
	length name $30. _6am_7am 3. _7am_8am 3._8am_9am 3. _9am_10am 3. _10am_11am 3. _11am_12pm 3. _12pm_1pm 3. _1pm_2pm 3. _2pm_3pm 3. _3pm_4pm 3. _4pm_5pm 3. _5pm_6pm 3. _6pm_7pm 3.;
	input name $ _6am_7am _7am_8am _8am_9am _9am_10am _10am_11am _11am_12pm _12pm_1pm _1pm_2pm _2pm_3pm _3pm_4pm _4pm_5pm _5pm_6pm _6pm_7pm;
datalines;
Evan_Boyak 1 1 0 0 0 3 3 3 4 4 3 0 0
Andrew_Miller 1 1 1 1 3 2 0 0 0 4 4 4 1
Jess_Whitaker 1 1 3 3 0 4 4 2 4 4 2 1 1
Rachel_Hartshorn 4 4 0 0 4 0 0 2 2 4 1 1 1
Mark_Hamilton 1 1 1 0 0 0 3 3 2 2 2 1 1
Jace_Webster 1 1 2 1 1 3 0 0 0 3 3 4 4
Brexton_Simonsen 0 0 1 4 4 0 3 0 0 4 1 2 4
Levi_Morse 1 1 2 2 2 2 1 1 0 0 0 0 0
Jhonny_Loaiza 2 3 0 0 3 3 2 3 4 4 4 4 3
Mitch_Lindeman 3 4 2 2 1 1 1 1 2 2 2 1 1
Joel_LaPray 1 1 0 2 0 0 2 3 3 1 1 1 4
Jenna_Hamilton 1 1 1 1 1 2 2 2 2 4 4 4 2
Dallin_Miller 1 1 0 1 0 4 0 4 4 4 2 2 1
Jimmothy_Winters 1 1 2 0 0 0 2 2 2 3 3 0 0
Daniel_Barrio 1 1 2 4 0 2 2 0 0 4 4 4 4
Alex_Walters 0 0 3 3 3 3 3 3 1 1 1 2 2
Gideon_Stowell 1 1 1 1 2 4 0 0 0 0 0 1 1
Landon_Dyer 1 1 1 2 2 2 2 2 4 4 4 4 1

Here is my code:

proc optmodel;
set <string> employees;

set <string> shifts = {"_6am_7am", "_7am_8am", "_8am_9am", "_9am_10am", "_10am_11am", "_11am_12pm", "_12pm_1pm", "_1pm_2pm", "_2pm_3pm", "_3pm_4pm", "_4pm_5pm", "_5pm_6pm", "_6pm_7pm"};
read data Preferences into employees=[name];
num preferencesArray{employees, shifts};

for {name in employees} read data Preferences into {sh in shifts} <preferencesArray[name, sh] = col(sh)>;

for {name in employees} for {sh in shifts}
	put preferencesArray[name, sh]=;

quit;

In my log, every employee has the same shift preferences.  Only the first line of preference weights is being read.  What is the syntax for reading in such a two dimensional array?

 

Thanks!

1 REPLY 1
RobPratt
SAS Super FREQ

Your FOR loop was reading the first line of the data set each time.  To read one observation per employee (with name as the key column), do this instead:

read data Preferences into [name] {sh in shifts} <preferencesArray[name, sh] = col(sh)>;

 

You can in fact combine your two READ DATA statements as follows:

read data Preferences into employees=[name] {sh in shifts} <preferencesArray[name, sh] = col(sh)>;

For more examples, see SAS/OR User's Guide: Mathematical Programming Examples.  The pdf version has a subject index that contains entries on reading various types of data, including two-dimensional.

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

Discussion stats
  • 1 reply
  • 1484 views
  • 0 likes
  • 2 in conversation