Data visualization with SAS programming

How to change color on the map and more.

Reply
Occasional Contributor
Posts: 10

How to change color on the map and more.

I'm a SAS beginner. I have the following code:

What should I do to label all the counties to be yellow if the last number is greater than 9.7 (see all the underlined data)? If the last number is smaller than 9.7, I want the county map to be black and white.

 

What should I do to remove the word "county" from the map to save some space? For example: I want the Adams County to be shown on map as "Adams" instead of "Adams County". I want to remove the word "county" from each and every county names.

Please help. Thank you very much!

 

 

 

data sample(rename=(county=county_name));

  input County $19. @21 Data $8.;

  /* determine the name of the county to merge with MAPS.CNTYNAME to

     obtain the county FIPS code */

  countynm=upcase(transtrn(county,'County',''));

  datalines;

Adams County        1/2/7.3

Asotin County       1/2/2.7

Benton County       1/2/9.2

Chelan County       1/2/9.7

Clallam County      1/2/19.8

Clark County        1/2/5.8

Columbia County     1/2/7.4

Cowlitz County      1/2/5.5

Douglas County      1/2/9.5

Ferry County        1/2/12.4

Franklin County     1/2/7.3

Garfield County     1/2/3.3

Grant County        1/2/6.5

Grays Harbor County 1/2/6.5

Island County       1/2/12.2

Jefferson County    1/2/10

King County         1/2/8.5

Kitsap County       1/2/10.4

Kittitas County     1/2/8.1

Klickitat County    1/2/3.4

Lewis County        1/2/6.7

Lincoln County      1/2/10.4

Mason County        1/2/8.5

Okanogan County     1/2/11.5

Pacific County      1/2/3.4

Pend Oreille County 1/2/11

Pierce County       1/2/9.8

San Juan County     1/2/9.6

Skagit County       1/2/16.9

Skamania County     1/2/7.1

Snohomish County    1/2/12.1

Spokane County      1/2/9.9

Stevens County      1/2/12.4

Thurston County     1/2/11.6

Wahkiakum County    1/2/5.5

Walla Walla County  1/2/5.4

Whatcom County      1/2/25.9

Whitman County      1/2/5.7

Yakima County       1/2/4.5

;

run;

proc sort; by countynm;

data sample2(drop=countynm);

  merge sample maps.cntyname(where=(state=53) keep=state countynm county);

    by countynm;

run;

/* project a county map of Washington */

proc gproject data=maps.counties out=wa;

  where state=53 ;

  id state county;

run;

/* make the annotate macros available */

%annomac;

/* determine the visual center of the county */

%centroid(wa,center,state county);

 

/* create an annotate data set to place the label */

data anno;

   merge sample2 center;

    by state county;

  retain xsys ysys '2' when 'a' function 'label' color 'black' size 1;

  length text $20;

  text=data;

  position='5';

  output;

  text=county_name;

  position='8';

  output;

run;

 

/* You can define patterns to use to color the map areas using the

   PATTERN statement */

/* create the map */

proc gmap data=sample2 map=wa;

  id state county;

  choro data / nolegend anno=anno;

run;

quit;

Grand Advisor
Posts: 17,313

Re: How to change color on the map and more.

If you want to remove the word "COUNTY" you'll need to change your data. You can use the TRANSWRD function within a data step to remove the word COUNTY. 

 

There are a lot of papers on Lexjansen.com that show how to use PROC GMAP, you may want to start there. Here's one. 

http://www2.sas.com/proceedings/sugi29/251-29.pdf

 

Look at the MIDPOINTS option in GMAP to specify your boundaries and colors.

 

 

Grand Advisor
Posts: 10,196

Re: How to change color on the map and more.

Since your values for data are like "1/2/7.3" Then there is no last "number" as that is a character value.

You are not clear as to whether only the County or the County and the data are to show in yellow. I will assume the later.

Note that the LENGTH of your color variable (and function should be ) set to allow longer text.

 

data anno;
   merge sample2 center;
   by state county;
   LENGTH COLOR $ 8;
  retain xsys ysys '2' when 'a' function 'label' color 'black' size 1;
  length text $20;
  if scan(data,3,'/')>9.7 then color='yellow';
  County = strip(tranwrd(county,' County',''));
  text=data;
  position='5';
  output;
  text=county_name;
  position='8';
  output;
run;

 

Please post code in the code box opened with the {i} icon.

Occasional Contributor
Posts: 10

Re: How to change color on the map and more.

Thank you Ballardw for your inputs! I'm sorry that I did not state it clear.

Actually, I want to color all the counties to be yellow, not the county name. If it's like what you mentioned: there is no last "number", how can I color all the counties to be yellow where the last part of the data set is greater than 9.7?

Can you manually pick those counties out and color them to be yellow, and let the rest counties to be black and white? those counties include:Chelan,Clallam,Ferry,Island,Jefferson,Kitsap,Lincoln,Okanogan,Pend Orielle, pierce,skagit, snohomish, spokane, stevens, thurston, whatcom.

 

 

Thank you very much for your help! This is an urgent task!!!

