Do you know that you can quickly and easily remove
leading and trailing blanks when concatenating character variables together?
If you were thinking “Ah
yes, the CATS
function!” then give yourself a prize!
However, this tip is really about the CATX function.
The CATX function removes leading and trailing blanks and
inserts a separator between the variable values being concatenated. (The CATS function only
removes leading and trailing blanks).
This function is handy for building meaningful character variables from
two or more variables—like building a full name from first name, middle name,
and last name.
Here is an example:
data names;
firstname = " Francis";
middlename = "Scott ";
lastname
= "
Key ";
fullname = catx(" ",
firstname, middlename, lastname);
run;
proc print
noobs data=names;
run;
You can see that FIRSTNAME has leading blanks, MIDDLENAME
has trailing blanks, and LASTNAME has both leading and trailing blanks. The CATX function specifies that the
separator should be a blank. So, the
result of the PROC PRINT is:
firstname middlename lastname fullname
Francis Scott Key
Francis Scott Key
If you like the CATX function, check out its brothers,
the CAT, CATT, and CATS functions!
Best
of luck in all your SAS endeavors!
---MMMMIIIIKKKKEEEE
(aka Michael A. Raithel)
Author of the
new cult classic for computer programmers: It Only Hurts When I
Hit <ENTER>
Print edition: http://tinyurl.com/z8bzx2e
Kindle edition: http://tinyurl.com/zypgqa7
The hack above is an
excerpt from the book: Did
You Know That? Essential Hacks for Clever SAS Programmers
Print edition: http://tinyurl.com/z8bzx2e
Kindle edition: http://tinyurl.com/zypgqa7
No comments:
Post a Comment