BookmarkSubscribeRSS Feed

Convert LAT-LONG to UTM, and back

Started ‎11-19-2014 by
Modified ‎03-14-2017 by
Views 6,667

 

/*
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

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

Try Zone=32N for Denmark.

Very nice. I'm using this in a project. I'm including a link here.

Version history
Last update:
‎03-14-2017 01:45 PM
Updated by:

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Free course: Data Literacy Essentials

Data Literacy is for all, even absolute beginners. Jump on board with this free e-learning  and boost your career prospects.

Get Started

Article Labels
Article Tags