Skip to content

Instantly share code, notes, and snippets.

@daynebatten
Created April 4, 2015 15:00
Show Gist options
  • Select an option

  • Save daynebatten/e55d94553b91da374f35 to your computer and use it in GitHub Desktop.

Select an option

Save daynebatten/e55d94553b91da374f35 to your computer and use it in GitHub Desktop.
/* Count the number of needed cut types and lumber types */
/* Save in macro variables */
proc sql;
select
count(*)
into
:num_cuts
from
cuts;
select
count(*)
into
:num_lumber
from
lumber;
quit;
/* Generate every possible pattern we can feasibly cut */
data patterns;
array cut_raw{&num_cuts};
array lumber_raw{&num_lumber};
array price_raw{&num_lumber};
array choice{&num_cuts};
i = 1;
eof = 0;
do while(not eof);
set cuts end = eof;
cut_raw{i} = length;
i = i + 1;
end;
i = 1;
eof = 0;
do while(not eof);
set lumber end = eof;
lumber_raw{i} = length;
price_raw{i} = price;
i = i + 1;
end;
do lumber_index = 1 to &num_lumber;
lumber = lumber_raw{lumber_index};
price = price_raw{lumber_index};
do i = 1 to &num_cuts;
choice{i} = 0;
end;
first_maxed = 0;
choice_index = 1;
back = 0;
do while(first_maxed = 0);
the_sum = 0;
do i = 1 to &num_cuts;
the_sum = the_sum + choice{i} * cut_raw{i};
end;
if the_sum > lumber then do;
if choice_index = 1 then first_maxed = 1;
choice{choice_index} = 0;
choice_index = max(1, choice_index - 1);
back = 1;
end;
else if choice_index = &num_cuts then do;
output;
end;
if back = 1 or choice_index = &num_cuts then do;
choice{choice_index} = choice{choice_index} + 1;
back = 0;
end;
else do;
choice_index = min(choice_index + 1, &num_cuts);
end;
end;
end;
keep lumber price choice1-choice%trim(&num_cuts);
run;
/* Count patterns, save in macro variable */
proc sql;
select
count(*)
into
:num_patterns
from
patterns;
quit;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment