SAS sgplot step color gradient

785 Views Asked by At

I want to generate a "step" plot (CDF) and I'm trying to change the line color using the dattrmap option. But color are not changing. Below is my code:

%MACRO ATRRMAP(fich=,var=);
proc freq data=&fich noprint;
    tables &var/nocum nopercent norow nocol out=freq&var;
    format _all_;
    where &var^=.;
run;
data test;
    set freq&var end=eof;
    call symputx("mvCAT"||strip(_N_),&var);
    if eof then call symputx("NB",_N_);
run;
data myattrmap;
    length id $20 value 3 linecolor $10 pattern 3 fillcolor $20;
    %do i=1 %to &NB;
        id='myid';
        value = &&mvCAT&i;
        linecolor=cats("grey",put(&i*5,hex2.));
        %if &i=1 or &i=5 or &i=9 %then %do;
            pattern = 1;
        %end;%else %if &i=2 or &i=6 or &i=10 %then %do;
            pattern = 15;
        %end;%else %if &i=3 or &i=7 or &i=11 %then %do;
            pattern = 2;
        %end;%else %if &i=4 or &i=8 or &i=12 %then %do;
            pattern = 8;
        %end;%else %do;
            pattern = 41;
        %end;
        fillcolor=cats("grey",put(&i*5,hex2.));
        output;
    %end;
run;
%MEND ATRRMAP;

The generated data look like the following:

id value pattern fillcolor
myid -6 1 CXbdc3c7
myid -5 2 CXbdc3c7
myid -4 8 CXbdc3c7

Then, I used the sgplot:

PROC SGPLOT DATA=cumul sganno=annotation NOBORDER dattrmap=myattrmap;
        STEP X=variable Y=percent/GROUP=newgroup attrid=myid;
        YAXIS LABEL="Cumulative percentage of patients" VALUES=(0 TO 100 BY 
             10);
        XAXIS LABEL=" " VALUES=(-4 to 4 by 0.5) ;
        KEYLEGEND /TITLE=" " LOCATION=INSIDE POSITION=BOTTOMRIGHT ACROSS=1 
              DOWN=3 NOBORDER;
RUN;

The data myfile used with sgplot looks like the following:

variable percent newgroup
-3.66   2.70    -6
-3.41   5.40    -6
-3.26   8.11    -6
-3.28   5.8     -5
-2.97   13.51   -5

I would like to have a grey gradient. But first, I would simply like to choose, with dattrmap, color lines on my plot. I try with fillcolor and linecolor but it did not work. I try to change the color directly in the SGPLOT statement with the datacontrastcolors option of styleattrs and it works. Does someone see what am I missing ?

1

There are 1 best solutions below

2
On BEST ANSWER

It has to be the GROUP = variable that is the variable that controls the color, shape and patterns. You're grouping your variables by NEWGROUP not the values. You could create a proxy to do this though, if that's what you want. Without some more details of what you need, I'm not sure how we can help you find a work around, but this does explain why it's not working at the moment.

From documentation:

The values of the VALUE variable are valid data group values. These values are case sensitive. The data group is assigned in the plot statement with the GROUP= option.

Assuming you do want the lines different color based on the NEWGROUP here's how you can modify your code. Note that I've simplified your code drastically and there were issues with how you're specifying color - I ignored those for now and am leaving that up to you to fix. The values are currently hard coded in the macro. I would also recommend changing the if _n_ portion to use the MOD() function since you seem to have some sort of pattern in your data. It may not work, but worth considering.

*create fake data;
data myfile;
    input variable percent newgroup $;
    cards;
-3.66 2.70 group1
-3.41 5.40 group1
-3.26 8.11 group1
-3.28 5.8 group2
-2.97 13.51 group2 
;;;;
run;

*macro to create attribute map;
%MACRO ATRRMAP(fich=,var=);

    proc freq data=&fich noprint;
        tables &var/nocum nopercent norow nocol out=freq&var (drop=percent);
        format _all_;
        where not missing(&var);
    run;

    data myattrmap;
        length id $20 value $20 linecolor $10 pattern 3 fillcolor $20;
        set freq&var.;
        id='myid';
        value = &var.;

        if _n_ =1 then
            linecolor = 'CXbdbdbd';
        else if _n_=2 then
            linecolor = 'CX636363';

        *linecolor=cats("grey",put(_n_*5,hex2.));
        if _n_ in (1, 5, 9) then
            pattern = 1;
        else if _n_ in (2, 6, 10) then
            pattern = 15;
        else if _n_ in (3,  7, 11) then
            pattern = 2;
        else if _n_ in ( 4, 8, 12) then
            pattern=8;
        else  pattern = 14;
        fillcolor=cats("grey",put(_n_*5,hex2.));
        output;
    run;

%MEND ATRRMAP;

*create attribute map for newgroup;
%ATRRMAP(fich=myfile, var=newgroup);

*plot graph;
PROC SGPLOT DATA=myfile NOBORDER dattrmap=myattrmap;
    STEP X=variable Y=percent/GROUP=newgroup attrid=myid;
    YAXIS LABEL="Cumulative percentage of patients" VALUES=(0 TO 100 BY 
        10);
    XAXIS LABEL=" " VALUES=(-4 to 4 by 0.5);
    KEYLEGEND /TITLE=" " LOCATION=INSIDE POSITION=BOTTOMRIGHT ACROSS=1 
        DOWN=3 NOBORDER;
RUN;

Methods & rules for colour scheme names are found here.