BookmarkSubscribeRSS Feed
CP2
Pyrite | Level 9 CP2
Pyrite | Level 9

hello,

I am trying to show two lines for each group in the group= statement of an sgplot.  One line would be the actual value and the other would be the 12 month moving average. I want the actual to be a solid line and the moving average to be a dashed line but I also want the colors to be the same for each item in the group. 

 

I thought that I could exclude the color from the lineattrs so that the defaults would just be used for each of the series but it's not working the way I had hoped. I've tried a lot of the suggestions online but still not working. Originally I want to do this with two by variables (drug and prodname) but that was making it more complicated. I ended up wrapping the plot in a macro and calling out each drug and product name to plot separately.

 

The following is an hypothetical example with a drug called 'abcdrug' and prodname is blank. Is there a simple statement I can add or ods graphics option I can use without creating numerous lines of code? I often want to do these sorts of graphs on the fly and sometimes not worth creating all the code for it. 

 

%macro shareplot(dname=, prod=) ;
proc sgplot data=share (where = (drug="&dname." and prodname="&prod.")) ;
title "Shares Over Time";
title2 "&dname., &prod." ;
series y=share x= month/ group=strength lineattrs=(thickness=2 pattern=solid) legendlabel="Share by Month" ;
series y=share_12 x= month/ group=strength lineattrs=(thickness=2 pattern=longdash) legendlabel="Share by Month - 12 month MA" ;
YAXIS LABEL = 'Share (%)' ;
YAXIS min=0 max=1 ;
FOOTNOTE " ";
where MONTH>='01JAN2013'd ;
run;
%mend plot ;
%shareplot(dname=abcdrug);

7 REPLIES 7
Reeza
Super User

Have you looked at DATA ATTRIBUTE Maps? That's likely what you're after. 

 

https://blogs.sas.com/content/iml/2017/01/30/auto-discrete-attr-map.html

CP2
Pyrite | Level 9 CP2
Pyrite | Level 9

thanks for your response. I spent time using the suggestions in the link but I still cannot get the colors to be in sync between the actual and moving average. 

Reeza
Super User

Post your code so we can see and work off what you've already done please.

CP2
Pyrite | Level 9 CP2
Pyrite | Level 9
I suppose i can add 'lineattrs' in the map instead of marketstyleelement and hardcode in the colors. I'll try this. Let me know if you have other suggestions related to data attribute maps.
Reeza
Super User

Without seeing what you've already done, it's pretty hard to make any suggestions here.

 


@CP2 wrote:
I suppose i can add 'lineattrs' in the map instead of marketstyleelement and hardcode in the colors. I'll try this. Let me know if you have other suggestions related to data attribute maps.

 

CP2
Pyrite | Level 9 CP2
Pyrite | Level 9
can you help me figure out why I am getting errors with this supposedly simple loop and scan? I'm trying to add a color to the attribute map.
%let VarName = strength_; /* specify name of grouping variable */
proc freq data=Have
ORDER=FORMATTED; /* or ORDER=DATA|FREQ */
tables &VarName / out=Attrs(rename=(&VarName=Value));
run;
%let colorlist=blue, red, green, lightblue, cyan, orange, fuscia, brown, lavendar, purple ;
%LET color_count = %sysfunc(countw(%quote(&colorlist), %str(,) )) ;
data DAttrs;
ID = "&VarName";
set Attrs(keep=Value);
length linecolor $11. linecolor $100. ;
do i = 1 to %eval(&color_count.) ;
linecolor = %scan(&colorlist., i ) ;
put linecolor ;
end ;
run;

Ksharp
Super User
Check option grouplc= seriese ......./group=strength grouplc= Or statment : styleattrs ...........

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 7 replies
  • 1443 views
  • 0 likes
  • 3 in conversation