Learning SAS? Welcome to the exclusive online community for all SAS learners.

Proc GMAP data labels

Reply
Occasional Contributor
Posts: 5

Proc GMAP data labels

Using Proc Gmap in SAS 9.4, I have used the annotate feature to add a short text string to the right side of each plotted point on a map, but I would like to add a mouseover capability so that when I move the mouse over a plotted point, I am able to display additional information about that point on the map.  Is there a simple example of how to do that?

Super User
Posts: 13,926

Re: Proc GMAP data labels

Posted in reply to rrstarbuck49

To your DATA not annotate data set add a variable with the text you want to appear in the popup.

Assuming you are doing a CHORO map add the option HTML= <name of the variable with the popup text>

 

If you are placing much text you may want to provide some carriage controls to create a block of text instead of one very long line.

 

This is an example of one of these variables from a project:

   my_html = 'title='||
            quote(Catx('0d'x,Countyname,
            '-----------------------------',
            ('Exemption Rate:     '||put(exempt_rate,f6.1)),
            ('Enrollment K, 1, 7: '||put(enrollment_sum,comma6.)),
            ('Students Exempt:    '||put(Total_Exempt_Sum,comma6.))
            )
   );

The string following title= is the actual text displayed, the word title will not appear.

 

In this case I have the name of a county appear followed by a row of dashes to separate the name from the three values displayed.

The '0d'x creates a line break so there are 5 lines in the output: the county name, the dashes and 3 values with a description.

Occasional Contributor
Posts: 5

Re: Proc GMAP data labels

Hi,

 

I used your recommended code, but may have incorporated it incorrectly.  What I want the mouseover to do is to display two lines, such as:

Location: location_name

Sponsor: name_of_sponsor

 

I have attached the Excel spreadsheet of data and the SAS program (SAS code saved in a Word document).  When I run the program, I do get the expected map, but when I save the results as an HTML file, and then open that file, I don't see any text pop up when I move the mouse over a symbol displayed on the plot.

 

Any suggestions?

Super User
Posts: 13,926

Re: Proc GMAP data labels

Posted in reply to rrstarbuck49

It really is best to post code, especially into either the code box with the {I} or running man.

First thing I see questionable is in your gmap call.

proc gmap data=map map=map ;
  id state ;
  choro state / anno=dot html=my_html nolegend ;
run ;
quit ;

The Data= data set and Map= dataset seldom should be the same set.

 

The DATA data set should have one observation with values used for the labels, a variable with the name of the ID variable and the same values for the ones you want to display.

 

There is not need to project or even get any sort of coordinates for the data you want to appear as the popup text or the statistics or value to color the map. The Data  data set would need

State as a number or Statecode as the two character state identifier.

Since your locations data has a value for zip you could use the ZIPSTATE function to get the 2 character statecode, no reason to do any merging with the zipcode dataset.

Here is a brief example that will generate some states with pop up tips:

/* a data set with one record per state in the
   map data to hang single values on later
*/
data work.statedata;
   set mapsgfk.us_states;
   by notsorted state;
   if first.state;
   drop segment x y  long lat resolution density;
run;

/* a very small example of the desired tip text*/
data work.tips;
   infile datalines dlm='|';
   informat sponsor location $25. zip $5.;
   input sponsor location  zip;
   statecode= zipstate(zip);
   length my_html $ 85;
    my_html = 'title='||
            quote(Catx('0d'x,('Location: '||put(location, $20.)),
            ('Sponsor:  '||put(sponsor,$30.))
            )
      );

datalines;
UIDP|Washington, DC|20418
UIDP|Irvine, CA|92617
Kaufmann Foundation|Kansas City, MO|64110
Georgia Tech|Atlanta, GA|30332
UNC-Charlotte & IBM|Charlotte, NC|28223
Rutgers Univ.|New Brunswick, NJ|08901
Lexmark|Lexington, KY|40511
Deere|Moline, Ill|61265
West Virginia Univ.|Morgantown, WV|26505
;
run;

/* combine the data with the tip data*/
proc sql;
   create table mapdata as
   select a.*, b.my_html
   from work.statedata as a
        left join
        work.tips as b
        on a.statecode=b.statecode
   order by statecode
   ;
quit;

/* your path for the output files goes here*/
filename odsout "D:\";
ODS HTML path=odsout body="TipExample.htm" 
 (title="Map with tips") style=sasweb;

goptions noborder cback=white htitle=17pt htext=11pt ftitle="albany amt/bold" ftext="albany amt";

proc gmap data=mapdata 
          map=mapsgfk.us_states
;
   id state;
   choro state/ html=my_html
                nolegend
   ;
run;
ods html close;

You will need to set the path in the FILENAME ODSOUT statement.

 

The RESULTS window map will not display tips. You need to open the file created in the ODSOUT destination with the name TipExample.htm. Make sure you place the cursor over one the states with the statecode in the data. Such as CA, MO or IL.

 

 

There is one other thing with these tips. Each map area may have only ONE tip displayed. If your data has multiple records then you have to make sure that your text field that will create the tip will hold all of them and it is up to you to combine them and create an appearance you like.

 

 

