I have a simple code below. The data step works fine but when I try to put the do loop inside of the MACRO I get an error.
Any help getting the MACRO to work is appreciated. Thanks in advance.
%let UPDATE_MONTH = 201612;
data _null_; do i = 1 to INPUT(SUBSTR("&UPDATE_MONTH",5,2), 2.); PUT i; end; run;
%MACRO TEST; %do i = 1 %to INPUT(SUBSTR("&UPDATE_MONTH"),5,2), 2.); %PUT i; %end; %MEND; %TEST;
The Input/SUBSTR part won't work. To use functions in a macro you need to wrap them in %SYSFUNC so the macro processor can differentiate text from functions.
if I am understanding you correctly, then the following should work, correct?
%MACRO TEST; %do i = 1 %to %SYSFUNC(INPUT(%SUBSTR(&UPDATE_MONTH,5,2), 2.)); %PUT i; %end; %MEND; %TEST;
However, I receive the following error:
ERROR: The INPUT function referenced in the %SYSFUNC or %QSYSFUNC macro function is not found.
Check the docs for the SYSFUNC function, INPUT is not valid function. You can use INPUTC INPUTN instead as required.
You don't need it though. INPUT converts the type, but in a macro it will already look like a number so it's fine.
For a macro language loop, you could use:
%do i = 1 %to %SUBSTR(&UPDATE_MONTH,5,2);
You may find the examples here useful
The macro processor is primarily a text based system. If you pull numeric characters from a string as @Astounding suggest then the macro processor "sees" i = 1 to 12 (or what ever those 2 characters may be).
You really only need to force "numeric" when you need the processor to see the numeric equivalen of a resolved value such as addition
%do i = 1 %to (4 + 5) will fail because "( " is not something that resolves to a numeric value
%do 1 = 1 %to %eval(4 + 5) works because the %eval does the integer arithmetic and yields a "9" to the processor. Sometimes you may want the %sysevalf or to do non-integer results but I find that using that is often an idicator I am about to do something that is best not done in the macro processor.
ballardw,
You have a lot of valid principles mentioned here, but note ...
Macro language automatically applies %eval to the expressions on the %DO statement (from, to, and by values). So this would work:
%to (4 + 5)
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.