Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • change table cell color based on conditional

    Hello,

    How to change the collect table cell color to red, based on flagPrice.

    thanks in advance,

    Code:
    webuse auto
    gen byte flagPrice = price > 10000
    collect style cell price[4010], warn
    table make ,stat(max price flagPrice ) nototal
    
    ----------------------------------------
                        |  Price   flagPrice
    --------------------+-------------------
    Make and model      |                   
      AMC Concord       |   4099           0
      AMC Pacer         |   4749           0
      AMC Spirit        |   3799           0
      Audi 5000         |   9690           0
      Audi Fox          |   6295           0
      BMW 320i          |   9735           0
      Buick Century     |   4816           0
      Buick Electra     |   7827           0
      Buick LeSabre     |   5788           0
      Buick Opel        |   4453           0
      Buick Regal       |   5189           0
      Buick Riviera     |  10372           1
      Buick Skylark     |   4082           0
      Cad. Deville      |  11385           1
      Cad. Eldorado     |  14500           1
      Cad. Seville      |  15906           1
      Chev. Chevette    |   3299           0
      Chev. Impala      |   5705           0
      Chev. Malibu      |   4504           0
      Chev. Monte Carlo |   5104           0
      Chev. Monza       |   3667           0
      Chev. Nova        |   3955           0
      Datsun 200        |   6229           0
      Datsun 210        |   4589           0
      Datsun 510        |   5079           0
      Datsun 810        |   8129           0
      Dodge Colt        |   3984           0
      Dodge Diplomat    |   4010           0
      Dodge Magnum      |   5886           0
      Dodge St. Regis   |   6342           0
      Fiat Strada       |   4296           0
      Ford Fiesta       |   4389           0
      Ford Mustang      |   4187           0
      Honda Accord      |   5799           0
      Honda Civic       |   4499           0
      Linc. Continental |  11497           1
      Linc. Mark V      |  13594           1
      Linc. Versailles  |  13466           1
      Mazda GLC         |   3995           0
      Merc. Bobcat      |   3829           0
      Merc. Cougar      |   5379           0
      Merc. Marquis     |   6165           0
      Merc. Monarch     |   4516           0
      Merc. XR-7        |   6303           0
      Merc. Zephyr      |   3291           0
      Olds 98           |   8814           0
      Olds Cutl Supr    |   5172           0
      Olds Cutlass      |   4733           0
      Olds Delta 88     |   4890           0
      Olds Omega        |   4181           0
      Olds Starfire     |   4195           0
      Olds Toronado     |  10371           1
      Peugeot 604       |  12990           1
      Plym. Arrow       |   4647           0
      Plym. Champ       |   4425           0
      Plym. Horizon     |   4482           0
      Plym. Sapporo     |   6486           0
      Plym. Volare      |   4060           0
      Pont. Catalina    |   5798           0
      Pont. Firebird    |   4934           0
      Pont. Grand Prix  |   5222           0
      Pont. Le Mans     |   4723           0
      Pont. Phoenix     |   4424           0
      Pont. Sunbird     |   4172           0
      Renault Le Car    |   3895           0
      Subaru            |   3798           0
      Toyota Celica     |   5899           0
      Toyota Corolla    |   3748           0
      Toyota Corona     |   5719           0
      VW Dasher         |   7140           0
      VW Diesel         |   5397           0
      VW Rabbit         |   4697           0
      VW Scirocco       |   6850           0
      Volvo 260         |  11995           1
    ----------------------------------------
    
    .

  • #2
    Thanks for the reproducible example. A string variable such as make in the auto dataset is no good as you cannot reference its specific values. You will want to encode such a variable to achieve what you want.

    Code:
    webuse auto, clear
    encode make, gen(Make)
    gen byte flagPrice = price > 10000
    collect clear
    collect style cell price[4010], warn
    table Make,stat(max price flagPrice ) nototal
    levelsof Make if flagPrice, local(highlight)
    collect style cell Make[`highlight']#var[price],font(, color(red))
    collect export myfile, as(docx) replace
    Click image for larger version

Name:	Capture.PNG
Views:	1
Size:	23.0 KB
ID:	1783250

    Last edited by Andrew Musau; 18 Nov 2025, 18:15.

    Comment


    • #3
      The point made in #2 is not correct. You can reference the values of a string variable, but using a numerical variable is cleaner, so to speak. The following works:

      Code:
      webuse auto, clear
      gen byte flagPrice = price > 10000
      collect clear
      collect style cell price[4010], warn
      table make,stat(max price flagPrice ) nototal
      levelsof make if flagPrice, local(highlight)
      collect style cell make[`highlight']#var[price],font(, color(red))
      collect export myfile, as(docx) replace

      Comment


      • #4
        thanks Andrew, in the spirit of simplifying, the original request had an unnecessary command line:

        Code:
        webuse auto, clear
        gen byte flagPrice = price > 10000
        collect clear
        table make,stat(max price flagPrice ) nototal
        levelsof make if flagPrice, local(highlight)
        collect style cell make[`highlight']#var[price],font(, color(red))
        collect export myfile, as(docx) replace
        Last edited by Luis Pecht; 23 Nov 2025, 04:40.

        Comment

        Working...
        X