DATA Step, Macro, Functions and more

Truncate

Reply
Contributor
Posts: 25

Truncate

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.

 

 

Super User
Posts: 7,773

Re: Truncate

data want;
set have;
length new_column $4;
new_column = substr(strip(phone_number),1,4);
run;

strip() removes leading and trailing blanks.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Respected Advisor
Posts: 3,156

Re: Truncate

Posted in reply to KurtBremser

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

Super User
Posts: 7,773

Re: Truncate


Haikuo wrote:

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

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Posts: 7,773

Re: Truncate

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
New Contributor
Posts: 3

Re: Truncate

Posted in reply to KurtBremser

Hi Kurt

 

if it is numeric couldn't you just use

 

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

Super User
Posts: 7,773

Re: Truncate

Posted in reply to Tom_C_Mortensen

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Ask a Question
Discussion stats
  • 6 replies
  • 286 views
  • 1 like
  • 4 in conversation