BookmarkSubscribeRSS Feed
BPD
Obsidian | Level 7 BPD
Obsidian | Level 7
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
SASPhile
Quartz | Level 8
Thanks a ton to you all for giving me the necessary inputs for my question.
I couldnt incorporate any of the inputs as I was pressed for time to give my deliverable,I did learn new things looking at your coding styles.
As part of the solution what I did was:

%macro temp(n1= ,n=2);
%do i= &n1 %to &n2;
SAS code
%end;

%mend temp;
%temp(n1=2, n2=16);
%temp(n1=17, n2=17);
%temp(n1=19, n2=19);
%temp(n1=23, n2=27);

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 16 replies
  • 2700 views
  • 0 likes
  • 7 in conversation