Grand Advisor
Posts: 10,196

Re: How to change color on the map and more.

Generally for maps involving ranges of data then I use a combination of a custom format and pattern statements. Patterns override the default style colors. However there are problems with this approach because you are using text and 1/2/10.3 is less than 1/2/9.7 by the character sort orders. Create a numeric varaible that only has that actually value and use that for the choro variable. It is not uncommon to do such. Your annotate variable value does not have to be the same as the numeric. Then the format pattern would work:

 

Proc format;
   value myrange
   0 - <9.7 = '<9.7'
   9.7 -high = '9.7+'
   ;
run;
/* your other code goes here but need to incorporate
  a numeric data variable*/
pattern1 color=white;
pattern2 color=yellow;

proc gmap data=sample2 map=wa;
  id state county;
  choro numericdata / nolegend anno=anno;
run;
quit;

You could add:

 

Numericdata = input(scan(data,3,'/'),best5.);

to the SAMPLE2 datastep code to get a numeric variable.

 

When discussing map, or graph items in general, it is very import to specify whether you are talking about data values or appearance of map/graph features. You have for county potentially: the color of the displayed county AREA, the color of the displayed county BORDER, the color of the TEXT of the county name, the color of the BACKGROUND to the count name text for example. In other graphic procedures you may get to throw in things such as axis text labels, axis tickmarks, axis background to the text labels, colors of series lines, markers, or labels all involving the same variable name.

Occasional Contributor
Posts: 10

Re: How to change color on the map and more.

Thank you Ballard!

Can you please help me to check the following code and revised to get what I want?

I want the color of the displayed counties' AREA to be changed to yellow. And let the rest counties to be black and white.  The counties to be yellow including: Chelan,Clallam,Ferry,Island,Jefferson,Kitsap,Lincoln,Okanogan,Pend Orielle, pierce,skagit, snohomish, spokane, stevens, thurston, whatcom, where the last part of the data set is greater than 7.9.

 

Thank you very much!!!

 

 

 

 

data sample(rename=(county=county_name));

input County $19. @21 Data $8.;

/* determine the name of the county to merge with MAPS.CNTYNAME to

obtain the county FIPS code */

countynm=upcase(transtrn(county,'County',''));

datalines;

Adams County 1/2/7.3

Asotin County 1/2/2.7

Benton County 1/2/9.2

Chelan County 1/2/9.7

Clallam County 1/2/19.8

Clark County 1/2/5.8

Columbia County 1/2/7.4

Cowlitz County 1/2/5.5

Douglas County 1/2/9.5

Ferry County 1/2/12.4

Franklin County 1/2/7.3

Garfield County 1/2/3.3

Grant County 1/2/6.5

Grays Harbor County 1/2/6.5

Island County 1/2/12.2

Jefferson County 1/2/10

King County 1/2/8.5

Kitsap County 1/2/10.4

Kittitas County 1/2/8.1

Klickitat County 1/2/3.4

Lewis County 1/2/6.7

Lincoln County 1/2/10.4

Mason County 1/2/8.5

Okanogan County 1/2/11.5

Pacific County 1/2/3.4

Pend Oreille County 1/2/11

Pierce County 1/2/9.8

San Juan County 1/2/9.6

Skagit County 1/2/16.9

Skamania County 1/2/7.1

Snohomish County 1/2/12.1

Spokane County 1/2/9.9

Stevens County 1/2/12.4

Thurston County 1/2/11.6

Wahkiakum County 1/2/5.5

Walla Walla County 1/2/5.4

Whatcom County 1/2/25.9

Whitman County 1/2/5.7

Yakima County 1/2/4.5

;

run;

proc sort; by countynm;

data sample2(drop=countynm);

merge sample maps.cntyname(where=(state=53) keep=state countynm county);

by countynm;

run;

/* project a county map of Washington */

proc gproject data=maps.counties out=wa;

where state=53 ;

id state county;

run;

/* make the annotate macros available */

%annomac;

/* determine the visual center of the county */

%centroid(wa,center,state county);

 

/* create an annotate data set to place the label */

Proc format;

value myrange

0 - <9.7 = '<9.7'

9.7 -high = '9.7+'

;

run;

data anno;

merge sample2 center;

by state county;

retain xsys ysys '2' when 'a' function 'label' color 'black' size 1;

length text $20;

Numericdata = input(scan(data,3,'/'),best5.);

if scan(data,3,'/')>9.7 then color='yellow';

County = strip(tranwrd(county,' County',''));

 

text=data;

position='5';

output;

text=county_name;

position='8';

output;

run;

 

/* You can define patterns to use to color the map areas using the

PATTERN statement */

/* create the map */

proc gmap data=sample2 map=wa;

id state county;

choro data / nolegend anno=anno;

run;

quit;

Grand Advisor
Posts: 10,196

Re: How to change color on the map and more.

No pattern statement so you are going to get things in the default style colors.

 

You need to associate the format with the data values to be graphed. Something like: Format data MyRange. ; as part of the Proc Gmap code.

 

Without actual data that's about all I can suggest as this time.

