Published on :
Macro MIXTE

AHGsetvarLen Macro: Variable Length Modification

This code is also available in: Deutsch Español Français
Awaiting validation
This macro changes the length of a specified variable ('var') in a given table ('dsn'). It works by creating an empty temporary table containing the variable with the new length ('len'), then merges this structure with the original table. The 'merge' technique without a 'by' clause (implicit 1-to-1 merge used here for attributes) allows imposing the new length. Finally, it calls an external macro (%AHGordvar) to restore the column order. Note: This script depends on several other macros not provided (%AHGblank, %AHGbasename, %AHGgettempname, %AHGvarlist, %AHGordvar).
Data Analysis

Type : MIXTE


The macro operates on a SAS table passed as a parameter (&dsn). No specific data is hardcoded.

1 Code Block
DATA STEP Data
Explanation :
Macro definition. 1) Determines the output name. 2) Retrieves the list of current variables. 3) Creates an empty table forcing the new length for the target variable. 4) Merges the empty table with the source table (the length from the first table cited in the MERGE takes precedence). 5) Reorders the variables.
Copied!
1%macro AHGsetvarLen(dsn,var,len,out=);
2 %IF %AHGblank(&out) %THEN %let out=%AHGbasename(&dsn);
3 %local empty varlist;
4 %AHGgettempname(empty);
5 %AHGvarlist(&dsn,Into=varlist,dlm=%str( ),global=0);
6 DATA
7 LENGTH &var &len;
8 RUN;
9 DATA &out;
10 MERGE &empty &dsn;
11 RUN;
12 %AHGordvar(&out,&varlist,keepall=0);
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.