SAS Communities Library

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

Convert LAT-LONG to UTM, and back

Started ‎11-19-2014 by
Modified ‎03-14-2017 by
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

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-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

Register now!

SAS AI and Machine Learning Courses

The rapid growth of AI technologies is driving an AI skills gap and demand for AI talent. Ready to grow your AI literacy? SAS offers free ways to get started for beginners, business leaders, and analytics professionals of all skill levels. Your future self will thank you.

Get started

Article Labels
Article Tags