Attempting to convert character latitude/longitude values from character to numeric to utilize GEODIST function in PROC SQL.
Current code running on SAS (r) 9.4 (TS1M3)
data <dataset>;
set <tmp_dataset>;
LATITUDE2=input(LATITUDE, 20.15);
LONGITUDE2=input(LONGITUDE, 20.15);
run;
Output using PROC PRINT or as samples selected via PROC SQL displays as:
AX_LATITUDE2 AX_LATITUDE AX_LONGITUDE2 AX_LONGITUDE
------------------------------------------------------
40.54115 40.541147 -105.077 -105.076775
39.9924 39.992399 -104.978 -104.978418
40.39116 40.391158 -104.712 -104.711740
41.63468 41.634681 -93.698 -93.697993
First, you're actually getting all of the digits, they're just not being displayed. Use a FORMAT statement to increase the number of digits displayed. See the program below.
Second, though, because of computing restrictions on 8 byte floating point representation, YOU CAN'T GET MORE THAN 15 SIGNIFICANT DIGITS accurately stored. You probably don't need that level of precision, but be aware.
Tom
data have;
length LATITUDE LONGITUDE $20;
LATITUDE = "40.5411471234567";
LONGITUDE = "-105.076775123456";
LATITUDE2=input(LATITUDE, 20.15);
LONGITUDE2=input(LONGITUDE, 20.15);
format LATITUDE2 LONGITUDE2 best20.;
run;
proc print;
run;
You didn't post any example input so I am not sure what rounding you are talking about.
A couple of points.
Unless you know that your input character data does not include decimal places and you want SAS to insert one before the 15th to last character in the string then NOT include a the .15 on the INFORMAT. If the data does have decimal places SAS will ignore the .15 but if the character value looks like an integer then adding the .15 will cause SAS to divide the value by 10**15.
8 byte floating point numbers cannot store 19 digits of precision. Look at the value of the function CONSTANT('exactint') for your platform to see what is the larger integer that SAS can store exactly.
Not all decimal values can be stored exactly using binary floating point numbers.
Attach a format to your numeric variable if you want it to print more digits. The default in most places is to use BEST12. or sometimes BEST8. to display numbers.
1172 data test; 1173 input @1 str $20. @1 num 20. ; 1174 best8=put(num,best8.); 1175 best12=put(num,best12.); 1176 best20=put(num,best20.); 1177 f20_15=put(num,f20.15); 1178 put (_all_) (=/); 1179 cards; str=39.992399 num=39.992399 best8=39.9924 best12=39.992399 best20=39.992399 f20_15=39.992399000000000 str=40.391158 num=40.391158 best8=40.39116 best12=40.391158 best20=40.391158 f20_15=40.391158000000000 str=-93.697993 num=-93.697993 best8=-93.698 best12=-93.697993 best20=-93.697993 f20_15=-93.697993000000000 str=-104.711740 num=-104.71174 best8=-104.712 best12=-104.71174 best20=-104.71174 f20_15=-104.711740000000000
First, you're actually getting all of the digits, they're just not being displayed. Use a FORMAT statement to increase the number of digits displayed. See the program below.
Second, though, because of computing restrictions on 8 byte floating point representation, YOU CAN'T GET MORE THAN 15 SIGNIFICANT DIGITS accurately stored. You probably don't need that level of precision, but be aware.
Tom
data have;
length LATITUDE LONGITUDE $20;
LATITUDE = "40.5411471234567";
LONGITUDE = "-105.076775123456";
LATITUDE2=input(LATITUDE, 20.15);
LONGITUDE2=input(LONGITUDE, 20.15);
format LATITUDE2 LONGITUDE2 best20.;
run;
proc print;
run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.
Find more tutorials on the SAS Users YouTube channel.