09-21-2016 02:59 AM
Hi all, I need help to truncate the length of all the content of a column to 4.
e.g. name phone number
I would like to get the first first four digits of all phone numbers in a new column. Thanks for anticipated help.
09-21-2016 03:22 AM
data want; set have; length new_column $4; new_column = substr(strip(phone_number),1,4); run;
strip() removes leading and trailing blanks.
09-21-2016 09:34 AM
@KurtBremser, If the new_column is setup as length of 4, then SUBSTR() seems redundant in this context.
Until, for some reason, you need your target variable to be longer to also hold values from somewhere else, but the requirement for the first 4 digits HERE did not change. Then you suddenly get more than 4 characters, and your results go bonkers just because you wanted to avoid some typing.
Keep in mind that the length statement might be pages away from the place where you use the variable(s). Always make your code explicit so the intention is clearly visible right on the spot.
Been there, done that, and had to spend days to debug it.
09-21-2016 03:24 AM
Oh, and if phone_number is numeric:
data want; set have; new_column = input(substr(left(put(phone_number,best.)),1,4),4.); run;
If new_column is to be character, omit the input() function and define new_column as $4, as in my previous post.
09-21-2016 09:26 AM
if it is numeric couldn't you just use
new_column = substr(compress(phone_number),1,4);
You can do this, but you shouldn't.
The log looks like this:
23 data want; 24 set have; 25 new_column = substr(compress(phone_number),1,4); 26 run; NOTE: Numeric values have been converted to character values at the places given by: (Line):(Column). 25:30
NOTEs about type conversions done on the fly always raise red flags with me, as sooner or later the result of such a conversion will bite you in the ass, and sometimes big-time.
I consider that sloppy programming, and always make sure that my type-casts are forced by program code and leave nothing to guess for the computer.
Computers are dumb.
If I have written clean code, and such a NOTE appears, I know immediately that a variable has changed from numeric to character or vice-versa, and I need to check why this happened. Or it is my first clue that I misspelled a variable name somewhere.