I was also playing with this, but naming your macro variables as you described is easier in a data step:
* for debugging, print to output window ;
proc sql;
* zone distinct count ;
select count(0) as zone_count
from (select distinct zone from test);
* zone distinct values ;
select distinct zone as zone_value
from test;
* distinct count of state within zone ;
select zone, state, count(0) as zone_state_count
from (select distinct zone, state from test) group by zone;
* distinct value of state within zone ;
select distinct zone, state as zone_state_value
from test;
* distinct count of branch within within zone + state ;
select zone, state, branch, count(0) as zone_state_branch_count
from (select distinct zone, state, branch from test) group by zone, state;
* distinct value of branch within within zone + state ;
select distinct zone, state, branch as zone_state_branch_value
from test;
quit;
* I don't think you need macro variables, but if you REALLY want them ;
proc sql;
* zone distinct count ;
select count(0) as zone_count into :zone_count trimmed
from (select distinct zone from test);
%put &=zone_count;
* zone distinct values ;
select distinct zone as zone_value into :zone1 - :zone9999
from test;
%put &=zone1;
%put &=zone2;
* distinct count of state within zone ;
* skipping this one... ;
select zone, state, count(0) as zone_state_count
from (select distinct zone, state from test) group by zone;
* distinct value of state within zone ;
select distinct zone, state as zone_state_value
from test;
* distinct count of branch within within zone + state ;
select zone, state, branch, count(0) as zone_state_branch_count
from (select distinct zone, state, branch from test) group by zone, state;
* distinct value of branch within within zone + state ;
select distinct zone, state, branch as zone_state_branch_value
from test;
* at this point I gave up... ;
... View more