BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
SASAlex101
Quartz | Level 8

I have what seems to be a simple problem. I have a set of vertices which define a polygon on a map. I'm working with Canada. 

I'm using QGIS, here is my polygon:

SASAlex101_0-1696788898236.png

QGIS can output a list of vertices of the polygon, which I put into SAS:

data Polygon;
input vertex x y ;
infile datalines delimiter="#";
datalines;
1#44.1598831520675#-79.7063565442144
2#44.2877110404308#-79.0767942900621
3#44.1184676463941#-78.3947685147305
4#43.6748950070277#-78.9806111679
5#44.1598831520675#-79.7063565442144
;
run;

What I want to do is let's say I have this points: 

data PointData;
length City $100.;
input City $ X Y;
infile datalines delimiter="#";
datalines;
Mississauga#43.58718486496601#-79.64392326388756
Ajax#43.85040430693342#-79.01840936353507
;
run;

I want to know if any of these points are in the polygon or not. In the example, Ajax is within the polygon but Mississauga is not. so I'd expect the data output to be:

SASAlex101_1-1696789587464.png

 

 

I'm not sure how to do this. I've looked at GINSIDE, but it seems you need an underlying map for that, and I don't know how to define a polygon within that proc. So I'm kind of lost as geocoding isn't my expertise. 

 

any help would be appreciated. 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

A map data set includes an identification variable to "name" or "label" a polygon. So modify the Polygon data to include an identification variable. In this case I use Polygonid.

data Polygon;
input vertex x y ;
infile datalines delimiter="#";
polygonid=1;
datalines;
1#44.1598831520675#-79.7063565442144
2#44.2877110404308#-79.0767942900621
3#44.1184676463941#-78.3947685147305
4#43.6748950070277#-78.9806111679
5#44.1598831520675#-79.7063565442144
;
run;

I don't know why you couldn't provide another data step with the coordinates of interest. I shortened stuff as I'm too lazy to type a bunch just for an example:

data city;
  input city $ x y;
datalines;
citya  43.68 -79.64
cityb  43.85 -79.01
;

And use of Proc Ginside:

proc ginside   data=city map=polygon out=result;
  id polygonid;
run;

The ID variable for the polygon is added , along with other variables by default from the Map data set, to the DATA= data set in the Out= data set. If there is no match the id variable and associated other variables from the Map data set is missing. if you only want to add the Id variable use the Ginside option DROPMAPVARS .

 

If you really need a 0/1 value for "inside one of the polygons in the map data set" then a pass through a data step to add/change variable values.

View solution in original post

2 REPLIES 2
ballardw
Super User

A map data set includes an identification variable to "name" or "label" a polygon. So modify the Polygon data to include an identification variable. In this case I use Polygonid.

data Polygon;
input vertex x y ;
infile datalines delimiter="#";
polygonid=1;
datalines;
1#44.1598831520675#-79.7063565442144
2#44.2877110404308#-79.0767942900621
3#44.1184676463941#-78.3947685147305
4#43.6748950070277#-78.9806111679
5#44.1598831520675#-79.7063565442144
;
run;

I don't know why you couldn't provide another data step with the coordinates of interest. I shortened stuff as I'm too lazy to type a bunch just for an example:

data city;
  input city $ x y;
datalines;
citya  43.68 -79.64
cityb  43.85 -79.01
;

And use of Proc Ginside:

proc ginside   data=city map=polygon out=result;
  id polygonid;
run;

The ID variable for the polygon is added , along with other variables by default from the Map data set, to the DATA= data set in the Out= data set. If there is no match the id variable and associated other variables from the Map data set is missing. if you only want to add the Id variable use the Ginside option DROPMAPVARS .

 

If you really need a 0/1 value for "inside one of the polygons in the map data set" then a pass through a data step to add/change variable values.

SASAlex101
Quartz | Level 8
Quite extraordinary. this looks to be the foundation of what I was looking for!

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1094 views
  • 1 like
  • 2 in conversation