BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
joachimg
Obsidian | Level 7

I want to make a plot similar to this one, but I really have no idea how. I have four variable I want to include - sex, race, educ, and diet, and I want a variable called homoc on the y axis. Any ideas? Thanks! Screen Shot 2021-10-07 at 4.59.43 PM.png

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data toplot;
  set sashelp.class;
  length vn $ 10;
  vn='Sex'; category=put(sex,$5.); output;
  vn='Age'; category=put(age,f5.); output;
run;
proc sgpanel data=toplot noautolegend;
panelby vn/layout=columnlattice onepanel uniscale=row noheader;
vbox weight/category=category group=vn;
rowaxis display=(nolabel);
colaxis display=(nolabel);
run;

x.png

View solution in original post

4 REPLIES 4
ballardw
Super User

One way involves reshaping the data bit. Here is an example using a data set you should have that will allow you to run the code.

data toplot;
  set sashelp.class;
  length vn $ 10;
  vn='Sex'; category=put(sex,$5.); output;
  vn='Age'; category=put(age,f5.); output;
run;

proc sgplot data=toplot noautolegend;
   vbox weight /group=vn category=category;
   xaxis label=' ';

run;

The data step makes a duplicate of your analysis variable(s), in this case we could use either or both of the Height and Weight variables. We add a variable for the Group role using the name of the variable. This sets the colors for related values the same. We also add a variable for the Category role, which means each value will get a separate box created. I made sure the text created has the same length to avoid possible problems. The Output statement if you haven't used it, tells the program to write to the data set when encountered. This is where the values for each analysis variable get duplicated.

 

The Sgplot uses the created data set. Since your example doesn't show a key for the groups (Probably because your text is fairly explanatory) is suppress the automatic legend that would have the values of the Group variable as key to the colors. The Xaxis statement also suppresses any axis label. The vertical axis label will by default be the label associated with your analysis variable.

 

A REFLINE statement can add a reference line. I'm not sure if you actually wanted one, or where and I don't have your data to make any guess.  A simple:  REFLINE 50; would place a horizontal line across the graph at the Y axis value of 50. There are options to set line types and labels if desired.

joachimg
Obsidian | Level 7
Thank you so much! This worked as well. Really appreciate the help.
Ksharp
Super User
data toplot;
  set sashelp.class;
  length vn $ 10;
  vn='Sex'; category=put(sex,$5.); output;
  vn='Age'; category=put(age,f5.); output;
run;
proc sgpanel data=toplot noautolegend;
panelby vn/layout=columnlattice onepanel uniscale=row noheader;
vbox weight/category=category group=vn;
rowaxis display=(nolabel);
colaxis display=(nolabel);
run;

x.png

joachimg
Obsidian | Level 7
Thank you so much!

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 4 replies
  • 3481 views
  • 3 likes
  • 3 in conversation