Assign style to variable based off another variables value?

Reply
Frequent Contributor
Posts: 75

Assign style to variable based off another variables value?

Is there a way I can assign a STYLE value to a variable, based off the value of another variable?

Example:

PROC PRINT DATA=FRUITS LABEL NOOBS;

  VAR APPLE ORANGE;

  VAR PEAR / STYLE(data)={foreground=white background=blue};

RUN;

In this example I would like the STYLE to apply only when the APPLE variable equals "RED" or "YELLOW".

I've found an example using PROC FORMAT (but fails on me):

PROC FORMAT;

  VALUE $BACKCOLOR

     'RED' = 'blue'

     'YELLOW' = 'blue';

RUN;

PROC FORMAT;

  VALUE $FORECOLOR

     'RED' = 'white'

     'YELLOW' = 'white';

RUN;

PROC PRINT DATA=FRUITS LABEL NOOBS;

  VAR APPLE ORANGE;

  VAR PEAR / STYLE(data)={foreground=FORECOLOR. background=BACKCOLOR.};

RUN;

Whenever I execute the above code, it fails stating "THE FORMAT BACKCOLOR WAS NOT FOUND OR COULD NOT BE LOADED."

In these examples though, it needs to be considered that the var PEAR is numeric. Is this what is causing my problem?

Super User
Posts: 11,343

Re: Assign style to variable based off another variables value?

You declared your formats as text, correctly, but referenced them in the style as numeric. In the style section use $forecolor. and $backcolor.

Frequent Contributor
Posts: 75

Re: Assign style to variable based off another variables value?

At that folks, is why I'm still a NOOB. Thanks ballardw. That gets me past my error. Now how do I code it to check the var APPLE in the print statement? I don't think you can use IF/ELSE statements on PROC PRINT correct?

Super Contributor
Posts: 307

Re: Assign style to variable based off another variables value?

I'm not entirely sure what you mean by "how do I code it to check the var APPLE in the print statement?" Do you mean to apply the same formatting style to the APPLE var in the print statement? If so, then just specify all the variable names with the VAR statement where you want the style applied, e.g.:

PROC PRINT DATA=FRUITS LABEL NOOBS;

     VAR APPLE ORANGE PEAR / STYLE (DATA) = (FOREGROUND = $FORECOLOR. BACKGROUND = $BACKCOLOR.);;

RUN;

note: because of the way you have specified your formats, there is no difference in how "RED" or "YELLOW" are displayed. You could try something like this instead:

PROC FORMAT;
  VALUE $BACKCOLOR
     'RED' = 'red'
     'YELLOW' = 'yellow';
RUN;

PROC FORMAT;
  VALUE $FORECOLOR
     'RED' = 'white'
     'YELLOW' = 'black';
RUN;

SAS Super FREQ
Posts: 8,864

Re: Assign style to variable based off another variables value?

Hi:

  You cannot do that kind of conditional processing with PROC PRINT. However, you can use a COMPUTE block in PROC REPORT to alter the style of one cell based on the value in another cell. for example:

ods _all_ close;
proc format;
  value $bck 'F' = 'pink'
             'M' = 'lightblue';
run;

   

ods html file='c:\temp\hilite_val.html' style=sasweb;

proc report data=sashelp.class nowd;
  column name sex age height weight;
  define name / order;
  define sex / display
         style(column)={background=$bck.};
  define age / display;
  define height / display;
  define weight / display;
  compute height;
    if name = 'Alfred' then do;
       call define(_col_,'style','style={background=lightgreen}');
       call define('age','style','style={background=lightgreen}');
    end;
    else if name = 'Alice' then do;
       call define(_col_,'style','style={background=yellow}');
       call define('name','style','style={background=yellow}');
    end;
    else if substr(name,1,1) = 'J' then do;
       length svar $80;
       ** make style value dynamically using format for SEX variable value;
       svar = catt('style={background=',put(sex,$bck.),'}');
       call define(_col_,'style',svar);
    end;
    endcomp;
run;
ods html close;

  Only PROC REPORT has the COMPUTE block and the CALL DEFINE statement, which provide you a way to impact the style of one cell based on the value in a different cell. Do note, however, that you can only impact variables that appear -before- the variable of interest in the COLUMN staement. so, for example, in the COMPUTE block for HEIGHT, I can impact NAME, SEX, AGE and HEIGHT, but the COMPUTE block for HEIGHT cannot touch or impact the WEIGHT column. This is a timing issue, because when PROC REPORT is placing the column for HEIGHT on the report row, it has already placed NAME, SEX, and AGE, but has not yet placed WEIGHT. So the order of the variables on the COLUMN statement is important when altering variables in a COMPUTE block.

  In my code, the column for SEX is directly highlighted using the user defined format in the DEFINE statement. For the background for HEIGHT and AGE are changed when name='Alfred'; the background of HEIGHT and NAME are chanced when name='Alice' and for all the names where the first initial is 'J', the value for SEX is used to highlight the HEIGHT cell.

cynthia


dynamic_hilite_one_var_based_on_other_val.png
Ask a Question
Discussion stats
  • 4 replies
  • 616 views
  • 6 likes
  • 4 in conversation