I am trying to make a plot where the lines have different colors for each group (achieved with seriesplot and group) and there is some kind of marker to indicate whether the trend is increasing or decreasing (not achieved).
I would like to use markers because the trends are subtle and not necessarily obvious on the plot (even though for this data an increase of 1 or 2% is important.) I tried using the linepatterngroup statement, but the line patterns that SAS automatically chose are hard to distinguish.
Here is what I have:
proc template; define statgraph groupgraph; begingraph / subpixel=on; entrytitle 'TITLE'; layout overlay / xaxisopts=(display=(ticks tickvalues) label="Month") yaxisopts=(griddisplay=on linearopts=(viewmax=0.30) label="% of monthly x") ; seriesplot x=mo y=mo_perc / group=l_group name='a' lineattrs=(thickness=2) smoothconnect=true linecolorgroup=l_group linepatterngroup=trend_mo; scatterplot x=mo y=mo_perc / group=trend_mo name='b' markerattrs=(symbol=(circlefilled starfilled)); layout gridded / halign=right valign=top columns=2 columngutter=5; discretelegend 'a' / title='Group' type=linecolor location=outside across=4 halign=left valign=bottom; discretelegend 'b' / title='Trend' location=outside across=4 halign=left valign=bottom itemsize=(linelength=30); endlayout; endlayout; endgraph; end; run;
The error after the markerattrs statement is:
proc template; define statgraph symbolchange; begingraph; layout overlay; scatterplot y=height x=weight / markerattrs=(symbol=circlefilled); endlayout; endgraph; end; run;I generally find the limited examples in SAS documentation to be frustrating since I find examples more helpful than the syntax statements.
Is there a specific reason you're using SGTEMPLATE and not just SGPLOT?
If you use SGPLOT, use a discrete data attribute map.
https://documentation.sas.com/doc/en/vdmmlcdc/8.11/grstatproc/n16ad7597ctbm3n1x9pvpeo5g4gx.htm
Here's a better source of examples for base graphics:
http://robslink.com/SAS/ods2/aaaindex.htm
I'm assuming you also know to reference the Examples section in the documentation?
@kz_ wrote:
I am trying to make a plot where the lines have different colors for each group (achieved with seriesplot and group) and there is some kind of marker to indicate whether the trend is increasing or decreasing (not achieved).
I would like to use markers because the trends are subtle and not necessarily obvious on the plot (even though for this data an increase of 1 or 2% is important.) I tried using the linepatterngroup statement, but the line patterns that SAS automatically chose are hard to distinguish.
Here is what I have:
proc template; define statgraph groupgraph; begingraph / subpixel=on; entrytitle 'TITLE'; layout overlay / xaxisopts=(display=(ticks tickvalues) label="Month") yaxisopts=(griddisplay=on linearopts=(viewmax=0.30) label="% of monthly x") ; seriesplot x=mo y=mo_perc / group=l_group name='a' lineattrs=(thickness=2) smoothconnect=true linecolorgroup=l_group linepatterngroup=trend_mo; scatterplot x=mo y=mo_perc / group=trend_mo name='b' markerattrs=(symbol=(circlefilled starfilled)); layout gridded / halign=right valign=top columns=2 columngutter=5; discretelegend 'a' / title='Group' type=linecolor location=outside across=4 halign=left valign=bottom; discretelegend 'b' / title='Trend' location=outside across=4 halign=left valign=bottom itemsize=(linelength=30); endlayout; endlayout; endgraph; end; run;The error after the markerattrs statement is:
ERROR 22-322: Expecting a name.ERROR 200-322: The symbol is not recognized and will be ignored.I was going off this (https://documentation.sas.com/doc/en/pgmsascdc/v_015/grstatgraph/p0icsws0thpaghn1t12m27fyqnjc.htm😞proc template; define statgraph symbolchange; begingraph; layout overlay; scatterplot y=height x=weight / markerattrs=(symbol=circlefilled); endlayout; endgraph; end; run;I generally find the limited examples in SAS documentation to be frustrating since I find examples more helpful than the syntax statements.
@kz_ wrote:
In this series plot example (http://robslink.com/SAS/ods2/aaaindex.htm), the markers/colors are the same for both groups. I am trying to get different markers for the different groups.
I do realize that there are SAS examples - like the one I tried to copy. I was hoping that comment would get me something other than a reference to SAS documentation, which I usually check first.
The linked page shows 4 series plots. Which one? None of those creates the same color for" both groups" unless possibly you are using gray-scale style and can't tell the "colors" apart. None of those examples use Proc template and Sgrender either. So, what code are you using?
Being "old school" I would plot the lines, and then annotate the markers (using data step logic when you create the annotate dataset, to control which markers to use).
Here's how to setup an attribute map in GTL using the example you listed earlier:
ods path WORK.TEMPLAT(UPDATE) SASHELP.TMPLMST (READ);
proc template;
define statgraph symbolchange;
begingraph;
discreteattrmap name="attrs" / ignorecase=true DISCRETELEGENDENTRYPOLICY=ATTRMAP;
value 'F' / lineattrs=(color=red pattern=1) markerattrs=(symbol=triangle color=blue);
value 'M' / lineattrs=(color=blue pattern=2) markerattrs=(symbol=triangle color=red);
enddiscreteattrmap;
discreteattrvar attrvar=_attrs_ var=sex attrmap="attrs" ;
layout overlay;
seriesplot y=height x=weight / group=_attrs_ name='a';
scatterplot y=height x=weight / group=_attrs_ name='b';
mergedlegend 'a' 'b'/ location=inside autoalign=(bottom);
endlayout;
endgraph;
end;
run;
ods graphics / reset;
proc sgrender data=sashelp.class template=symbolchange;
run;
The discreteattrmap block is similar to a proc format section, but for each value you list out you can set the individual style elements (markerattrs, lineattrs, etc.). You then setup a temporary variable with discreteattrvar that gets mapped to the variable that contains the values, and use that temporary variable in a GROUP option in your graph statements.
If you need the style to be based on two different variables (for lines and markers) there are two options. First is to make two different attribute maps (named differently) and assign them to your series and scatter plot statements individually. The second option is to make a new variable in your dataset with the unique combinations of the two variables (e.g. A and B = 1, A and C=2, B and C=3) and use the new variable in the attribute map.
In the above example I used a merged legend to combine the series and scatter plot legends together to get the appropriate legend that shows lines and markers together. There's supposed to be a way that you can directly feed an attribute map into a legend but I haven't been able to get it to work so there could be a bug there.
Hi, I was also looking for a quick way to get different markers for different groups in PROC SGPLOT and found this.
ods graphics on / attrpriority=none; proc sgplot data=sashelp.iris; title 'Fisher (1936) Iris Data'; styleattrs datasymbols=(circlefilled squarefilled starfilled); scatter x=petallength y=petalwidth / group=species markerattrs=(size=5px); run; ods graphics / reset;
Source
https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.3/statug/statug_odsgraph_sect013.htm
You might want to brush up on attribute priority. I hope you find this discussion helpful. We needed options for black and white, color, color but ok for color blind, etc, and so we came up with attribute priority. http://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/statug/statug_odsgraph_sect050.htm
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 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.
Ready to level-up your skills? Choose your own adventure.