How does this data come into SAS, from which source(s) does it originate?
If this is actually about phone numbers, they should never have been stored as numeric values. They will never be used in calculations, can easily extend to more than 15 digits (causing changes in the last digits because of numeric imprecision), and you cannot (reliably) make a difference between domestic and international numbers.
To convert the number to a string like you indicated, you need to first left-align the number, and then dissect it:
length charvar $12;
charvar = substr(left(put(numvar,32.)),1,10);
charvar = catx("-",substr(charvar,1,3),substr(charvar,4,3),substr(charvar,7));
You must convert to a character value first to easily extract the first 10 digits. After that, there is no need to go back to numeric to use a picture format.
@Kurt_Bremser , is it not possible to use a picture format ?
But one needs to make sure first that only 10 digits are in the number, so you need to do math first. IMO it is easier to work with a string.
If your "number" included country codes you may have a problem with the codes that start with zero as the number would have ignored that but you would need it as part of a true "first 10 characters" value.
@Pandu2 wrote:
Thankyou very much for your help. Previously I figured out on seperating the number with hiphen using substr but, I was unable to convert the 15 digit number value to character value. I've a minor concern here, can't we achieve the required output without changing the length of the variable, please let me know. Anyhow, please fill your heart heartily thanked.
15 digits is probably too many to store into a number. SAS only uses binary floating point numbers and it cannot generate all possible 15 digit decimal strings.
But since you appear to only want the 10 most significant digits that might not matter.
If you just want the 10 most significant digits of a 15 digit number just divide by 10**5 and use INT() function to eliminate the fractional part. For example:
76 data want; 77 set have; 78 first10 = int(example/1E5); 79 put (_all_) (= comma20. /); 80 run; example=123,456,789,011,111 first10=1,234,567,890
If you just want to print the existing number in that way you could make a PICTURE format that incorporates the division.
proc format;
picture hyphen low-high = '999-999-9999' (mult=0.00001);
run;
proc print data=have;
format example hyphen.;
run;
If you want to generate a character string you could also use that picture format to help.
data want;
set have;
hyphen=put(example,hyphen.);
run;
Or build the string yourself.
data want;
set have;
length hyphen $12;
hyphen = put(example,Z15.);
hyphen=catx('-',substr(hyphen,1,3),substr(hyphen,4,3),substr(hyphen,7,4));
run;
@Pandu2 wrote:
Hi All,
I hope you're doing well.
I require your assistance on seperating a numeric variable values with hiphen (-). I've a numeric variable which has a values of length 15 each which means 8 bytes, but I require only 1st 10 indexed values seperated with hiphen (-).
Example: 123456789011111
Required : 123-456-7890
Thankyou in advance.
If they are Phone numbers then why are they numeric?
How did you get the values into a SAS dataset? Did you read it from a text file, like a CSV file? If so then read the values from the text file as a character variable to begin with and it will be much easier to deal with.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.