• 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 / Concatenating Strings in a SAS Data Step

Concatenating Strings in a SAS Data Step

April 11, 2022 Leave a Comment

To concatenate strings in a SAS data step, the easiest way is to use the SAS catx() 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.

When working with character variables and strings in SAS, many times we need to combine and concatenate strings to create new concatenated variables.

There are a number of ways to concatenate strings in SAS. The easiest is with the SAS catx() function.

The catx() function in SAS takes in an argument for the delimiter you want to use, as well as arguments for the variables you want to concatenate.

catx() remove leading and trailing blanks from the variables and then concatenates them.

Below is a simple example of how to use catx() to concatenate strings in a SAS data step.

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.

The rest of this post will show you other ways you can concatenate variables in SAS.

Basic Concatenation of Character Variables in a SAS Data Step

You can also concatenate variables in a SAS data step without the use of a SAS function. You can do basic concatenation in SAS with ‘||’.

Just like in other languages, where typically the concatenate operator is “+”, use ‘||’ to concatenate strings in a SAS data step.

Let’s say we have the following data in SAS.

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 simple concatenation as shown below.

data data_new;
    set k;
    basic_concat = word || "+" || word2;
run;

/* Output: */
  word    word2   basic_concat
1 this	     is	       this+is
2 some	   data	     some+data
3  for	     an	        for+an
4 easy	example	  easy+example

Using cat() to Concatenate Strings in SAS

There are a number of useful functions in SAS which allow us to concatenate character variables. While catx() is probably the best option in most cases, there are a few others which could be useful depending on what you want to accomplish.

The cat() function concatenates the given arguments, but does not remove leading or trailing spaces and does not have an option for a delimiter.

cat() is basically the same as the simple concatenation showed in the last section.

Below is an example of how to use cat() to concatenate strings in a SAS data step.

data k;
        var1 = "  This   ";
        var2 = "is   ";
        var3 = " a    ";
        var4 = "    string.";
        cat = cat(var1, var2, var3, var4);
        put cat=;
run;

/* Output */
cat=This   is    a        string.

Using catt() to Concatenate Strings in SAS Data Steps

Another function you can use to concatenate two strings together in a SAS data step is the catt() function.

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.

catt() differs from cat() in that it removes the trailing blanks from a variable. Other than that, it is performs basic concatenation.

Below is an example of how to use catt() to concatenate strings in a SAS data step.

data k;
        var1 = "  This   ";
        var2 = "is   ";
        var3 = " a    ";
        var4 = "    string.";
        catt = catt(var1, var2, var3, var4);
        put catt=;
run;

/* Output */
catt=Thisis a    string.

Using cats() to Concatenate String Variables in SAS

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.

Again, we have a function which will concatenate variables, but if you need to use a delimiter, cats() won’t work.

Below is an example of how to use cats() to concatenate multiple strings in a SAS data step.

data k;
        var1 = "  This   ";
        var2 = "is   ";
        var3 = " a    ";
        var4 = "    string.";
        cats = catss(var1, var2, var3, var4);
        put cats=;
run;

/* Output */
cats=Thisisastring.

Hopefully this article has been useful for you to learn how to concatenate string and character variables in SAS.

Other Articles You'll Also Like:

  • 1.  SAS left() Function – Left Align Character Variables in Data Step
  • 2.  Change Length of Character Variable in SAS Dataset
  • 3.  Get Substring from Right in SAS
  • 4.  SAS let – Create User-Defined Macro Variables in Your SAS Code
  • 5.  SAS Percent Format – Formatting Number as Percent in SAS Dataset
  • 6.  SAS tranwrd() Function – Replace Characters in Strings in SAS Data Step
  • 7.  SAS Less Than or Equal to with LE or <=
  • 8.  SAS Not In – How to Check if Variable is Not in List of Values
  • 9.  SAS nodupkey – How to Remove Duplicates with PROC SORT by Key
  • 10.  SAS Comments – Commenting Your SAS Code the Right Way

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