Convert to MACRO

Reply
Occasional Contributor
Posts: 6

Convert to MACRO

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;

 

Grand Advisor
Posts: 17,360

Re: Convert to MACRO

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. 

 

 

Occasional Contributor
Posts: 6

Re: Convert to MACRO

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.

 

Grand Advisor
Posts: 17,360

Re: Convert to MACRO

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.

Respected Advisor
Posts: 4,977

Re: Convert to MACRO

For a macro language loop, you could use:

 

%do i = 1 %to %SUBSTR(&UPDATE_MONTH,5,2);

Grand Advisor
Posts: 17,360

Re: Convert to MACRO

Grand Advisor
Posts: 10,211

Re: Convert to MACRO

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.

Respected Advisor
Posts: 4,977

Re: Convert to MACRO

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)

Ask a Question
Discussion stats
  • 7 replies
  • 207 views
  • 1 like
  • 4 in conversation