Hello everyone,
I want to find STATES by Longitude and Latitude,but I can't get the data I need, here is my code:
data have; input n y x id; cards; 1 40.0746 -123.787 1 2 34.1388 -118.252 2 4 34.0628 -118.485 3 5 34.0745 -118.481 4 6 33.8462 -118.044 5 7 33.9320 -117.942 6 8 33.5442 -117.709 7 9 34.1756 -118.528 8 10 38.1794 -122.137 9 ; run; proc ginside data=have map=maps.us out=need; id state ; run;
proc print;
run;
SAS Output
Obs | x | y | SEGMENT | STATE | n | id | STATECODE |
---|---|---|---|---|---|---|---|
1 | 40.0746 | -123.787 | . | . | 1 | 1 | |
2 | 34.1388 | -118.252 | . | . | 2 | 2 | |
3 | 34.0628 | -118.485 | . | . | 4 | 3 | |
4 | 34.0745 | -118.481 | . | . | 5 | 4 | |
5 | 33.8462 | -118.044 | . | . | 6 | 5 | |
6 | 33.9320 | -117.942 | . | . | 7 | 6 | |
7 | 33.5442 | -117.709 | . | . | 8 | 7 | |
8 | 34.1756 | -118.528 | . | . | 9 | 8 | |
9 | 38.1794 | -122.137 | . | . | 10 | 9 |
The state column are all missing. please advise me.
Thanks!
(x,y corrected, thank you ballardw and Robert, here is the new corrected code:
data have;
input y x ;
label x ='Longitude' y ='Latitude';
cards;
41.0746 -123.787
35.1388 -118.252
35.0628 -118.485
35.0745 -118.481
35.2462 -118.044
35.6320 -117.942
33.6442 -117.709
35.1756 -118.528
37.1794 -122.137
;
run;
proc ginside data=have map=mapsgfk.us_states(rename=(x=x0 y=y0 long=x lat=y)) out=need(keep=y x statecode);
*The DATA= data set must have X and Y variables.;
id statecode;
run;
When using Proc Ginside, your coordinates must be in X and Y variables in the data, and in the map dataset. And they must both be in the same coordinate system (in your case, latitude and longitude in degrees). Since the map has lat and long variables, you'll have to rename them.
data have;
input n y x id_foo;
cards;
1 40.0746 -123.787 1
2 34.1388 -118.252 2
4 34.0628 -118.485 3
5 34.0745 -118.481 4
6 33.8462 -118.044 5
7 33.9320 -117.942 6
8 33.5442 -117.709 7
9 34.1756 -118.528 8
10 38.1794 -122.137 9
;
run;
data my_map (rename=(lat=y long=x)); set mapsgfk.us_states (drop = x y);
run;
proc ginside data=have map=my_map out=need;
id state statecode;
run;
proc print data=need;
var n y x id_foo state statecode;
run;
If you check the MAPS.US data set you will see that the X and Y coordinates are projected to generate a map and not Lat/Long coordinates.
You would need to turn your lat/ long into map coordinates. The coordinates in MAPS.US are in radians as well, IIRC.
If you have the mapsgfk you might try using MAPSGFK.US_ALL but you may still need to project your coordinates using Proc Gproject.
I think you have x and y reversed in your data as well.
When using Proc Ginside, your coordinates must be in X and Y variables in the data, and in the map dataset. And they must both be in the same coordinate system (in your case, latitude and longitude in degrees). Since the map has lat and long variables, you'll have to rename them.
data have;
input n y x id_foo;
cards;
1 40.0746 -123.787 1
2 34.1388 -118.252 2
4 34.0628 -118.485 3
5 34.0745 -118.481 4
6 33.8462 -118.044 5
7 33.9320 -117.942 6
8 33.5442 -117.709 7
9 34.1756 -118.528 8
10 38.1794 -122.137 9
;
run;
data my_map (rename=(lat=y long=x)); set mapsgfk.us_states (drop = x y);
run;
proc ginside data=have map=my_map out=need;
id state statecode;
run;
proc print data=need;
var n y x id_foo state statecode;
run;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.