It needs two passes to have it done. First one needs get unique city name list. Second, based upon the result proper sas program needs to be generated. If a city name will be a data set name, it must be concur with SAS name convention.
data city;
length city $20;
do city='shanghai','bejing','nanjin','tianjing';
n=ceil(ranuni(123)*10);
do i=1 to n;
othervar=rannor(123);
output;
end;
end;
run;
%macro split(dsn_in=);
proc sql noprint;
select distinct city into: city1 - : city3000
from city;
%let n=&sqlobs;
quit;
data %do i=1 %to &n;
&&city&i
%end;
;
set &dsn_in;
if city="&city1" then output &city1;
%do i=2 %to &n-1;
else if city="&&city&i" then output &&city&i;
%end;
else output &&city&n;
run;
%mend;
options mprint;
%split(dsn_in=city);
data city;
length city $20;
do city='shanghai','bejing','nanjin','tianjing';
n=ceil(ranuni(123)*10);
do i=1 to n;
othervar=rannor(123);
output;
end;
end;
run;
proc sql noprint;
select distinct strip("_"||city) into : citylist separated by " "
from city;
quit;
%put &citylist;
data _null_;
length code $1000;
retain code;
set city end=last;
by city notsorted;
if _n_=1 then code="data &citylist; set city;";
if first.city then do;
count+1;
if count=1 then code=strip(code)||"if city="||quote(strip(city))||" then output _"||strip(city)||";";
else if count>1 then code=strip(code)||"else if city="||quote(strip(city))||" then output _"||strip(city)||";";
end;
if last then do;
code=strip(code)||" run;";
put code=;
call execute(code);
end;