Following @ballardw's suggestion, here is an example. The goal here is to get no state with 3 or more selected units and at most two states with 2 selected units for any given zip code termination digit.
data frame;
call streamInit(17646);
do state = 1 to 50;
do id = 1 to 1000;
zip = int(10*rand("UNIFORM"));
output;
end;
end;
run;
proc sort data=frame; by zip state; run;
%macro mySurvey;
%do %until(&n3=0 AND &n2<=2);
proc surveyselect data=frame out=sample sampsize=10;
strata zip;
run;
proc sql;
select
max(n3s),
max(n2s)
into :n3, :n2
from (
select
sum(n >= 3) as n3s,
sum(n >= 2) - sum(n >= 3) as n2s
from (
select
zip,
count(*) as n
from sample
group by zip, state)
group by zip)
;
quit;
%end;
%mend mySurvey;
%mySurvey;
proc sql;
select zip, state, count(*) as n from sample group by zip, state;
quit;
... View more