Published on :
Macro CREATION_INTERNE

AHGaddordvar Macro - Sort Variable Creation

This code is also available in: Deutsch Español Français
This macro is designed to be invoked within a DATA Step. It analyzes a source variable (`var`) to detect the presence of numbers. If numbers are present, it constructs a sort variable (`ordVar`) by normalizing the numeric segments (likely separated by periods). It uses a macro loop to generate `tranwrd` instructions that add leading zeros (padding) to numbers from 0 to 9 surrounded by periods, thus allowing consistent alphanumeric sorting (e.g., treating '1.2' as less than '1.10').
Data Analysis

Type : CREATION_INTERNE


The code defines a software macro and does not directly load or manipulate external data during its compilation.

1 Code Block
MACRO
Explanation :
Definition of the `AHGaddordvar` macro. It generates conditional `IF` logic for the DATA Step. It transforms the string by doubling the '.' delimiters and adding a '0' prefix to single digits found between these delimiters to standardize the sort format.
Copied!
1%macro AHGaddordvar(var,ordVar);
2 %local i;
3 IF anydigit(&var) THEN
4 DO;
5 &ordvar='..'||trim(substr(tranwrd(&var,'.','..'),anydigit(&var)))||'..';
6
7 %DO i=0 %to 9;
8 &ordvar=tranwrd(&ordvar,".&i..",".0&i..");
9 %END;
10 &ordvar=substr(&var,1,anydigit(&var)-1)||&ordvar;
11 END;
12%mend;
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.