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

Good morning,

 

I'm coding a macro to create all the month-year between two dates and concatenate them with a string.

When I write it without macro, it works:

 

	%let start_date=01jan2013;
	%let end_date=01dec2019;
	%let mylib = "SCCOMOTR.Vinculacion2_";
	data want_date;
		date="&start_date"d;

		do while (date<="&end_date"d);
			output;
			date=intnx('month', date, 1, 's');
		end;

		format date YYMMN6.;
	run;

	data want_date;
		set want_date;
		new_var= &mylib ||  put(date,YYMMN6.);
	run;

	PROC SQL;
		SELECT new_var INTO :output SEPARATED BY " "
			FROM want_date;
	QUIT;
%put &output.;

but when I want to convert it to a macro, it fails:

%MACRO createStringWithTables (start_date, end_date, mylib);

	%global output;

		data want_date;
		date="&start_date"d;

		%do %while (date<="&end_date"d);
			output;
			date=intnx('month', date, 1, 's');
		%end;

		format date YYMMN6.;
	run;

	data want_date;
		set want_date;
		new_var= &mylib ||  put(date,YYMMN6.);
	run;	PROC SQL;
		SELECT new_var INTO :output SEPARATED BY " "
			FROM want_date;
	QUIT;
%mend;

%createStringWithTables(01jan2010, 01dec2019, BaseDeDades_) 

 

rotmo86_1-1620981876502.png

Could you help me to find the bug?

 

Thank you in advance

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

Good morning, @rotmo86, and welcome to the SAS Support Communities!

 

The error messages must be due to something you submitted before the code shown in the screenshot. I see two issues with the macro:

  1. Remember that the DATA step code created by the macro processor is executed only after the macro processor has finished its work. This implies that values of variables read by the DATA step cannot be used by the macro processor. Therefore the %DO %WHILE ... %END loop starting with
    %do %while (date<="&end_date"d);
    should have remained a DO WHILE loop: It's the value of variable date which is to be compared to the "&end_date"d (in the DATA step), not the four-letter text date (which the macro processor sees).
  2. When macro parameter &mylib resolves to BaseDeDades_, the DATA step will interpret this as a variable name. So, either use quotes around BaseDeDades_ in the macro call (as you did in the %LET statement in line 3) or add double quotes in the macro:
    new_var= "&mylib" ||  put(date,YYMMN6.);

These corrections should resolve the issues. Start a new SAS session if leftovers from a previous failed macro call continue to produce errors in the log.

 

View solution in original post

2 REPLIES 2
ballardw
Super User

Please paste the log entries into a Text box on the forum opened with the </> icon. Then we can much more easily make corrections and paste corrected code.

 

Since your Picture of the error shows that this is generated by a MACRO you need to do one of two things, and better is both:

1) Provide the Code to the macro including the call to that macro.

2) One of the general debugging options for macro issues is to set the OPTION MPRINT; before running the macro so get details about the code generated by the macro. Then copy the entire log from running the macro and paste that whole mess into a text box on the forum

 

"Used out of order" often means that lines before what you have shown actually caused the issue. In fact, many errors can be caused by previous lines.

FreelanceReinh
Jade | Level 19

Good morning, @rotmo86, and welcome to the SAS Support Communities!

 

The error messages must be due to something you submitted before the code shown in the screenshot. I see two issues with the macro:

  1. Remember that the DATA step code created by the macro processor is executed only after the macro processor has finished its work. This implies that values of variables read by the DATA step cannot be used by the macro processor. Therefore the %DO %WHILE ... %END loop starting with
    %do %while (date<="&end_date"d);
    should have remained a DO WHILE loop: It's the value of variable date which is to be compared to the "&end_date"d (in the DATA step), not the four-letter text date (which the macro processor sees).
  2. When macro parameter &mylib resolves to BaseDeDades_, the DATA step will interpret this as a variable name. So, either use quotes around BaseDeDades_ in the macro call (as you did in the %LET statement in line 3) or add double quotes in the macro:
    new_var= "&mylib" ||  put(date,YYMMN6.);

These corrections should resolve the issues. Start a new SAS session if leftovers from a previous failed macro call continue to produce errors in the log.

 

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

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

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
  • 2 replies
  • 7454 views
  • 1 like
  • 3 in conversation