To concatenate character variables with a delimiter, the easiest way is with the catx() SAS function.
data k; var1 = "This"; var2 = "is"; var3 = "a"; var4 = "string."; concatenated = catx(" ",var1, var2, var3, var4); put concatenated=; run; /* Output */ concatenated=This is a string.
If you are working with the SAS Macro Language, you can concatenate
When working with character variables in our SAS datasets, a useful operation is to be able to concatenate strings into new character variables.
In SAS, we can easily concatenate character variables with the SAS catx() function. The catx() function in SAS takes an argument for the delimiter you want to use, as well as arguments for the variables you want to concatenate together.
catx() remove leading and trailing blanks from the variables and then concatenates them.
Let’s say we have the following SAS dataset.
data k; input word $ 5. word2 $ 8.; datalines; this is some data for an easy example ; run;
We can concatenate the two variables with a delimiter in the following SAS code using both simple concatenation and catx().
data data_new; set k; basic_concat = word || "+" || word2; catx_concat = catx("+", word, word2); catx_concat2 = catx(",", word, word2); run; /* Output: */ word word2 basic_concat catx_concat catx_concat2 1 this is this+is this+is this,is 2 some data some+data some+data some,data 3 for an for+an for+an for,an 4 easy example easy+example easy+example easy,example
What is the Difference Between cat(), catt(), cats(), and catx() in SAS?
There are a few different concatenation methods in SAS: cat(), catt(), cats(), and catx(). Each of these methods are slightly different and can be useful depending on what you want to do in your SAS program.
The cat() function concatenates the given arguments, but does not remove leading or trailing spaces and does not have an option for a delimiter.
The catt() function concatenates the given arguments and removes trailing blanks, but does not remove leading spaces and also does not have an option for a delimiter.
Finally, we have the cats() function which concatenates the given arguments, removes the trailing and leading blanks, but does not have an option for a delimiter.
Let’s see how these functions work with the following SAS data step.
data k; var1 = " This "; var2 = "is "; var3 = " a "; var4 = " string."; cat = cat(var1, var2, var3, var4); catt = catt(var1, var2, var3, var4); cats = catss(var1, var2, var3, var4); catx = catx(" ",var1,var2,var3,var4); put cat=; put catt=; put cats=; put catx=; run; /* Output */ cat=This is a string. catt=Thisis a string. cats=Thisisastring. catx=This is a string.
Depending on the structure of your data and what you want to accomplish in your program, each of these functions could be the solution to your problem.
Creating a Dates with catx() in SAS
One way we can use the catx() SAS function in a useful way is creating dates by concatenating variables representing the month, day, and year.
Let’s say we have the following SAS dataset with numeric values for the month, day and year of some dates.
data dt; input mon day year; datalines; 12 4 2021 1 19 2022 10 6 2020 3 4 2020 5 12 2021 ; run;
We can concatenate these values with the catx() function to “create” a new date in the following SAS code. In the code below, I’ve also shown how you can use the SAS mdy() function to create dates from numeric variables.
data dt_w_catx; set dt; dt_w_catx = catx("/",mon,day,year); format dt_w_mdy mmddyy10.; dt_w_mdy = mdy(mon,day,year); run; /* Output */ mon day year dt_w_catx dt_w_mdy 1 12 4 2021 12/4/2021 12/04/2021 2 1 19 2022 1/19/2022 01/19/2022 3 10 6 2020 10/6/2020 10/06/2020 4 3 4 2020 3/4/2020 03/04/2020 5 5 12 2021 5/12/2021 05/12/2021
If you just want to create the dates for display or output purposes, the catx() function can be useful.
Hopefully this article has been useful for you to learn how to use the catx() function in SAS to concatenate variables with a delimiter.