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!
data one;
length x1-x4 $1;
input x1-x4;
datalines;
A B C D
E . F G
H . . J
;
run;
1
DATA one;
2
LENGTH x1-x4 $1;
3
INPUT x1-x4;
4
DATALINES;
5
A B C D
6
E . F G
7
H . . J
8
;
9
RUN;
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!
data two;
set one;
SP='^';
test1=catx(sp, of x1-x4);
test2=trim(left(x1)) || sp || trim(left(x2)) || sp || trim(left(x3)) || sp ||
trim(left(x4));
run;
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!
proc print data=two;
run;
1
PROC PRINTDATA=two;
2
RUN;
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.
SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and other countries. ® indicates USA registration. WeAreCAS is an independent community site and is not affiliated with SAS Institute Inc.
This site uses technical and analytical cookies to improve your experience.
Read more.