BookmarkSubscribeRSS Feed
Kayomole
Calcite | Level 5

Hi all, I need help to truncate the length of all the content of a column to 4.

 

e.g.         name                  phone number

               Joe                      54478955

               Jill                        35467889

               Kane                   78542661

 

I would like to get the first first four digits of all phone numbers in a new column. Thanks for anticipated help.

 

 

6 REPLIES 6
Kurt_Bremser
Super User
data want;
set have;
length new_column $4;
new_column = substr(strip(phone_number),1,4);
run;

strip() removes leading and trailing blanks.

Haikuo
Onyx | Level 15

@Kurt_Bremser, If the new_column is setup as length of 4, then SUBSTR() seems redundant in this context.

Kurt_Bremser
Super User

@Haikuo wrote:

@Kurt_Bremser, 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.

Kurt_Bremser
Super User

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.

Tom_C_Mortensen
Calcite | Level 5

Hi Kurt

 

if it is numeric couldn't you just use

 

data want;
set have;
new_column = substr(compress(phone_number),1,4);
run;

Kurt_Bremser
Super User

@Tom_C_Mortensen wrote:

Hi Kurt

 

if it is numeric couldn't you just use

 

data want;
set have;
new_column = substr(compress(phone_number),1,4);
run;


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.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 1451 views
  • 1 like
  • 4 in conversation