SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
InspectahDex
Obsidian | Level 7

I'm trying to concatenate firstname, middlename, lastname, and professionaltitle columns to look like this:

 

John David Smith, MD

 

However when I try the below cat function within my PROC SQL statement, I get a column that is completely blank with null values:

 

cat(PROVIDER_FIRST_NAME, " ", PROVIDER_MIDDLE_NAME, " ", PROVIDER_LAST_NAME, ", ", PROF_DESIGNATION) as provider_name

 

The CATT, CATS, and CATX functions return values as expected but I'm stumped as to why the CAT function returns nothing.

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

@InspectahDex wrote:

 

The CATT, CATS, and CATX functions return values as expected but I'm stumped as to why the CAT function returns nothing.


The other functions trim (remove blank spaces) by default and CAT does not. 

 

Does this work for you:

cat(trim(PROVIDER_FIRST_NAME), " ", trim(PROVIDER_MIDDLE_NAME), " ", trim(PROVIDER_LAST_NAME), ", ", trim(PROF_DESIGNATION)) as provider_name

 


@InspectahDex wrote:

I'm trying to concatenate firstname, middlename, lastname, and professionaltitle columns to look like this:

 

John David Smith, MD

 

However when I try the below cat function within my PROC SQL statement, I get a column that is completely blank with null values:

 

cat(PROVIDER_FIRST_NAME, " ", PROVIDER_MIDDLE_NAME, " ", PROVIDER_LAST_NAME, ", ", PROF_DESIGNATION) as provider_name

 

The CATT, CATS, and CATX functions return values as expected but I'm stumped as to why the CAT function returns nothing.


 

 

View solution in original post

6 REPLIES 6
Reeza
Super User

@InspectahDex wrote:

 

The CATT, CATS, and CATX functions return values as expected but I'm stumped as to why the CAT function returns nothing.


The other functions trim (remove blank spaces) by default and CAT does not. 

 

Does this work for you:

cat(trim(PROVIDER_FIRST_NAME), " ", trim(PROVIDER_MIDDLE_NAME), " ", trim(PROVIDER_LAST_NAME), ", ", trim(PROF_DESIGNATION)) as provider_name

 


@InspectahDex wrote:

I'm trying to concatenate firstname, middlename, lastname, and professionaltitle columns to look like this:

 

John David Smith, MD

 

However when I try the below cat function within my PROC SQL statement, I get a column that is completely blank with null values:

 

cat(PROVIDER_FIRST_NAME, " ", PROVIDER_MIDDLE_NAME, " ", PROVIDER_LAST_NAME, ", ", PROF_DESIGNATION) as provider_name

 

The CATT, CATS, and CATX functions return values as expected but I'm stumped as to why the CAT function returns nothing.


 

 

InspectahDex
Obsidian | Level 7
It does but another issue is popping up. When the provider has no middle name, there are 3 spaces instead of 1 because of that rule. Is there anyway to account to have a single space for instances where a middle name value is null?
InspectahDex
Obsidian | Level 7
I found a solution to that second issue. I just added compbl before the (cat(trim... and it worked! Thanks, Reeza!
ChrisNZ
Tourmaline | Level 20

CAT() does this (assuming the variable lengths here):

'John                                                                 ' || 'David                                                                ' ||'Smith                                                                ' ||', ' ||'MD                                                                   '       

which does not fit in your result variable.
If PROVIDER_FIRST_NAME is empty, you may well run out of space to see anything at all.

 

 

Tom
Super User Tom
Super User

Hard to tell without seeing the context you are using this.

But the CAT() function is inappropriate for what you are doing.

You will not get 

John David Smith, MD

as the result unless PROVIDER_FIRST_NAME is defined as length $4 and PROVIDER_MIDDLE_NAME is defined as length $5 and PROVIDER_LAST_NAME is defined as length $5 and PROF_DESIGNATION is defined as length equal to longer than $2.

 

That is because the CAT() function, unlike the other functions you mentioned, does NOT trim the trailing spaces from the fixed length character strings that SAS uses.

InspectahDex
Obsidian | Level 7
Ahh, thanks for putting it that way. That makes sense!

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 3630 views
  • 5 likes
  • 4 in conversation