DATA Step, Macro, Functions and more

Merging datasets using sas macro code?

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

Merging datasets using sas macro code?

Dear all,

 

I'm quite new to macro, and my question is:

I'm trying to merge 12 datasets together, named data_1 to data_12 with id and already sorted, and my code is

%macro mergedata;

%do i=1 %to 12;

data merged_data;

merge data_&i;

by id;

%end;

run;

%mend;

 

and it resulted in only the data from data_12. it seems that data_1 to data_11 were replaced and disappeared.

How should I rewrite that? Thank you very much!


Accepted Solutions
Solution
‎07-04-2017 10:35 PM
Regular Contributor
Posts: 234

Re: Merging datasets using sas macro code?

[ Edited ]
%macro mergedata;
data merged_data;
  merge
%do i=1 %to 12;
 data_&i
%end;
  ;
  by id;
run;
%mend;

 

View solution in original post


All Replies
Solution
‎07-04-2017 10:35 PM
Regular Contributor
Posts: 234

Re: Merging datasets using sas macro code?

[ Edited ]
%macro mergedata;
data merged_data;
  merge
%do i=1 %to 12;
 data_&i
%end;
  ;
  by id;
run;
%mend;

 

Super User
Posts: 5,093

Re: Merging datasets using sas macro code?

When writing a macro language program, you always, always, always have to know what the program would look like if you were not using macro language.  

 

@SAS_inquisitive has the right idea here.  It just needs a slight tweak, removing the extra semicolon here:

 

%do;

New Contributor
Posts: 3

Re: Merging datasets using sas macro code?

thanks a lot for advice. really helped a lot !
New Contributor
Posts: 3

Re: Merging datasets using sas macro code?

solved! thanks a lot
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 130 views
  • 2 likes
  • 3 in conversation