O'Reilly logo

SAS 9.4 Macro Language, 5th Edition by

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

%end;
Use the DCLOSE function to close the directory. Use the FILENAME function to
unassign the fileref.
%let rc=%sysfunc(dclose(&did));
%let rc=%sysfunc(filename(filrf));
%mend list_files;
Invoke the macro. The first parameter is the directory where the files exist. The second
parameter is the extension of the files that you want to list.
%list_files(c:\temp,sas)
Example 3: How to Increment a Macro DO Loop
by a Non-integer Value
Details
The iterative %DO loop within the macro facility can handle only integer values or
expressions that generate integer values. This goes for the %BY portion also. This macro
illustrates how to loop through values using a non-integer %BY value. Some arithmetic
and various macro functions are used to accomplish the task.
Program
%macro loop( start= , end= , by= ) ;
%local i;
%do i = 1 %to %eval(%sysfunc(Ceil(%sysevalf((&end - &start ) / &by ) ) ) +1) ;
%let value=%sysevalf(( &start - &by ) + ( &by * &i )) ;
%if &value <=&end %then %do;
%put &value;
%end;
%end ;
%mend loop ;
%loop(start = 1 , end = 5 , by = .25 )
Program Description
Begin the macro definition with three keyword parameters.
%macro loop( start= , end= , by= ) ;
%local i;
When using nested functions, you start inside and work your way out. The inner most
%SYSEVALF function is used to subtract &END by &START and then divide that
result by the value of &BY. This method returns a whole number. The CEIL function is
used to return the smallest integer that is greater than or equal to the argument. To
complete the formula the %EVAL function is used to add 1 to the final value. Use %DO
to loop through the number returned from this formula.
%do i = 1 %to %eval(%sysfunc(Ceil(%sysevalf((&end - &start ) / &by ) ) ) +1) ;
Example 3: How to Increment a Macro DO Loop by a Non-integer Value 451

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required