Hello,
I want to ask about concatenate data sets.
What is the difference between Way2 and Way3 ?
Is Way3 correct ?
Is it essential to use macro with DO (Way2) ?
data tbl1;
input a;
cards;
1
2
3
;
run;
data tbl2;
input a;
cards;
4
5
6
;
run;
/*Way1*/
data wanted;
set tbl1 tbl2;
run;
/*Way2*/
%macro combine;
data wanted;
set
%do i = 1 %to 2;
tbl&i
%end;
;
run;
%mend;
%combine;
/*Way3*/
data wanted;
set
do i = 1 to 2;
tbl&i
end;
;
run;
1 and 2 create identical code, so they are equivalent; 2 has the advantage that you can easily make the number of datasets dynamic.
3 is syntactically incorrect and will never work.
If you always have only two datasets, use method 1. Method 2 adds unneeded complexity that makes the code harder to maintain, in that case.
@Ronein wrote:
Hello,
I want to ask about concatenate data sets.
What is the difference between Way2 and Way3 ?
Is Way3 correct ?
Is it essential to use macro with DO (Way2) ?
data tbl1; input a; cards; 1 2 3 ; run; data tbl2; input a; cards; 4 5 6 ; run; /*Way1*/ data wanted; set tbl1 tbl2; run; /*Way2*/ %macro combine; data wanted; set %do i = 1 %to 2; tbl&i %end; ; run; %mend; %combine; /*Way3*/ data wanted; set do i = 1 to 2; tbl&i end; ; run;
1 and 2 create identical code, so they are equivalent; 2 has the advantage that you can easily make the number of datasets dynamic.
3 is syntactically incorrect and will never work.
If you always have only two datasets, use method 1. Method 2 adds unneeded complexity that makes the code harder to maintain, in that case.
@Ronein wrote:
Hello,
I want to ask about concatenate data sets.
What is the difference between Way2 and Way3 ?
Is Way3 correct ?
Is it essential to use macro with DO (Way2) ?
data tbl1; input a; cards; 1 2 3 ; run; data tbl2; input a; cards; 4 5 6 ; run; /*Way1*/ data wanted; set tbl1 tbl2; run; /*Way2*/ %macro combine; data wanted; set %do i = 1 %to 2; tbl&i %end; ; run; %mend; %combine; /*Way3*/ data wanted; set do i = 1 to 2; tbl&i end; ; run;
Already explained by @Kurt_Bremser who said it is syntactically incorrect. You cannot have DO in a SET statement.
@Ronein wrote:
Thanks, may you please explain why way3 is incorrect?
Run the code.
Read the error messages.
If you have more than two of these numbered datasets then use a dataset list in the SET statement.
set tbl1-tbl5 ;
Or the upper bound for the number of dataset varies.
%let n=5;
....
set tbl1-tbl&n;
Or if you have "many" data sets with "non-sequential suffix" you can make it:
set tbl: ;
so all data sets with the "tbl" prefix will be included.
And by using the "indsname=" option you can get the name of all data set read int a variable:
data want;
set tbl: indsname=indsname ;
tbl_name = indsname;
run;
All the best
Bart
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.