Create a macro variable named &VALUE that uses the %SYSEVALF function to re-
create the value as a non-integer. First subtract &START by &BY and then add that to
the product of &BY and &I.
%let value=%sysevalf(( &start - &by ) + ( &by * &i )) ;
Use the %IF statement to check that &VALUE from the %LET statement is less than or
equal to &END. If the condition is true, then place the value in the log using the %PUT
statement.
%if &value <=&end %then %do;
%put &value;
Use the %END statement to close both %DO loop blocks.
%end;
%end ;
%mend loop ;
Invoke the macro by using the start and end values of the loop as well as the increment
value.
%loop(start = 1 , end = 5 , by = .25 )
Example 4: How to Use Character Values on a
Macro %DO Loop
Details
The macro facility does not allow character values on an iterative %DO loop. There are
two macros below that give a work around to that limitation. These two macros use
different techniques to step through single-byte characters that are passed to the macro.
Program
Example 4A: This example enables you to step through only the characters that are
passed to the macro as a parameter to the macro variable named &LST.
%macro iterm(lst);
%let finish=%sysfunc(countw(&lst));
%do i = 1 %to &finish;
%put %scan(&lst,&i);
%end;
%mend iterm;
%iterm(a c e)
Example 4B: This example enables you to step through the characters between the
&BEG and &END values.
%macro iterm(beg,end);
%do i = %sysfunc(rank(&beg)) %to %sysfunc(rank(&end));
%put %sysfunc(byte(&i));
%end;
%mend iterm;
%iterm(a,e)
452 Appendix 5 SAS Macro Examples
Program Description
Example 4A
Begin the macro definition with one parameter.
%macro iterm(lst);
Create the macro variable named &FINISH to contain the number of characters or words
in the macro variable named &LST.
%let finish=%sysfunc(countw(&lst));
Use the %DO statement to loop through the number of characters or words contained
within &LST.
%do i = 1 %to &finish;
Use the %SCAN function to pull off each character or word in the macro variable named
&LST and use the %PUT statement to write contents to the log.
%put %scan(&lst,&i);
End the %DO block.
%end;
End the macro.
%mend iterm;
Invoke the macro that passes in the characters to loop through, which are separated by a
space.
%iterm(a c e)
Here are the results for example, 4A.
Example 4B
Begin the macro definition with two parameters.
%macro iterm(beg,end);
Use the RANK function to return the position of a character in the ASCII or EBCDIC
collating sequence. The %SYSFUNC function is needed to use SAS functions within the
macro facility. Use the %DO statement to loop through the number of words between
&BEG and &END.
%do i = %sysfunc(rank(&beg)) %to %sysfunc(rank(&end));
Use the BYTE function to return the character in the ASCII or the EBCDIC collating
sequence that is represented from the macro variable named &I. The %PUT statement is
used to write the contents to the log.
%put %sysfunc(byte(&i));
End the %DO block.
%end;
End the macro.
%mend iterm;
Invoke the macro that uses the starting character and ending character.
%iterm(a,e)
Example 4: How to Use Character Values on a Macro %DO Loop 453
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset