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

I'm experimenting with ods graphics to create a scatterplot for a grouped variable with two categories. The values are marked by a circle and a plus sign but I'd prefer to use 'X' and 'Y'.

This is how I assumed it should be done:

proc sgscatter data=mydata;

    plot myXvar*mYvar / group=myGroup|Var MARKERATTRS=GraphData1(symbol=X) MARKERATTRS=GraphData2(symbol=Y);

run;

But this sets the symbols in both groups to "Y", the first markerattrs option is ignored.

From the SAS 9.4 documentation:

MARKERATTRS=style-element <(options)> | (options

GraphData1 ... GraphDatan style elements in the current style for grouped data.

SAS(R) 9.4 ODS Graphics: Procedures Guide, Fourth Edition

One way to do this would be to use a modified template but I'd prefer not to do it that way. I use Enterprise Guide to develop the program, using a template would complicate matters.

Does anyone know how to create separate symbols using the markerattrs options in sgscatter (or another ods graphics procedure? I'm using SAS 9.4 with EG 6.1. I'm using the "analysis" style for the ods graphics.

1 ACCEPTED SOLUTION

Accepted Solutions
djrisks
Barite | Level 11

Hi,

There are at least two ways to do this:

One of the simpler ways is to use the marker character option, as in the example below. However, the legend does not contain the X and Y. All you need to do is adapt the program so that one of your groups are assigned the letter "X" and the other group is assigned the letter "Y".

Because you have SAS 9.4 though, it might be better if you use the Attribute maps option, as also shown in the example further below. This also gives you the X and Y symbols on your legend.


data cars;
  set sashelp.cars;
  where make in ("Acura","Audi");
  if make = "Acura" then Groupmarker = "X";
  else if make = "Audi" then Groupmarker = "Y";
  keep make cylinders horsepower Groupmarker;
run;

/* Using Markercharacter option */

proc sgplot data = cars;
  scatter x = CYLINDERS y = HORSEPOWER / group = make markercharacter = Groupmarker;
run;


data myattrmap;
  input ID $ value $ markersymbol $ ;
  datalines;
  myid  Acura X
  myid  Audi Y
  ;
run;


/* Using Attribute Maps */

proc sgplot data=cars dattrmap=myattrmap;
  scatter x = CYLINDERS y = HORSEPOWER / group = make attrid=myid;
run;

Please let me know if you have any questions.

View solution in original post

12 REPLIES 12
djrisks
Barite | Level 11

Hi,

There are at least two ways to do this:

One of the simpler ways is to use the marker character option, as in the example below. However, the legend does not contain the X and Y. All you need to do is adapt the program so that one of your groups are assigned the letter "X" and the other group is assigned the letter "Y".

Because you have SAS 9.4 though, it might be better if you use the Attribute maps option, as also shown in the example further below. This also gives you the X and Y symbols on your legend.


data cars;
  set sashelp.cars;
  where make in ("Acura","Audi");
  if make = "Acura" then Groupmarker = "X";
  else if make = "Audi" then Groupmarker = "Y";
  keep make cylinders horsepower Groupmarker;
run;

/* Using Markercharacter option */

proc sgplot data = cars;
  scatter x = CYLINDERS y = HORSEPOWER / group = make markercharacter = Groupmarker;
run;


data myattrmap;
  input ID $ value $ markersymbol $ ;
  datalines;
  myid  Acura X
  myid  Audi Y
  ;
run;


/* Using Attribute Maps */

proc sgplot data=cars dattrmap=myattrmap;
  scatter x = CYLINDERS y = HORSEPOWER / group = make attrid=myid;
run;

Please let me know if you have any questions.

ckx
Quartz | Level 8 ckx
Quartz | Level 8

Thanks for the quick reply, it worked brilliantly! I'm particularly happy to learn about the dattrmap option, that adds a lot of flexibiltiy.

djrisks
Barite | Level 11

You're welcome. Yes that's correct Attribute Maps add a lot of flexibility! Did you use the dattrmap option for your example?

ckx
Quartz | Level 8 ckx
Quartz | Level 8

I tried both options.The dattrmap option gave me a spot of trouble because I had a numeric grouping variable. After some experimentation I found that creating the variable VALUE from the formatted values of my group variable worked great.

djrisks
Barite | Level 11

That's great! Oh yes I remember when I initially had problems when using dattrmaps with formats...

Jay54
Meteorite | Level 14

If you have SAS 9.4M1, the right way to do this is using the SYMBOLCHAR statement.  This statement allows you to define a marker from any Unicode font symbol.  The legend will also include the symbol.  See example code below.  There is also a similar SYMBOLIMAGE statement..

proc sgplot data=sashelp.class;

  symbolchar name=X  char='0058'x / scale=2;

  symbolchar name=Y  char='0059'x / scale=2;

  styleattrs datasymbols=(X Y);

  scatter x=height y=weight / group=sex;

  run;

ckx
Quartz | Level 8 ckx
Quartz | Level 8

Yes indeed, the symbolchar method works as well. I think I would still prefer the dattrmap option because the documentation seems to be clearer. But that's just a ver first impression.

Jay54
Meteorite | Level 14

For future reference, with SAS 9.4, all you really need is a STYLEATTRS statement as shown below.  You can use StyleAttrs to set group symbols, colors, line patterns,etc.

proc sgplot data=cars;

  styleattrs datasymbols=(x y);

  scatter x = CYLINDERS y = HORSEPOWER / group = make;

run;

STYLEATTRS was added at SAS 9.4.  With SAS 9.3, Attrmap is the way to go, and is effectively doing the same thing.  Yes, this can be a bit confusing, but that is the price of having a system that is being actively enhanced.  🙂

However, if you need some symbol that is not included in the pre-defined list, you can define it using SYMBOLCHAR, and then use StyleAttrs or Attrmap to include it in the list of group symbols.

djrisks
Barite | Level 11

Hi Sanjay,

Out of interest, does Styleattrs work with the group = variable? If so what will happen if you use more than one group = variable please?

Many thanks

Jay54
Meteorite | Level 14

Yes, it works with group case, and also for direct assignment of attributes.

STYLEATTRS is simply an easy, in-line code way to change the GraphData1-12 elements in the active style.  Instead of having to derive a new style with the changed group values, you can define the new values in the SGPLOT code itself.  This is a easy way for a quick, non persistent change.  For persistent change, it is still better to derive a corporate style.

STYLEATTRS has four bundles:

  • DATACOLORS for the fill colors,
  • DATACONTRASTCOLORS for the line and marker colors,
  • DATASYMBOLS
  • DATALINEPATTERNS. 

The list you provide REPLACES the list from the style.  In the case above, I changed the Symbols list to (X, Y).  So, now the graph will cycle through only these two marker symbols.  All other group lists will stay unchanged.  For symbols, you can include the pre-defined symbol names (like Circle, CircleFilled, etc), or a new symbol name created using the SYMBOLCHAR or SYMBOLIMAGE statements. 

Also remember, the HTMLBlue style has ATTRPRIORITY=COLOR.  So, symbols change only after the colors are exhausted. To see symbol cycling, use a different style, or set ATTRPRIORITY=NONE in the ODS Graphics statement.  Sounds like a good topic for a blog article.  🙂

djrisks
Barite | Level 11

Thank you for the explantion!

It looks so easy to use, and it appears that the Colors and Symbols can be changed simultaneously. It's a welcomed upgrade to the %modstyle macro.

I agree, it sounds like a good topic for Graphically Speaking 🙂

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 12 replies
  • 11870 views
  • 12 likes
  • 3 in conversation