BookmarkSubscribeRSS Feed
m195
Calcite | Level 5

Below is my dummy code. I expected Variable A to be highlighted yellow in the same pattern as Variable B, but instead the whole column is yellow. I've switched the order of the compute blocks but get the same results. 

 

data mydata;
    input ID $ VarA VarB;
    datalines;
    A 10 10
    B 20 25
    C 30 30
    D 40 35
    ;
run;

proc report data=mydata;
    columns ID VarA VarB;

    define ID / display;
    define VarA / display 'Variable A';
    define VarB / display 'Variable B'; 

    compute VarA;
        if   VarB ne VarA then do;
            call define(_col_, "style", "style={background=yellow}"); 
        end;
    endcomp;

	compute VarB;
        if   VarB ne VarA then do;
            call define(_col_, "style", "style={background=lightred}");
        end;
    endcomp;
run;

 

... yields the following output. 

 

SAS Question.png

 

Why is this?

1 REPLY 1
Cynthia_sas
Diamond | Level 26

Hi:

  Your original code has a "timing" issue. Think about PROC REPORT as only placing one value on the report row at a time, following the order of the variables in the COLUMN statement from left to right. So on the first data row on the report (after the column headers), the first item that PROC REPORT puts on the report row is the value for ID -- at that point in PROC REPORT timing -- only the value of ID is known. When PROC REPORT puts the ID value on the report row, it hasn't placed VarA or VarB on the report row yet, so it has no visibility or knowledge about what those values are. 

  In the same way, next, PROC REPORT writes the value of VarA to the report row. Now, in a COMPUTE block for VarA, you can test either the value of ID OR the value of VarA. But, just like before, when PROC REPORT is putting the value for VarA on the report row, it doesn't know what the value is for VarB, so in terms of your comparison in the COMPUTE block for VarA, inside that COMPUTE block, PROC REPORT doesn't have any value on the report row yet for VarB, so the value of VarA can't be compared to VarB because VarB isn't on the report row yet.  So it's not the order of the COMPUTE blocks that will make a difference, but changing the placement of the conditional testing until AFTER all the report row has finished being built. That means in the COMPUTE block for VarB, you will have the ability to use your conditional logic because at the point in time when the COMPUTE block for VarB is executed, ALL of the items on the report row have been placed down, so now PROC REPORT knows the values for ID, VarA and VarB. Basically, you can remove the COMPUTE block for VarA, but move the IF statement into the block for VarB. You'll need to change the CALL DEFINE for the VarA IF to be sure that the correct column is highlighted, as shown in the screen shots for results and code below.

Cynthia

Results:

Cynthia_sas_1-1756246298826.jpeg

 

Code:

Cynthia_sas_2-1756246389251.jpeg

 

 

 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 244 views
  • 1 like
  • 2 in conversation