• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / SAS / catx SAS – Concatenate String Variables with Delimiter in SAS Data Step

catx SAS – Concatenate String Variables with Delimiter in SAS Data Step

January 24, 2022 Leave a Comment

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.

Other Articles You'll Also Like:

  • 1.  =">SAS Greater Than or Equal to with GE or >=
  • 2.  SAS ceil – Round Up to Ceiling of Number in a SAS Data Step
  • 3.  SAS If Then Statements with Multiple Variables
  • 4.  Identifying Duplicates in SAS with PROC SORT dupout Option
  • 5.  SAS Delete Dataset – Use PROC Datasets to Delete SAS Files
  • 6.  How to Select First 100 Observations of SAS Dataset
  • 7.  IN in SAS – Checking if Variable is in Array of Values
  • 8.  Round Number to Nearest Integer in SAS
  • 9.  SAS Comments – Commenting Your SAS Code the Right Way
  • 10.  SAS call symput – Create Macro Variables in Data Step

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy

x