Published on :
Data Manipulation INTERNAL_CREATION

CATX() Function Demonstration

This code is also available in: Deutsch Español Français
Awaiting validation
The script begins by creating a dataset 'one' containing character variables and missing values. It then illustrates how the CATX() function can be used to concatenate variables with a specified delimiter, automatically managing missing values without introducing superfluous delimiters. A comparison is made with a manual concatenation method to highlight the advantages of CATX(). The final result is displayed via a PROC PRINT.
Data Analysis

Type : INTERNAL_CREATION


Data is created directly within the script using a DATALINES/CARDS block, which allows demonstrating CATX() behavior with missing values.

1 Code Block
DATA STEP Data
Explanation :
This DATA STEP block creates a dataset named 'one'. It defines four character variables (x1 to x4) with a length of 1. Data is then read via a DATALINES block, including observations with missing values for intermediate variables (represented by a dot '.').
Copied!
1DATA one;
2 LENGTH x1-x4 $1;
3 INPUT x1-x4;
4 DATALINES;
5A B C D
6E . F G
7H . . J
8;
9RUN;
2 Code Block
DATA STEP Data
Explanation :
This DATA STEP block creates a new dataset 'two' based on 'one'. A variable 'SP' is defined as the delimiter '^'.
- The 'test1' variable is created using the CATX() function to concatenate variables x1 to x4. CATX() automatically handles missing values by not inserting a delimiter before or after them, nor consecutive delimiters.
- The 'test2' variable is created by manual concatenation. This approach requires the use of TRIM() and LEFT() functions to remove trailing spaces and may produce additional delimiters if values are missing, unlike CATX().
Copied!
1DATA two;
2 SET one;
3 SP='^';
4 test1=catx(sp, of x1-x4);
5 test2=trim(left(x1)) || sp || trim(left(x2)) || sp || trim(left(x3)) || sp ||
6 trim(left(x4));
7RUN;
3 Code Block
PROC PRINT
Explanation :
This PROC PRINT displays the content of the 'two' dataset. This allows visualizing and comparing the results of 'test1' (using CATX()) and 'test2' (manual concatenation), highlighting the advantage of CATX() for handling missing values and delimiters.
Copied!
1PROC PRINT DATA=two;
2RUN;
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.