BookmarkSubscribeRSS Feed
GeorgeBonanza
Obsidian | Level 7

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;

 

7 REPLIES 7
Reeza
Super User

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. 

 

 

GeorgeBonanza
Obsidian | Level 7

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.

 

Reeza
Super User

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.

Astounding
PROC Star

For a macro language loop, you could use:

 

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

ballardw
Super User

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.

Astounding
PROC Star

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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 654 views
  • 1 like
  • 4 in conversation