Macro addvar - Text variable splitting

This code is also available in: Deutsch Español Français
Difficulty Level
Beginner
Published on :
The `addvar` macro analyzes a given variable in an input table. If the content length exceeds `maxlen` (default 200), it uses an external macro `%gmModifySplit` to prepare the splitting, then distributes the text into several new indexed variables using a defined separator. If the length is below the limit, the variable is simply formatted and copied. Note that the macro modifies the input table (`&in_data`) if splitting is enabled.
Data Analysis

Type : EXTERNE


Data is defined by the `in_data` macro parameter. The script expects to receive an existing table.

1 Code Block
PROC SQL
Explanation :
Calculates the maximum length of the text variable in the input table to determine if splitting is required.
Copied!
1PROC SQL noprint;
2 select distinct max(LENGTH(&in_var)) into :lngth
3 from &in_data;
4QUIT;
2 Code Block
DATA STEP Data
Explanation :
Executed if the length exceeds the limit: Preparation of the input table by renaming the variable and calling the external macro `%gmModifySplit`.
Copied!
1DATA &in_data;
2 SET &in_data(rename=&in_var=_&in_var._);
3 %gmModifySplit(var=_&in_var._, width=&maxlen);
4RUN;
3 Code Block
PROC SQL
Explanation :
Determines the number of additional variables needed by counting separator occurrences.
Copied!
1PROC SQL noprint;
2 select cats(max(count(_&in_var._, "&split"))) into :varn
3 from &in_data;
4QUIT;
4 Code Block
DATA STEP Data
Explanation :
Creation of the final output table. The text is split into an array of variables (`vlst`) using the `scan` function.
Copied!
1DATA &out_data;
2 SET &in_data;
3 array vlst{*} $200 &out_pre. &out_pre.1 - &out_pre.&varn;
4 DO i=1 to %eval(&varn+1);
5 vlst(i)=scan(_&in_var._, i, "&split");
6 END;
7 drop _&in_var._ i;
8RUN;
5 Code Block
DATA STEP Data
Explanation :
Alternative (ELSE): If no splitting is required, the output table is created with the variable simply adjusted to the standard length.
Copied!
1DATA &out_data;
2 SET &in_data(rename=&in_var=_&in_var._);
3 LENGTH &out_pre $200;
4 &out_pre=_&in_var._;
5RUN;
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.
Copyright Info : PAREXEL INTERNATIONAL LTD / Janssen Research & Development, LLC


Related Documentation

Aucune documentation spécifique pour cette catégorie.