The following code allows you to specify the ranges you require in the call to %Flatten. Note the use of the %quote function in the macro call that masks the ',' symbols so that the macro knows that there is only one parameter being passed.
%macro flatten(inn);
%LOCAL lista list i ;
%LET i = 1;
%DO %UNTIL(%SCAN(&inn,&i,%STR(,)) EQ );
%LET lista = %SCAN(&inn,&i,%STR(,));
%IF %index(&lista,%STR(:)) %THEN
%DO x = %SCAN(&lista,1,':') %TO %SCAN(&lista,2,':');
%LET list = &list&x%STR( );
%END;
%ELSE %DO;
%LET list = &list&lista%STR( );
%END;
%let i =%eval(&i+1);
%END;
data
%LET i = 1;
%DO %UNTIL(%SCAN(&list,&i,%STR( )) EQ ) ;
%unquote(temp%SCAN(&list,&i,%STR( ))(keep=m%SCAN(&list,&i,%STR( ))))
%let i =%eval(&i + 1);
%END;
;
%LET i = 1;
%DO %UNTIL(%SCAN(&list,&i,%STR( )) EQ );
m%SCAN(&list,&i,%STR( )) = %SCAN(&list,&i,%STR( )) + 1;
%let i =%eval(&i+1);
%END;
%LET i = 1;
%DO %UNTIL(%SCAN(&list,&i,%STR( )) EQ );
output temp%SCAN(&list,&i,%STR( ));
%let i =%eval(&i+1);
%END;
run;
%mend flatten;
options nomlogic mprint nosymbolgen;
%flatten(%quote(1:7,14:16,18,30))
The following log is produced by the macrto call:
MPRINT(FLATTEN): data temp1(keep=m1) temp2(keep=m2) temp3(keep=m3) temp4(keep=m4) temp5(keep=m5)
temp6(keep=m6) temp7(keep=m7) temp14(keep=m14) temp15(keep=m15) temp16(keep=m16) temp18(keep=m18)
temp30(keep=m30) ;
MPRINT(FLATTEN): m1 = 1 + 1;
MPRINT(FLATTEN): m2 = 2 + 1;
MPRINT(FLATTEN): m3 = 3 + 1;
MPRINT(FLATTEN): m4 = 4 + 1;
MPRINT(FLATTEN): m5 = 5 + 1;
MPRINT(FLATTEN): m6 = 6 + 1;
MPRINT(FLATTEN): m7 = 7 + 1;
MPRINT(FLATTEN): m14 = 14 + 1;
MPRINT(FLATTEN): m15 = 15 + 1;
MPRINT(FLATTEN): m16 = 16 + 1;
MPRINT(FLATTEN): m18 = 18 + 1;
MPRINT(FLATTEN): m30 = 30 + 1;
MPRINT(FLATTEN): output temp1;
MPRINT(FLATTEN): output temp2;
MPRINT(FLATTEN): output temp3;
MPRINT(FLATTEN): output temp4;
MPRINT(FLATTEN): output temp5;
MPRINT(FLATTEN): output temp6;
MPRINT(FLATTEN): output temp7;
MPRINT(FLATTEN): output temp14;
MPRINT(FLATTEN): output temp15;
MPRINT(FLATTEN): output temp16;
MPRINT(FLATTEN): output temp18;
MPRINT(FLATTEN): output temp30;
MPRINT(FLATTEN): run;
NOTE: The data set WORK.TEMP1 has 1 observations and 1 variables.
NOTE: The data set WORK.TEMP2 has 1 observations and 1 variables.
NOTE: The data set WORK.TEMP3 has 1 observations and 1 variables.
NOTE: The data set WORK.TEMP4 has 1 observations and 1 variables.
NOTE: The data set WORK.TEMP5 has 1 observations and 1 variables.
NOTE: The data set WORK.TEMP6 has 1 observations and 1 variables.
NOTE: The data set WORK.TEMP7 has 1 observations and 1 variables.
NOTE: The data set WORK.TEMP14 has 1 observations and 1 variables.
NOTE: The data set WORK.TEMP15 has 1 observations and 1 variables.
NOTE: The data set WORK.TEMP16 has 1 observations and 1 variables.
NOTE: The data set WORK.TEMP18 has 1 observations and 1 variables.
NOTE: The data set WORK.TEMP30 has 1 observations and 1 variables.
NOTE: DATA statement used (Total process time):
real time 0.25 seconds
cpu time 0.09 seconds
You can see that I've output each of the differently summed/named variables to different named datasets. This may not be quite what you want but you can cherry pick out the bits that you require.
Regards,
BPD