If I want to generate code fragments, I do this before I use the fragment.
The following code is for illustration:
%macro test;
%global foobar;
%let foobar = ;
%let foo1 = 1;
%let foo2 = 1;
%let foo3 = 1;
%let foo4 = 1;
%let foobar = &foo1.;
%do bar = 2 %to 4;
%let foobar = %sysfunc(cats(&foobar., &&foo&bar..));
%put *&foobar.*;
%end;
%mend;
%test;
Now you kann use the macrovariable foobar wherever you like. e.g.
declare hash hash&foobar.();
Aftersome research I finally found a solution in http://www2.sas.com/proceedings/sugi28/011-28.pdf
You need to unquote the macro loop. I don't know why, but the following code is working:
%macro test3();
%global foobar;
%let foobar =;
%let foo1 = 1;
%let foo2 = 1;
%let foo3 = 1;
%let foo4 = 1;
data test2;
declare hash hash%unquote(%do bar = 1 %to 4;&&foo&bar..%end;)();
run;
%mend;
%test3;
The following statement of the SUGI-paper lead to solution:
"In any case, the older documentation gave the simple rule:
If the mprint looks good and the SAS compiler does not understand it, then try %UNQUOTE."