02-02-2017 10:48 AM
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;
02-02-2017 11:29 AM
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.
02-02-2017 01:50 PM
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.
02-02-2017 11:36 AM
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.
02-02-2017 11:46 AM
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)