/*
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;
**************************************** */
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Data Literacy is for all, even absolute beginners. Jump on board with this free e-learning and boost your career prospects.