I am using the following code to generate the box plot. However My carmodel with Red color have only one record. I want to display the patterns to every Blue and Green colors, but not for the 'Red' color group . Is there any way I can control it? thank you in advance.
/* Create dataset with 20 cars - Volvo/Fiat/Land Rover forced to Red */
data car_performance;
length CarModel $20 Color $10;
/* Define car models */
array car_models[20] $20 _temporary_ (
"Toyota Camry", "Honda Accord", "Ford Mustang", "Chevrolet Corvette",
"BMW M3", "Tesla Model S", "Audi A4", "Subaru Outback",
"Jeep Wrangler", "Porsche 911", "Lexus RX", "Hyundai Sonata",
"Volkswagen Golf", "Mazda CX-5", "Mercedes C-Class", "Nissan Altima",
"Kia Telluride", "Volvo XC90", "Land Rover Defender", "Fiat 500"
);
/* Define colors (Green replaces Silver) */
array colors[2] $10 _temporary_ ("Blue", "Green");
/* Generate data */
do i = 1 to 20;
CarModel = car_models[i];
/* Force Volvo, Fiat, Land Rover to Red */
if CarModel in ("Volvo XC90", "Fiat 500", "Land Rover Defender") then
Color = "Red";
/* Distribute others evenly between Blue/Green */
else
Color = colors[mod(i, 2) + 1];
/* Random MPG values (color-specific ranges) */
if Color = "Red" then
MPG = round(18 + 12 * ranuni(0)); /* Red: 18-30 MPG */
else if Color = "Blue" then
MPG = round(22 + 10 * ranuni(0)); /* Blue: 22-32 MPG */
else
MPG = round(25 + 8 * ranuni(0)); /* Green: 25-33 MPG */
output;
end;
drop i;
run;
data xx;
set car_performance;
if CARMODEL in ('Volvo XC90' 'Land Rover Defender') and color = 'Red' then delete;
run;
options orientation = landscape errors = 2 missing = ' ' nofmterr ls = 175 validvarname = upcase nobyline;
ods graphics on / attrpriority=none reset=all width=9.0in height=4.6in border=off;
ods escapechar = '^';
ods results on;
ods listing close;
ods rtf file = "C:\temp\test.rtf" ;
proc sgplot data=xx;
title "Car MPG Distribution by Color";
vbox MPG / category=Color
dataskin=none
fillpattern
lineattrs=(thickness=2);
/* Customize colors in the plot */
styleattrs datafillpatterns=(X1);
xaxis label="Car Color";
yaxis label="Miles Per Gallon (MPG)" grid;
run;
ods rtf close;
And Answer is yes. but you need to polt this BOX graphc twice .
options orientation = landscape errors = 2 missing = ' ' nofmterr ls = 175 validvarname = upcase nobyline;
ods graphics on / attrpriority=none reset=all width=9.0in height=4.6in border=off outputfmt=png;
ods escapechar = '^';
ods results on;
ods listing close;
ods rtf file = "C:\temp\test.rtf" ;
data xx1;
set xx;
if COLOR ne 'Red' then MPG1=MPG;
run;
proc sgplot data=xx1 noautolegend;
title "Car MPG Distribution by Color";
vbox MPG / category=Color
dataskin=none fillattrs=(color=lightblue)
lineattrs=(thickness=2);
vbox MPG1 / category=Color
dataskin=none
fillpattern nofill
lineattrs=(thickness=2 );
/* Customize colors in the plot */
styleattrs datafillpatterns=(X1) datacolors=(black) datacontrastcolors=(black);
xaxis label="Car Color";
yaxis label="Miles Per Gallon (MPG)" grid;
run;
ods rtf close;
options orientation = landscape errors = 2 missing = ' ' nofmterr ls = 175 validvarname = upcase nobyline;
ods graphics on / attrpriority=none reset=all width=9.0in height=4.6in border=off outputfmt=png;
ods escapechar = '^';
ods results on;
ods listing close;
ods rtf file = "C:\temp\test.rtf" ;
proc sgplot data=xx;
title "Car MPG Distribution by Color";
vbox MPG / category=Color
dataskin=none
fillpattern
lineattrs=(thickness=2);
/* Customize colors in the plot */
styleattrs datafillpatterns=(X1);
xaxis label="Car Color";
yaxis label="Miles Per Gallon (MPG)" grid;
run;
ods rtf close;
And Answer is yes. but you need to polt this BOX graphc twice .
options orientation = landscape errors = 2 missing = ' ' nofmterr ls = 175 validvarname = upcase nobyline;
ods graphics on / attrpriority=none reset=all width=9.0in height=4.6in border=off outputfmt=png;
ods escapechar = '^';
ods results on;
ods listing close;
ods rtf file = "C:\temp\test.rtf" ;
data xx1;
set xx;
if COLOR ne 'Red' then MPG1=MPG;
run;
proc sgplot data=xx1 noautolegend;
title "Car MPG Distribution by Color";
vbox MPG / category=Color
dataskin=none fillattrs=(color=lightblue)
lineattrs=(thickness=2);
vbox MPG1 / category=Color
dataskin=none
fillpattern nofill
lineattrs=(thickness=2 );
/* Customize colors in the plot */
styleattrs datafillpatterns=(X1) datacolors=(black) datacontrastcolors=(black);
xaxis label="Car Color";
yaxis label="Miles Per Gallon (MPG)" grid;
run;
ods rtf close;
Thank you.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.