Published on :
Macro EXTERNE

AHGvar2arr Macro: Converting a Variable into an Array of Macro Variables

This code is also available in: Deutsch Español Français
The `AHGvar2arr` macro loads the content of a data column (`var`) from a table (`dsn`) into global macro variables named with a prefix (`arrPre`) and an index (e.g., PRE1, PRE2...). It handles prior cleanup via an external macro `%AHGdel`, the initialization of global variables up to a given dimension (`dim`), and dynamic assignment via a Data Step and `CALL SYMPUT`. The total number of elements is stored in a variable with the `_n` suffix.
Data Analysis

Type : EXTERNE


The data source is dynamically defined by the macro parameter `dsn` during the call.

1 Code Block
MACRO
Explanation :
Macro definition: global variables are declared via a loop, then a _NULL_ Data Step is executed to read the source table and create the corresponding macro variables via CALL SYMPUT.
Copied!
1%macro AHGvar2arr(dsn,var,arrPre,dim=100);
2 %AHGdel(&arrpre,like=1);
3 %global &arrPre._n;
4 %local i;
5 %DO i=1 %to &dim;
6 %global &arrPRE&i;
7 %END;
8 DATA _null_;
9 SET &dsn END=END;
10 call symput(strip("&arrpre"||%AHGputn(_n_)),strip(&var));
11 IF END THEN call symput("&arrPre._n",strip(put(_n_,best.)) );
12 RUN;
13%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.