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_)
Could you help me to find the bug?
Thank you in advance
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:
%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).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.
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.
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:
%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).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.
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!
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.
Ready to level-up your skills? Choose your own adventure.