String Reversal Macro and Variable Tests

This code is also available in: Deutsch Español Français
Difficulty Level
Beginner
Published on :
The code defines a macro named 'revrs' that uses an iterative loop to reverse the order of characters in an input string. It also defines a 'test' macro to provide an example string. The script then executes %PUT commands to display the result of the inversion as well as the concatenation of two global variables defined in the script.
Data Analysis

Type : CREATION_INTERNE


The script does not manipulate any SAS data tables. It operates solely on macro variables and character strings.

1 Code Block
MACRO DEFINITION
Explanation :
Definition of the 'revrs' macro. It iterates through the input string from end to beginning to build 'nstring'. Note: The presence of '&nstring' in the loop causes the variable to be displayed at each iteration.
Copied!
1%macro revrs(string);
2 %local nstring;
3 %DO i=%LENGTH(&string) %to 1 %BY -1;
4 %let nstring=&nstring%qsubstr(&string,&i,1);
5 &nstring
6 %END;
7 *&nstring;
8%mend revrs;
2 Code Block
MACRO DEFINITION
Explanation :
Definition of a utility macro 'test' that simply returns the string 'Two words'.
Copied!
1%macro test;
2 Two words
3%mend test;
3 Code Block
%PUT
Explanation :
Execution of the reversal macro on the result of the 'test' macro concatenated with itself, and display of the result in the log.
Copied!
1%put %nrstr(%test%test) - %revrs(%test%test);
4 Code Block
%LET / %PUT
Explanation :
Initialization of two global macro variables (wbh and ldh) and display of their direct concatenation.
Copied!
1%let wbh=A;
2%let ldh=B;
3 
4%put &wbh&ldh;
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.

Related Documentation

Aucune documentation spécifique pour cette catégorie.