BookmarkSubscribeRSS Feed
BobHope
Quartz | Level 8

Hi,

I am really frustrated with this (In my opinion there should be an option for this in SAS). My intention is to graph a pie chart from the process. The process has three stages (on,off,sleep (not neccessary all of them)) and the shifting times vary case to case. I would need a pie chart from the cumulative time the process has been in each stage on each process.I have several(random) number of these processes and I have made a macro to loop through all the processes and graph the graphs for each process. However I would need the pie chart to always show the same color for the stage in the pie chart for each process. And that is  where the problem occures.

Here is my code (inside a do loop i=1 to number of processes):

proc gchart data=print&i;

pie state2 /sumvar=duration descending value=none nogroupheading noheading nolegend;

  %if %eval(&&count&i)=3 %then %do;

pattern1 color=&col1;

pattern2 color=&col2;

pattern3 color=&col3;

%end;

%if %eval(&&count&i)=2 %then %do;

pattern1 color=&col1;

pattern2 color=&col2;

%end;

%if %eval(&&count&i)=1 %then %do;

pattern1 color=&col1;

%end;

run;

goptions reset=all colors=;

&&count&i tells how many stages the process has had (1-3)

and the &col1-&col3 gets values from the dataset(which has a variable telling the color) green yellow red depending on which stages appear on the process.

I have tested with %put that all the macro variables get the value wanted, so that is not the problem. Also the %if loops works as intented. However SAS doesn't take notice on the colors (Actually it does it randomly for example one chart can be in the right form but then the next gets 2 greens and one pink, or the colors are mixed so that yellow=green red=yellow green=red etc.)

The problem has to in the pattern statement itself or with the reset option idk. I hope this wasn't too confusing. Please help.

7 REPLIES 7
Cynthia_sas
SAS Super FREQ


Hi, you have to read the doc on the PATTERN statement. Colors are not always assigned intuitively when you use a PATTERN statement. Robert's example from the Tech Support is a good example of how you have to "take control" if you want colors to stay consistent across groups.

cynthia

BobHope
Quartz | Level 8

Hi, and thanks for the answers. However the given code won't work as intended. My case is similar to what is in the example, in my case my grp is process ID and flavor is the state. I copy paste the code Robert posted here and replace all the variable references to fit my data. The results are even worse than with my code in the first post. The pies are either one colored or bicolored (never 3 colors). With R this would be a nobrainer, but unofortunately I have to get this to work on SAS too.

One change I had to remove, patternid=midpoints from the options since pie statement doesn't take that as an option.

GraphGuy
Meteorite | Level 14

Could you post some sample data?

DanH_sas
SAS Super FREQ

The following code should get you what you want without all of the complexity (and no loops required :-)). The DISCRETEATTRMAP defines the association between values and the graph attributes (in this case, just color). A variable called ATTRSEX is defined in the DISCRETEATTRVAR statement to bind your real variable (sex) with the attrmap (gender). Then, just use that variable on the PIECHART statement. The attribute will be applied correctly regardless of data order, and data "drop outs" will no longer be a problem. If you have any questions, just let me know.

Thanks!

Dan

proc template;

define statgraph pie;

begingraph;

  discreteattrmap name="gender";

    value "F" / fillattrs=(color=pink);

    value "M" / fillattrs=(color=blue);

  enddiscreteattrmap;

  discreteattrvar attrvar=attrsex var=sex attrmap="gender";

  layout region;

    piechart category=attrsex response=height / name="mypie";

    discretelegend "mypie";

  endlayout;

endgraph;

end;

run;

proc sgrender data=sashelp.class template=pie;

/* Put your BY statement here if you are doing a BY-group */

run;

BobHope
Quartz | Level 8

This sounds promising. However a ton of errors are created while running the code below

proc template;

define statgraph pie;
begingraph;
  discreteattrmap name="state";
    value "On" / fillattrs=(color=green);
    value "Off" / fillattrs=(color=red);
value "Standby" / fillattrs=(color=yellow);
  enddiscreteattrmap;
  discreteattrvar attrvar=attrstate var=state attrmap="state";
  layout region;
    piechart category=attrstate response=duration / name="State distribution";
    discretelegend "State distribution";
  endlayout;
endgraph;
end;

run;

it seems that discreteattrmap is an invalid statement and piechart too... My sas version is 9.2 and I have had huge struggles with pie chart templates before (are they even possible to make?) however these are the errors I get:

19           discreteattrmap name="state";

             _______________

             180

20             value "On" / fillattrs=(color=green);

               _____

               180

21             value "Off" / fillattrs=(color=red);

               _____

               180

22          value "Standby" / fillattrs=(color=yellow);

            _____

            180

23           enddiscreteattrmap;

             __________________

             180

24           discreteattrvar attrvar=attrstate var=state attrmap="state";

             _______________

             180

25           layout region;

                    ______

                    22

                    76

26             piechart category=attrstate response=duration / name="State distribution";

               ________

               1

ERROR 180-322: Statement is not valid or it is used out of proper order.

ERROR 22-322: Syntax error, expecting one of the following: DATALATTICE, DATAPANEL, GRIDDED, LATTICE,

              OVERLAY, OVERLAY3D, OVERLAYEQUATED, PROTOTYPE.

ERROR 76-322: Syntax error, statement will be ignored.

 

WARNING 1-322: Assuming the symbol PIECHARTPARM was misspelled as piechart.

Thanks!

DanH_sas
SAS Super FREQ

Yes, you need at least SAS 9.3 for this to work. Sorry, I was not sure what version of SAS you had.

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
  • 7 replies
  • 1847 views
  • 3 likes
  • 4 in conversation