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

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
Meteorite | Level 14
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
Onyx | Level 15

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



sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 601 views
  • 5 likes
  • 6 in conversation