Occasional Contributor
Posts: 10

Re: How to change color on the map and more.

This is my actual data:

Adams County 1/2/7.3

Asotin County 1/2/2.7

Benton County 1/2/9.2

Chelan County 1/2/9.7

Clallam County 1/2/19.8

Clark County 1/2/5.8

Columbia County 1/2/7.4

Cowlitz County 1/2/5.5

Douglas County 1/2/9.5

Ferry County 1/2/12.4

Franklin County 1/2/7.3

Garfield County 1/2/3.3

Grant County 1/2/6.5

Grays Harbor County 1/2/6.5

Island County 1/2/12.2

Jefferson County 1/2/10

King County 1/2/8.5

Kitsap County 1/2/10.4

Kittitas County 1/2/8.1

Klickitat County 1/2/3.4

Lewis County 1/2/6.7

Lincoln County 1/2/10.4

Mason County 1/2/8.5

Okanogan County 1/2/11.5

Pacific County 1/2/3.4

Pend Oreille County 1/2/11

Pierce County 1/2/9.8

San Juan County 1/2/9.6

Skagit County 1/2/16.9

Skamania County 1/2/7.1

Snohomish County 1/2/12.1

Spokane County 1/2/9.9

Stevens County 1/2/12.4

Thurston County 1/2/11.6

Wahkiakum County 1/2/5.5

Walla Walla County 1/2/5.4

Whatcom County 1/2/25.9

Whitman County 1/2/5.7

Yakima County 1/2/4.5

SAS Employee
Posts: 967

Re: How to change color on the map and more.

I've made a few small changes to your code, to get the map  you wanted...

 

I created a numeric variable containing the value of the last number in your 'data' field, and then created a Y/N variable saying whether or not to color the map area yellow...

 

val3=.; val3=scan(data,3,'/');
if val3>9.7 then yellow='Y'; else yellow='N';

 

I used the SAS tranwrd() function to convert the word 'County' to a blank...

 

text=tranwrd(county_name,'County','');

 

I added pattern statements so gmap will use white & yellow for the 'N' and 'Y' values of the yellow variable ...

 

pattern1 v=s c=white;
pattern2 v=s c=yellow;

 

And I had gmap use the yellow variable to color the map...

 

choro yellow / nolegend

 

 

Here's the complete code:

 

 

data sample(rename=(county=county_name));
input County $19. @21 Data $8.;
/* determine the name of the county to merge with MAPS.CNTYNAME to
obtain the county FIPS code */
countynm=upcase(transtrn(county,'County',''));
val3=.; val3=scan(data,3,'/');
if val3>9.7 then yellow='Y'; else yellow='N';
datalines;
Adams County 1/2/7.3
Asotin County 1/2/2.7
Benton County 1/2/9.2
Chelan County 1/2/9.7
Clallam County 1/2/19.8
Clark County 1/2/5.8
Columbia County 1/2/7.4
Cowlitz County 1/2/5.5
Douglas County 1/2/9.5
Ferry County 1/2/12.4
Franklin County 1/2/7.3
Garfield County 1/2/3.3
Grant County 1/2/6.5
Grays Harbor County 1/2/6.5
Island County 1/2/12.2
Jefferson County 1/2/10
King County 1/2/8.5
Kitsap County 1/2/10.4
Kittitas County 1/2/8.1
Klickitat County 1/2/3.4
Lewis County 1/2/6.7
Lincoln County 1/2/10.4
Mason County 1/2/8.5
Okanogan County 1/2/11.5
Pacific County 1/2/3.4
Pend Oreille County 1/2/11
Pierce County 1/2/9.8
San Juan County 1/2/9.6
Skagit County 1/2/16.9
Skamania County 1/2/7.1
Snohomish County 1/2/12.1
Spokane County 1/2/9.9
Stevens County 1/2/12.4
Thurston County 1/2/11.6
Wahkiakum County 1/2/5.5
Walla Walla County 1/2/5.4
Whatcom County 1/2/25.9
Whitman County 1/2/5.7
Yakima County 1/2/4.5
;
run;
proc sort; by countynm;
data sample2(drop=countynm);
merge sample maps.cntyname(where=(state=53) keep=state countynm county);
by countynm;
run;
/* project a county map of Washington */
proc gproject data=maps.counties out=wa;
where state=53 ;
id state county;
run;
/* make the annotate macros available */
%annomac;
/* determine the visual center of the county */
%centroid(wa,center,state county);

/* create an annotate data set to place the label */
data anno;
merge sample2 center;
by state county;
retain xsys ysys '2' when 'a' function 'label' color 'black' size 1;
length text $20;
text=data;
position='5';
output;
text=tranwrd(county_name,'County','');
position='8';
output;
run;

pattern1 v=s c=white;
pattern2 v=s c=yellow;
proc gmap data=sample2 map=wa;
id state county;
choro yellow / nolegend anno=anno;
run;
quit;

 

yellowmap.png

 

Ask a Question
Discussion stats
  • 8 replies
  • 288 views
  • 0 likes
  • 4 in conversation