Convert LAT-LONG to UTM, and back
Article Options
- Article History
- RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
Views
7,946
/*
latLong2UTM : Convert latitude and longitude (in degrees) to UTM X and Y (in meters).
Syntax : latLong2UTM( DSN, lat=LAT, long=LONG, zone=18N )
Variables X and Y are added to dataset DSN.
UTM2latLong : Convert UTM X and Y (in meters) to latitude and longitude (in degrees).
Syntax : UTM2latLong( DSN, y=Y, x=X, zone=18N )
Variables LAT and LONG are added to dataset DSN.
See Example below.
PG
*/
%macro latLong2UTM(DSN,lat=LAT,long=LONG,zone=18N);
data _null_;
no = compress("&zone", , "as");
if findc("&zone", "sS") = 0 then
call symputx("to", cats("+proj=utm +datum=WGS84 +units=m +no_defs +zone=", no));
else call symputx("to", cats("+proj=utm +datum=WGS84 +units=m +no_defs +south +zone=", no));
run;
proc gproject latlon degrees
project = proj4
from = "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"
to = "&to"
data = &DSN(rename=(&lat=LAT &long=LONG))
out = &DSN;
id;
run;
%mend;
%macro UTM2latLong(DSN,y=Y,x=X,zone=18N);
data _null_;
no = compress("&zone", , "as");
if findc("&zone", "sS") = 0 then
call symputx("from", cats("+proj=utm +datum=WGS84 +units=m +no_defs +zone=", no));
else call symputx("from", cats("+proj=utm +datum=WGS84 +units=m +no_defs +south +zone=", no));
run;
data _UTM2latLong;
set &DSN;
_oldX = &x;
_oldY = &y;
rename &x=X &y=Y;
run;
proc gproject
project = proj4
to = "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"
from = "&from"
data = _UTM2latLong
out = _UTM2latLong;
id;
run;
data &DSN(drop=x y rename=(_oldX=&x _oldY=&y));
set _UTM2latLong;
lat = y;
long = x;
run;
proc sql; drop table _UTM2latLong; quit;
%mend;
/* Example *******************************
data test;
lat=45.002361; long=-73.556944; Location="Montreal"; output;
run;
title "Original coordinates";
proc print data=test noobs; run;
%LatLong2UTM(test);
title "LatLong2UTM";
proc print data=test noobs; run;
data test2(drop=lat long);
set test;
run;
title "Original UTMs";
proc print data=test2 noobs; run;
%UTM2LatLong(test2);
title "UTM2LatLong";
proc print data=test2 noobs; run;
**************************************** */
Comments
03-14-2017
09:37 AM
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
03-14-2017
09:37 AM
Hi there
Great code - unfortunately im not really into the definition of Zone- value
How do I preset the correct zone (or is it necessary?).
My data is mainly from Denmark, Europe.
Hope yu can help 🙂
Best regards, MikA
08-27-2017
04:54 PM
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
08-27-2017
04:54 PM
Try Zone=32N for Denmark.
07-31-2020
10:11 PM
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
07-31-2020
10:11 PM
Very nice. I'm using this in a project. I'm including a link here.