Hello Community,
I am using Base SAS to analyze a crime dataset related to one of the cities of US. I have a dataset that has x-coordinate, y-coordinate, longitude, and latitude data related to the city. Can I use Proc gmap or any other function to map these crime locations for this city?
I am stuck and I could not proceed ahead with it. Again, I am aware of SAS visual analytics but that is not what I want to use. I want this to be done through Base SAS. Please provide some help.
Thank you in advance.
You need SAS/Graph, which comes with mapping data sets.
When you have this, see here.
Have you tried and looked at the examples?
I am assuming that you have imported the shapefile for the city (using Proc Mapimport), and have projected the lat/long values to get projected y/x values (using Proc Gproject).
When you do the above, you can use Proc Gproject's parmout= option, and save the projection parameters, to use in the next step...
Now you can take your crime lat/long dataset, and project it using Proc Gproject, and use the same projection parameters as you used for the city map (by using Gproject's parmin= option). And then use those projected x/y coordinates, and use them to annotate markers on the map. You could either use the annotate function='label' and annotate symbols from a font, or use the annotate function='pie' to annotate colored bubbles.
Here's an example that uses the parmout and parmin, that might help get you started...
http://robslink.com/SAS/democd95/scottish_festivals_nc_info.htm
/*Here's an example from Central Texas Ya'll*/
/*This example shows how to convert GPS to planar, and then gplot
/*Create a sample data set a location in Central Texas - Google says this is Austin*/
data have;
input lat long POINT_ID;
datalines;
30.2272 -97.7431 1
;
run;
/*Project Lat Long onto Central Texas Map - See Sashelp.Proj4def */
/*Project your lat long onto NAD 1983 StatePlane Texas Central FIPS 4203 Feet*/
/*Parameters: +proj=lcc +lat_1=30.11666666666667 +lat_2=31.88333333333333 +lat_0=29.66666666666667
+lon_0=-100.3333333333333 +x_0=700000 +y_0=3000000 +ellps=GRS80 +datum=NAD83 +to_meter=0.3048006096012192 no_defs*/
proc gproject latlon
project=proj4 to="ESRI:102739" /*NAD 1983 StatePlane Texas Central FIPS 4203 Feet*/
data=have out=want;
id POINT_ID;
run;
/*Now plot using sgplot*/
proc sgplot data=want;
scatter x=x y=y;
run;
/*But wait - there's more...*/
/*You can download public maps (.gov websites usually have these) as shapefiles and convert them to your local planar coordinate system*/
PROC MAPIMPORT OUT=<your filename here> DATAFILE='<public shapefile here>.shp';
run;
/*Check - You must call lat long 'lat' 'long' before you can proc gproject...if not rename them*/
/*Convert Shape file map to Central Texas State Plane*/
proc gproject latlon
project=proj4 to="ESRI:102739" /*NAD 1983 StatePlane Texas Central FIPS 4203 Feet*/
data=<your filename here> out=<file with planar coordinates>;
id SHAPE_ID; /*SHAPE_ID is the field common to all the points that define a shape*/
run;
/*Now concatenate the point and the shape file - Note You will need to rename the X and Y in your points file, call them XP and YP*/
data combined; set want <file with planar coordinates>; run;
proc sgplot data=combined;
polygon x=x y=y ID=SHAPE_ID / fill outline;
scatter x=XP y=YP;
run;
/*Even cooler...You can also use proc ginside to see if your point lies inside any of the shapes from your shape file...for example*/
proc ginside data=want map=<your filename> out=Inside INSIDEONLY;
id SHAPE_ID;
run;
Now that Proc SGmap is available, you might want to use it as a simple way to see crime data points on a city map. SGmap uses tile-based maps (such as Openstreetmap), therefore you don't have to worry about getting a city map polygon, and projecting your map and crime point-data the same way.
Here's a simple example (using fake data):
data my_data;
length crime $20;
input lat long crime;
datalines;
35.7922679 -78.7771215 robbery
35.7894014 -78.772710 disorderly
35.7943392 -78.7666627 jaywalking
;
run;
title h=22pt "Fake Crime Data in Cary, NC";
proc sgmap plotdata=my_data;
openstreetmap;
scatter x=long y=lat / group=crime markerattrs=(size=25px symbol=circlefilled);
run;
* Update...The National Oceanic and Atmospheric Administration (NOAA) has forecast data available on a Microsoft Public Blob...;
* Download a specific date and forecast from the Windows DOS CMD line (or Linux);
* Windows azcopy example...
* azcopy cp "https://noaahrrr.blob.core.windows.net/hrrr/hrrr.20210601/conus/hrrr.t12z.wrfsfcf00.grib2" "C:\YOURFOLDER\conus.grib2";
* or Windows curl...
* curl https://noaahrrr.blob.core.windows.net/hrrr/hrrr.20210601/conus/hrrr.t12z.wrfsfcf00.grib2 --output "C:\YOURFOLDER\conus.grib2";
* But this is a "grib2" file, so you also need to install the wgrib2.exe application from NOAA to parse it...
* Get a copy of wgrib2.exe (Linux version also available) from NOAA to parse the binary weather file;
* Use the NOAA link below to install wgrib2.exe in DOS;
* NOAA link to wgrib2.exe install...https://ftp.cpc.ncep.noaa.gov/wd51we/wgrib2/Windows10/v3.0.2/;
* You can parse-out your local data and overlay on a city map. Here's how...;
* wgrib2 C:\YOURFOLDER\conus.grib2 -set_grib_type j -small_grib -98.2:-97.35 29.95:30.78 C:\YOURFOLDER\austin.grib2;
* Convert the filtered grib2 file to .csv...
* wgrib2 "C:\YOURFOLDER\austin.grib2" -csv "C:\YOURFOLDER\austin.csv";
* Now import the file into SAS...;
data WORK.austin;
infile 'C:\YOURFOLDER\austin.csv' dsd truncover ;
length COL1 $30. COL2 $30. COL3 $30. COL4 $30. COL5 $30. COL6 $30. COL7 $30.;
input COL1 -- COL7 ;
run;
* Assign an ID to each coordinate;
data have (KEEP = ID lat long Weather Value);
set WORK.austin;
format ID 10. lat long best20.;
ID = _N_;
lat = COL6;
long = COL5;
Weather = COL3;
Value = COL7;
run;
* Convert weather coodinates to Texas planer XY coordinates;
proc gproject latlon
project=proj4 to="ESRI:102739"
data=have out=want;
id ID;
run;
* Now you have the lat, long, X and Y projection, and weather forecast parameters for a grid of locations over your city - pretty cool, huh?
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.