The OPs entire program for those interested:

/*--------------------------------------------------------------------------*/
/*  This program imports the Excel spreadsheet UIDP_Meeting_Locations.xslx  */
/*  into a SAS dataset named UIDP_Meeting_Locations.sas7bdat stored in      */
/*  folder named data.                                                      */
/*--------------------------------------------------------------------------*/

/* location of Excel spreadsheet containing data */
FILENAME REFFILE '/home/rrstarbuck49/data/UIDP_Meeting_Locations_2.xlsx';

/* library location of SAS dataset created */
libname MYDATA '/home/rrstarbuck49/data/' ;

PROC IMPORT DATAFILE = REFFILE replace
	DBMS = XLSX
	OUT = mydata.uidp_meeting_locations_2 ;
  GETNAMES = YES ;  /* first row of Excel spreadsheet has variable names */
run ;

proc sort  data = mydata.uidp_meeting_locations_2 ; by zip ;
run ;

/* Set the graphics environment */
goptions reset = all  cback = white  border  htitle = 12pt  htext = 10pt ; 

data longlat (keep = zip lat long uidp sector sponsor location my_html) ;
  length sponsor $30. location $20. ;
 /* In case there are duplicate ZIP codes, rename */
 /* X and Y from the SASHELP.ZIPCODE data set.    */
  merge mydata.uidp_meeting_locations_2 (in = mine)
        sashelp.zipcode (rename=(x=long y=lat)) ;
  by zip ;
 /* Keep if the ZIP code was in my data set. */
  if mine then do ;
    my_html = 'title='||
            quote(Catx('0d'x,('Location: '||put(location, $20.)),
            ('Sponsor:  '||put(sponsor,$30.))
            )
      );
    output ;
  end ;
run;

 /* Create an annotate data set to place */
 /* a symbol at the ZIPCODE locations.   */
data anno ;
 /* Use the X and Y values from the LONGLAT data set. */
  set longlat ;
  length text $10. ;
 /* Set the data value coordinate system. */
 /* Set the function to label.            */
 /* Set the size of the symbol to .75.    */
 /* Set a FLAG variable to signal annotate observations. */
  retain xsys ysys '2' function 'label' flag 1 when 'a' ;
 /* area of symbol is proportional to number of times meeting location used */
  size = 1.3 ; 
 /* Set the font to the Special font. */
  style = 'special' ;
 /* The symbol is a star. */
  text = 'M' ;
 /* Specify the color for the symbol. */
  if sector = 'I' then color = 'vilg' ;          /* Industry */
      else if sector = 'A' then color = 'depk' ; /* Academe  */
    else color = 'vio' ;                         /* Other    */
 /* Output the observation to place the symbol. */
  output ;
  long = long + 0.5 ;
  style = 'normal' ;
  fontweight = 'bold' ;
  position = '6' ;
  text = uidp ;
  color = 'blue' ;
  output ;
run;

/* Combine the map data set with the annotate data set. */
data all ;
  /* Subset out the states that you do not want. */
  /* The FIPS code of 2 is Alaska, 15 is Hawaii, */
  /* and 72 is Puerto Rico.  */
  set mapsgfk.us_states(where=(state not in(2 15 72))) anno ;
run;

/* Project the combined data set. */
proc gproject data=all out=allp  degrees latlong eastlong ;
  id state;
  
run;
quit;

/* Separate the projected data set into a map and an annotate data set. */
data map dot;
  set allp;
  /* If the FLAG variable has a value of 1, it is an annotate  */
  /* observation; otherwise, it is a map data set observation. */
  if flag = 1 then output dot;
  else output map;
run;

/* Define the pattern for the map. */
pattern1 v=me c=grp r=50 ;

/* Define the title & footnote for the map. */
title1 height = 24pt bold 'UIDP General Meeting Locations' ;
footnote color = depk 'University   ' color  = vilg 'Industry   ' color = vio  'Other' ;

/* Generate the map and place the symbols at ZIP code locations. */
proc gmap data=map map=map ;
  id state ;
  choro state / anno=dot html=my_html nolegend ;
run ;
quit ;
Occasional Contributor
Posts: 5

Re: Proc GMAP data labels

Hi,

I ran your example program, and it creates two files: TipExample.htm and gmap.png. I am using SAS on Demand for Academics on a MacBookPro, and I had the SAS program store these two files using

filename odsout "/home/rrstarbuck49/output/“;

I then downloaded these files to my “desktop” folder on my MacBookPro. When I open the TipExample.htm file, I see a map of the USA, but nothing happens when I move the mouse cursor over any of the states. I examined the html file and do see the gmap.png file on line 1469 of the file. There is no path preceding that filename, so I assume these two files need to be in the same folder, which is where they are currently located.

Any idea why this might not be working? Also, I am not familiar with how to specify a folder on my MacBookPro so that these two files would go to the specified folder rather than to the one mentioned in the filename statement above.

Kind regards,
Bob
Ask a Question
Discussion stats
  • 4 replies
  • 212 views
  • 0 likes
  • 2 in conversation