BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Ronein
Onyx | Level 15

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 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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;

 

View solution in original post

6 REPLIES 6
Kurt_Bremser
Super User

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;

 

Ronein
Onyx | Level 15
Thanks, may you please explain why way3 is incorrect?
PaigeMiller
Diamond | Level 26

Already explained by @Kurt_Bremser who said it is syntactically incorrect. You cannot have DO in a SET statement.

--
Paige Miller
ballardw
Super User

@Ronein wrote:
Thanks, may you please explain why way3 is incorrect?

Run the code.

Read the error messages.

Tom
Super User Tom
Super User

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;
yabwon
Amethyst | Level 16

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

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

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
  • 6 replies
  • 1413 views
  • 5 likes
  • 6 in conversation