A good use case for SAS macros. Here's a link to a tutorial, but I also highly recommend the purple Carpenter book.
%MACRO do_test2;
proc sql;
create table test2 as
select
%DO i = 1 %TO 20;
sum(X&i) as X&1
%IF &i ^= 20 %THEN , ;
%END;
from test
group by region;
quit;
%MEND;
As @user667489 points out, because the macro code is using a %DO loop, the code block must be defined as a named, invokable macro (%MACRO do_test2 . . . %MEND). Once you've defined it as above, you call it like this:
%do_test2;
Since we have this requirement, we can take advantage of the situation and make the number of repetitions a parameter, in case you ever want to vary it. Then the definition is:
%MACRO do_test2(num_reps);
proc sql;
create table test2 as
select
%DO i = 1 %TO &num_reps;
sum(X&i) as X&1
%IF &i ^= &num_reps %THEN , ;
%END;
from test
group by region;
quit;
%MEND;
A good use case for SAS macros. Here's a link to a tutorial, but I also highly recommend the purple Carpenter book.
As @user667489 points out, because the macro code is using a
%DO
loop, the code block must be defined as a named, invokable macro (%MACRO do_test2 . . . %MEND
). Once you've defined it as above, you call it like this:Since we have this requirement, we can take advantage of the situation and make the number of repetitions a parameter, in case you ever want to vary it. Then the definition is:
Which you would call like this: