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

Hello, 

I would like to create a macro to merge the sample data sets as I show below.   There are a total of ten of them, I don't list all the them.

data ARIunderly_1617;
	set hou1617_ kc1617_ pit1617_ ;
run;

My macro code is shown below.  I know the code is not right, any idea how to fix it?  Thanks.

%let X1=VAN;
%let X2=ROC;
%let X3=SEA; 

%macro aaa;

%do i  = 1 %to 3;
data underly_1617;
	set &&X&i..1617_;
run;

%end; 
%mend;
%aaa;
1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Your non-macro code shows one DATA step with one SET statement with three datasets, not three separate DATA steps with a SET statement each, so you need to make your macro create such a statement.

Instead of

%do i  = 1 %to 3;
data underly_1617;
	set &&X&i..1617_;
run;
%end; 

you need

data underly_1617;
set
%do i  = 1 %to 3;
  &&X&i..1617_
%end;
; /* this ends the SET */
run;

View solution in original post

7 REPLIES 7
Kurt_Bremser
Super User

Your non-macro code shows one DATA step with one SET statement with three datasets, not three separate DATA steps with a SET statement each, so you need to make your macro create such a statement.

Instead of

%do i  = 1 %to 3;
data underly_1617;
	set &&X&i..1617_;
run;
%end; 

you need

data underly_1617;
set
%do i  = 1 %to 3;
  &&X&i..1617_
%end;
; /* this ends the SET */
run;
ybz12003
Rhodochrosite | Level 12

I got an error message.  Where did I do wrong?

%macro merge;
data underly_1617;
	set
	%do i  = 1 %to 6;
	  &&X&i..1617_
	%end;
run;	
%mend;
%merge;


MPRINT(MERGE): data underly_1617;
MLOGIC(MERGE): %DO loop beginning; index variable I; start value is 1; stop value is 6; by value is
1.
SYMBOLGEN: && resolves to &.
SYMBOLGEN: Macro variable I resolves to 1
SYMBOLGEN: Macro variable X1 resolves to VAN
MLOGIC(MERGE): %DO loop index variable I is now 2; loop will iterate again.
SYMBOLGEN: && resolves to &.
SYMBOLGEN: Macro variable I resolves to 2
SYMBOLGEN: Macro variable X2 resolves to ROC
MLOGIC(MERGE): %DO loop index variable I is now 3; loop will iterate again.
SYMBOLGEN: && resolves to &.
SYMBOLGEN: Macro variable I resolves to 3
SYMBOLGEN: Macro variable X3 resolves to SEA
MLOGIC(MERGE): %DO loop index variable I is now 4; loop will iterate again.
SYMBOLGEN: && resolves to &.
SYMBOLGEN: Macro variable I resolves to 4
SYMBOLGEN: Macro variable X4 resolves to HOU
MLOGIC(MERGE): %DO loop index variable I is now 5; loop will iterate again.
SYMBOLGEN: && resolves to &.
SYMBOLGEN: Macro variable I resolves to 5
SYMBOLGEN: Macro variable X5 resolves to KC
MLOGIC(MERGE): %DO loop index variable I is now 6; loop will iterate again.
SYMBOLGEN: && resolves to &.
SYMBOLGEN: Macro variable I resolves to 6
SYMBOLGEN: Macro variable X6 resolves to PIT
MLOGIC(MERGE): %DO loop index variable I is now 7; loop will not iterate again.
MPRINT(MERGE): set VAN1617_ ROC1617_ SEA1617_ HOU1617_ KC1617_ PIT1617_ run;
ERROR: File WORK.RUN.DATA does not exist.
MLOGIC(MERGE): Ending execution.

 

PaigeMiller
Diamond | Level 26

You didn't end the SET statement with a semi-colon (which was earlier in this thread specifically pointed out by @Kurt_Bremser)

 

%macro merge;
data underly_1617;
    set
        %do i = 1 %to 6;
            &&X&i..1617_
        %end;
    ;    /* This semi-colon ends the SET statement */
run;	
%mend;
%merge

Use of proper indenting in your code (as I have done) helps find these types of problems. Don't write code without proper indenting.

--
Paige Miller
Tom
Super User Tom
Super User

It is much clearer if you indent the MACRO statements independently from the SAS statements.

Right now you have this inconsistent indentation:

For the macro code you are using 8 spaces of indentation. 

For the SAS code you are using 4 in some places and 8 in others.

Screenshot 2022-02-17 132051.jpg

Instead be consistent. Then when you are trying to see how the logic of the macro works it is easier. And you are trying to follow the flow of the SAS code the macro is generating it is also easier.

%macro merge;
data underly_1617;
    set
    %do i = 1 %to 6;
        &&X&i..1617_
    %end;
    ; 
run;	
%mend;

 

ybz12003
Rhodochrosite | Level 12
Yes, forgot to add semicolon at the end.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 7 replies
  • 506 views
  • 4 likes
  • 4 in conversation