Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

use of substr for numerical values

Status
Not open for further replies.

bbprakash

Programmer
May 8, 2008
6
GB
Thank you Mr. Chris.

Could you please tell me the use of substr function for numerical values is it same as for charecters. Here is example
data work.AreaCodes;
Phonenumber=3125551212;
Code='('!!substr(Phonenumber,1,3)!!')';
run;

answer is ( 3).
 
bbprakash,

There might be a better way, but this worked for me.

data test;
Phonenumber=3125551212;
Code=input(substr(put(Phonenumber,10.),1,3),10.);
run;

It converts the number to character, substrings it, then converts it back to a number.

You can drop the "input(" and ",10.)" if you are ok with the results being characater.
 
No, you can't use substr with numeric data. If you run the code you included above, you'll get a message like this:-
NOTE: Numeric values have been converted to character values at the places given by:

What you should do is convert the data to text as Dblan showed above.

Also, for things like telephone numbers, you should use a text field anyway, not a numeric. Numeric fields only have a maximum of 8 bytes to store the number. Once the number gets above a certain magnitude, it starts suffering from rounding issues. For actual numeric data this possibly isn't such a big deal, however a telephone number that has been rounded to the nearest 3 is totally useless. This also applies to account numbers, credit card numbers, and in fact, I'd say ANY number which is a code rather than a value should be stored as a character field.
I have hit upon this issue before, and it's difficult to track down.


Chris
Business Analyst, Code Monkey, Data Wrangler.
SAS Guru.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top