We’re smarter together. Learn from this collection of community knowledge and add your expertise.

Convert LAT-LONG to UTM, and back

by Respected Advisor on ‎11-19-2014 10:06 PM - edited on ‎03-14-2017 01:45 PM by Community Manager (1,511 Views)

 

/*
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
by Contributor MikA
on ‎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

Your turn
Sign In!

Want to write an article? Sign in with your profile.