BookmarkSubscribeRSS Feed
TomHsiung
Pyrite | Level 9

Hello, everyone Today I read a paper. The bar plot is nice and it differs from other bar plots in that the x-axis is based on several category variables. The authors use R to make this bar plot and I wonder if we can do the same in SAS Viya? Thank you

 

Source: https://www.ahajournals.org/doi/epdf/10.1161/JAHA.123.033772 

 

Screenshot 2024-08-24 at 12.59.31 PM.png

6 REPLIES 6
Ksharp
Super User
data have;
set sashelp.heart;
if ageatstart<35 then flag=1;
  else if ageatstart<45 then flag=2;
    else if ageatstart<55 then flag=3;
	  else flag=4;
run;
proc sgplot data=have;
vbar ageatstart/group=flag;
run;

Ksharp_0-1724485653927.png

 

TomHsiung
Pyrite | Level 9
I see. But things are a little different. Please notice that the x-axis uses several different category systems (e.g., gender, age, Hypertension). Hmmm,
ballardw
Super User

One way.

 

/* get a summary data set*/
Proc freq data=sashelp.class;
ods output onewayfreqs=work.myfreqs;
tables sex age ;
run;

/* get a single variable to use for x axis*/
/* and values for a grouping variable */

data work.toplot;
   set work.myfreqs;
   length xvar groupvar $ 10;
   xvar= cats(F_sex,F_age);
   groupvar = scan(table,2);
run;

proc sgplot data=work.toplot;
   vbarparm category=xvar  response=frequency / group=groupvar;
   yaxis label='Number of Students';
   label Groupvar='Subgroups';
run;

The Sgplot is the actual plot code and could be considered the "answer" but I provide how to create the values from a data set that will work with the code.

 

The Work.Toplot demonstrates how the data would need to look. Order with this example is "nice". Sorting and data order options may be needed depending on YOUR plot data. For the appearance desired the key is going to be a single variable holding all of the xaxis values and another for the grouping variable.

The Proc Freq is just one way to get summaries of multiple categories. Note that the way ODS OUTPUT for Onewayfreqs creates a formatted value of the variable, named F_<variable name>. These will all be character values and will reflect the format applied to the variable at the time Proc Freq executes. So if a format had created categories of "Pre-teen" and "Teen" based on the numeric values of the variable Age that is what you would see in the F_Age variable.

 

SGPlot2.png

 


@TomHsiung wrote:

Hello, everyone Today I read a paper. The bar plot is nice and it differs from other bar plots in that the x-axis is based on several category variables. The authors use R to make this bar plot and I wonder if we can do the same in SAS Viya? Thank you

 

Source: https://www.ahajournals.org/doi/epdf/10.1161/JAHA.123.033772 

 

Screenshot 2024-08-24 at 12.59.31 PM.png


 

TomHsiung
Pyrite | Level 9

Thanks for the code lines.

 

Hmmm, so the xvar variable contains two category systems - by sex and by age. There must be repeated rows in the dataset, I think, that is the rows whose xvar variable has either F or M values, in addition to the same rows but instead whose xvar values are those age categories. Correct?

ballardw
Super User

@TomHsiung wrote:

Thanks for the code lines.

 

Hmmm, so the xvar variable contains two category systems - by sex and by age. There must be repeated rows in the dataset, I think, that is the rows whose xvar variable has either F or M values, in addition to the same rows but instead whose xvar values are those age categories. Correct?


The example I created has ONE observation for EACH value of the x axis variable. That is how VBARPARM works. I also chose VBARPARM because the ways that VBAR creates bars might have some issues with different numbers of observations for each x axis variable. Especially if you want percents and not frequency.

TomHsiung
Pyrite | Level 9

@ballardw Thank you for the clarification. And based on your codes, i.e. the cats(F_sex, F_age) part. I developed my own code lines. Below,

proc sql noprint;
create table food_2 as
select *,
cats(Category) as xvar,
'By category' as Group
from food_raw
union all
select *,
case
when Day lt 10 then '1 Early'
when Day ge 10 and Day lt 20 then '2 Middle'
when Day ge 20 then '3 Late'
end as xvar,
'By date' as Group
from food_raw;
quit;

proc sgpanel data = food_2;
title 'Quarterly total cost';
panelby quarter;
vbar xvar / groupdisplay = cluster group = Group response = Cost stat = sum transparency = 0.00;
label Category = 'Spending category';
label Cost = 'Total cost & Total cost (mean, 95%CI)';
label xvar = 'Spendings';
run;

Output:

Unknown-8.png

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 491 views
  • 0 likes
  • 3 in conversation