/*+-------------------------------------------------------+ | SAS Global Forum 2012 Sample | | Together at Last: Spatial Analysis and SAS Mapping | | | | NAME: mpmacros.sas | | TITLE: Macro programs used with the example | | PRODUCT: GRAPH | | PAPER TITLE: | | Together at Last: Spatial Analysis and SAS Mapping | | | | This program expects you to run from the directory | | containing all programs and data sets. Otherwise, | | modify the program. | | | |15March2012 | +-------------------------------------------------------+*/ /*-----------------------------------------------------------*/ /* Macro to create rectangle from a x,y locations in * the input dataset * annodata = annotate data set created. * inputdata = Input data set containing points to be drawn. * Note that this data must contain variables x and y * with the location of the points. * rwidth = width of rectangle (optional - default value) * rdepth = depth of rectangle (optional - default value) * lcolor = Line color * fcolor = Fill color * solid = "Y" if you want a solid/filled rectangle. "N" = empty. * center = "Y" if you want rectangles centered at x,y. "N" means * x,y specifies the upper left corner. * tipvar = Variable name to be used for tool tip (optional). */ %macro make_rectangle(annodata,inputdata, rwidth,rdepth,lcolor,fcolor,solid,center,tipvar); data &annodata; length function $8 color $ 12 style $20 ; retain xsys ysys '2' hsys '3' when 'a' size .7; retain width 0.01 depth 0.01 ; %if (&inputdata ^= ) %then %do; set &inputdata; %end; %if (&solid = 'Y') %then %do; style='solid'; %end; %else %do; style='empty'; %end; line=1; /* optional arguments */ %if (&rwidth ^= ) %then %do; width=&rwidth; %end; %if (&rdepth ^= ) %then %do; depth=&rdepth; %end; /* optional tool tips */ %if (&tipvar ^= ) %then %do; html= 'title=' || quote("&tipvar" ||'=' || trim(left(&tipvar)) ); %end; startx =x; starty= y; /*** create the rectangle ***/ /* coordinates of the rectangle */ function='POLY'; color=&fcolor; x=startx; y=starty; output; /* start position */ /*output each corner */ function='POLYCONT'; color=&lcolor; x=startx+width; y=starty; output; x=startx+width; y=starty-depth; output; x=startx; y=starty-depth; output; x=startx; y=starty; output; run; %mend; /*-----------------------------------------------------------*/ /*-----------------------------------------------------------*/ /* Macro to convert degrees radians to degrees. * outdata = output data that is created. * inputdata = Input data set containing points to be drawn. * Note that this data must contain variables x and y * with the location of the points. */ %macro to_degrees(outdata, inputdata); /* Convert from radians to degrees */ data &outdata; set &inputdata; x=-x*45/atan(1); y= y*45/atan(1); run; %mend; /*-----------------------------------------------------------*/ /*-----------------------------------------------------------*/ /* Macro to find the highest and lowest numbers of a variable * inputdata = Input data set containing points to be drawn. * varname = Variable to process * Note that this data must contain variables x and y * with the location of the points. * NOTE: This macro creates &minval and &maxval with results */ %macro get_minmax(inds,varname); %global minval; %global maxval; data tempmm; set &inds END=lastob; retain maxval -999999999; retain minval 999999999; if &varname <= minval then minval=&varname; else if &varname >= maxval then maxval=&varname; if (lastob = 1) then do; output; call symput("minval",minval); call symput("maxval",maxval); end; run; %mend; /*-----------------------------------------------------------*/ /*-----------------------------------------------------------*/ /* Macro to display a map. * mapdata = map data set. * respdata = response data set. * annodata = annotate data set. * id = id statement variable. * choro = choro statement variable. * name = file name to output. * dev = device to draw the map. * mapcolor = "Y" to color the map background or "N" to not color * map background (optional - default is "Y"). * htmlvar = Variable containing html info. (optional) */ %macro draw_map(mapdata,respdata, annodata, id, choro, name, dev, mapcolor, htmlvar); /************************************************************/ /* Create an HTML file showing the map and points. */ /************************************************************/ GOPTIONS reset=all; GOPTIONS CBACK=white; GOPTIONS DEVICE=&dev; *goption xpixels=2400 ypixels=1800; ODS LISTING CLOSE; ODS HTML path=odsout body="&name._&dev..html" ; pattern; %if ((&mapcolor ne ) and (&mapcolor = "N")) %then %do; pattern v=me c=CX000000 r=100; %end; %else %do; pattern v=s c=CXE9E8DC r=100; %end; %if (&htmlvar ^= ) %then %do; %let htmlv=html=&htmlvar; %end; %else %do; %let htmlv=; %end; goptions border; proc gmap map=&mapdata anno=&annodata data=&respdata all; id &id; choro &choro /nolegend name="&name" &htmlv; run; quit; ODS HTML CLOSE; ODS LISTING; %mend; /*-----------------------------------------------------------*/ data WORK.COMBINED; infile datalines dsd truncover; input orientation:$10. Target:$15. k:32. station:$8. lat:8.1 lon:8.1 xpos1:32. ypos1:32. ksum:32. ksumk:32.; format lat 8.1 lon 8.1; datalines4; 45/35,360/-30/100,20,USL000,34.5,-110.7,360,-30,3,23 45/35,390/-30/100,18,USL000,34.5,-110.4,390,-30,3,21 45/35,420/-60/100,20,USL000,34.2,-110.1,420,-60,4,24 45/35,450/-60/100,20,USL000,34.2,-109.7,450,-60,4,24 45/35,450/-30/100,19,USL000,34.5,-109.7,450,-30,3,22 45/35,480/-90/100,19,USL000,34.0,-109.4,480,-90,4,23 45/35,480/-60/100,19,USL000,34.2,-109.4,480,-60,4,23 45/35,480/-30/100,19,USL000,34.5,-109.4,480,-30,3,22 45/35,510/-90/100,19,USL000,34.0,-109.1,510,-90,4,23 45/35,540/-120/100,17,USL000,33.7,-108.8,540,-120,4,21 ;;;; run; /*name of dataset with oil data*/ %let oildata=combined; %let predvar=ksumk; /*%let predvar=bpm; */ /*********************************************** * Prepare Grid * ***********************************************/ /* %let maxval = 24; */ /* %let minval = 0; */ /*set macro variables maxval and minval */ %get_minmax(&oildata,&predvar); /* Set colors on the grid that we will draw */ /* This will ramp the negative colors as green */ /* Neutral values (zero) is black */ /* Positive colors are red */ data grid; set &oildata; length hx $2; /*make negative numbers green to black */ /*the more negative the number, the greener */ if (&predvar < 0) then do; cnum=(&predvar * (255/&minval)); /*scale colors */ /*Make sure we stay within the threshold values*/ if (cnum < 0) then cnum=0; else if (cnum > 255) then cnum=255; hx=put(cnum,hex2.); /* Transparency for 9.3 and up*/ color="RGBA00"||trim(left(hx))||"0099"; /* Pre-9.3 cannot use transparency **** color="CX00"||trim(left(hx))||"00"; ***/ end; /* Make Positive values black to Red */ /* Larger numbers are more red */ else do; cnum=(&predvar * (255/ &maxval)); /*Make sure we stay within the threshold values*/ if (cnum <= 0) then cnum=0; else if (cnum > 255) then cnum=255; hx=put(cnum,hex2.); /* Transparency for 9.3 and up*/ color="RGBA"||trim(left(hx))||"000099"; /* Pre-9.3 cannot use transparency **** color ="CX"||trim(left(hx))||"0000"; ***/ end; x=lon; y=lat; /* use x and y values for GMAP */ run; /*+-------------------------------------------------------+ | SAS Global Forum 2012 Sample | | Together at Last: Spatial Analysis and SAS Mapping | | | | NAME: mpmacros.sas | | TITLE: Macro programs used with the example | | PRODUCT: GRAPH | | PAPER TITLE: | | Together at Last: Spatial Analysis and SAS Mapping | | | | This program expects you to run from the directory | | containing all programs and data sets. Otherwise, | | modify the program. | | | |15March2012 | +-------------------------------------------------------+*/ /*-----------------------------------------------------------*/ /* Macro to create rectangle from a x,y locations in * the input dataset * annodata = annotate data set created. * inputdata = Input data set containing points to be drawn. * Note that this data must contain variables x and y * with the location of the points. * rwidth = width of rectangle (optional - default value) * rdepth = depth of rectangle (optional - default value) * lcolor = Line color * fcolor = Fill color * solid = "Y" if you want a solid/filled rectangle. "N" = empty. * center = "Y" if you want rectangles centered at x,y. "N" means * x,y specifies the upper left corner. * tipvar = Variable name to be used for tool tip (optional). */ %macro make_rectangle(annodata,inputdata, rwidth,rdepth,lcolor,fcolor,solid,center,tipvar); data &annodata; length function $8 color $ 12 style $20 ; retain xsys ysys '2' hsys '3' when 'a' size .7; retain width 0.01 depth 0.01 ; %if (&inputdata ^= ) %then %do; set &inputdata; %end; %if (&solid = 'Y') %then %do; style='solid'; %end; %else %do; style='empty'; %end; line=1; /* optional arguments */ %if (&rwidth ^= ) %then %do; width=&rwidth; %end; %if (&rdepth ^= ) %then %do; depth=&rdepth; %end; /* optional tool tips */ %if (&tipvar ^= ) %then %do; html= 'title=' || quote("&tipvar" ||'=' || trim(left(&tipvar)) ); %end; startx =x; starty= y; /*** create the rectangle ***/ /* coordinates of the rectangle */ function='POLY'; color=&fcolor; x=startx; y=starty; output; /* start position */ /*output each corner */ function='POLYCONT'; color=&lcolor; x=startx+width; y=starty; output; x=startx+width; y=starty-depth; output; x=startx; y=starty-depth; output; x=startx; y=starty; output; run; %mend; /*-----------------------------------------------------------*/ /*-----------------------------------------------------------*/ /* Macro to convert degrees radians to degrees. * outdata = output data that is created. * inputdata = Input data set containing points to be drawn. * Note that this data must contain variables x and y * with the location of the points. */ %macro to_degrees(outdata, inputdata); /* Convert from radians to degrees */ data &outdata; set &inputdata; x=-x*45/atan(1); y= y*45/atan(1); run; %mend; /*-----------------------------------------------------------*/ /*-----------------------------------------------------------*/ /* Macro to find the highest and lowest numbers of a variable * inputdata = Input data set containing points to be drawn. * varname = Variable to process * Note that this data must contain variables x and y * with the location of the points. * NOTE: This macro creates &minval and &maxval with results */ %macro get_minmax(inds,varname); %global minval; %global maxval; data tempmm; set &inds END=lastob; retain maxval -999999999; retain minval 999999999; if &varname <= minval then minval=&varname; else if &varname >= maxval then maxval=&varname; if (lastob = 1) then do; output; call symput("minval",minval); call symput("maxval",maxval); end; run; %mend; /*-----------------------------------------------------------*/ /*-----------------------------------------------------------*/ /* Macro to display a map. * mapdata = map data set. * respdata = response data set. * annodata = annotate data set. * id = id statement variable. * choro = choro statement variable. * name = file name to output. * dev = device to draw the map. * mapcolor = "Y" to color the map background or "N" to not color * map background (optional - default is "Y"). * htmlvar = Variable containing html info. (optional) */ %macro draw_map(mapdata,respdata, annodata, id, choro, name, dev, mapcolor, htmlvar); /************************************************************/ /* Create an HTML file showing the map and points. */ /************************************************************/ GOPTIONS reset=all; GOPTIONS CBACK=white; GOPTIONS DEVICE=&dev; *goption xpixels=2400 ypixels=1800; ODS LISTING CLOSE; /* ODS HTML path=odsout body="&name._&dev..html" */ /* ; */ pattern; %if ((&mapcolor ne ) and (&mapcolor = "N")) %then %do; pattern v=me c=CX000000 r=100; %end; %else %do; pattern v=s c=CXE9E8DC r=100; %end; %if (&htmlvar ^= ) %then %do; %let htmlv=html=&htmlvar; %end; %else %do; %let htmlv=; %end; goptions border; proc gmap map=&mapdata anno=&annodata data=&respdata all; id &id; choro &choro /nolegend name="&name" &htmlv; run; quit; /* ODS HTML CLOSE; */ ODS LISTING; %mend; /*-----------------------------------------------------------*/ data mystates; set maps.us; if ((state eq stfips("TX")) or (state eq stfips("OK")) or (state eq stfips("KS")) or (state eq stfips("CO")) or (state eq stfips("NM")) or (state eq stfips("AZ")) or (state eq stfips("UT")) or (state eq stfips("NV")) or (state eq stfips("CA"))) then do; output; end; run; %make_rectangle(annogrid,grid,.1,.1,color,color,'Y','N',&predvar); run; %draw_map(mystates, mystates, annogrid,state,state,oil,png,"N"); run;