<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Can we have a bar plot whose x-axis is based on different category variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Can-we-have-a-bar-plot-whose-x-axis-is-based-on-different/m-p/940731#M369216</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/184847"&gt;@TomHsiung&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thanks for the code lines.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;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.&lt;/P&gt;</description>
    <pubDate>Sat, 24 Aug 2024 18:44:02 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2024-08-24T18:44:02Z</dc:date>
    <item>
      <title>Can we have a bar plot whose x-axis is based on different category variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Can-we-have-a-bar-plot-whose-x-axis-is-based-on-different/m-p/940689#M369201</link>
      <description>&lt;P&gt;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&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Source:&amp;nbsp;&lt;A href="https://www.ahajournals.org/doi/epdf/10.1161/JAHA.123.033772" target="_self"&gt;https://www.ahajournals.org/doi/epdf/10.1161/JAHA.123.033772&lt;/A&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Screenshot 2024-08-24 at 12.59.31 PM.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/99606i4AF5119DE9990E19/image-size/large?v=v2&amp;amp;px=999" role="button" title="Screenshot 2024-08-24 at 12.59.31 PM.png" alt="Screenshot 2024-08-24 at 12.59.31 PM.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 24 Aug 2024 05:03:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Can-we-have-a-bar-plot-whose-x-axis-is-based-on-different/m-p/940689#M369201</guid>
      <dc:creator>TomHsiung</dc:creator>
      <dc:date>2024-08-24T05:03:26Z</dc:date>
    </item>
    <item>
      <title>Re: Can we have a bar plot whose x-axis is based on different category variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Can-we-have-a-bar-plot-whose-x-axis-is-based-on-different/m-p/940699#M369205</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
set sashelp.heart;
if ageatstart&amp;lt;35 then flag=1;
  else if ageatstart&amp;lt;45 then flag=2;
    else if ageatstart&amp;lt;55 then flag=3;
	  else flag=4;
run;
proc sgplot data=have;
vbar ageatstart/group=flag;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Ksharp_0-1724485653927.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/99609i18BA43F3AAE0FD67/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Ksharp_0-1724485653927.png" alt="Ksharp_0-1724485653927.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 24 Aug 2024 07:47:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Can-we-have-a-bar-plot-whose-x-axis-is-based-on-different/m-p/940699#M369205</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2024-08-24T07:47:42Z</dc:date>
    </item>
    <item>
      <title>Re: Can we have a bar plot whose x-axis is based on different category variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Can-we-have-a-bar-plot-whose-x-axis-is-based-on-different/m-p/940701#M369206</link>
      <description>&lt;P&gt;One way.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;/* 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;
&lt;/PRE&gt;
&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;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_&amp;lt;variable name&amp;gt;. These will all be character values &lt;STRONG&gt;and&lt;/STRONG&gt; 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.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="SGPlot2.png" style="width: 640px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/99610i89E0553F0C07673D/image-size/large?v=v2&amp;amp;px=999" role="button" title="SGPlot2.png" alt="SGPlot2.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/184847"&gt;@TomHsiung&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;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&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Source:&amp;nbsp;&lt;A href="https://www.ahajournals.org/doi/epdf/10.1161/JAHA.123.033772" target="_self"&gt;https://www.ahajournals.org/doi/epdf/10.1161/JAHA.123.033772&lt;/A&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Screenshot 2024-08-24 at 12.59.31 PM.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/99606i4AF5119DE9990E19/image-size/large?v=v2&amp;amp;px=999" role="button" title="Screenshot 2024-08-24 at 12.59.31 PM.png" alt="Screenshot 2024-08-24 at 12.59.31 PM.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 24 Aug 2024 07:54:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Can-we-have-a-bar-plot-whose-x-axis-is-based-on-different/m-p/940701#M369206</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-08-24T07:54:06Z</dc:date>
    </item>
    <item>
      <title>Re: Can we have a bar plot whose x-axis is based on different category variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Can-we-have-a-bar-plot-whose-x-axis-is-based-on-different/m-p/940705#M369207</link>
      <description>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,</description>
      <pubDate>Sat, 24 Aug 2024 08:29:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Can-we-have-a-bar-plot-whose-x-axis-is-based-on-different/m-p/940705#M369207</guid>
      <dc:creator>TomHsiung</dc:creator>
      <dc:date>2024-08-24T08:29:02Z</dc:date>
    </item>
    <item>
      <title>Re: Can we have a bar plot whose x-axis is based on different category variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Can-we-have-a-bar-plot-whose-x-axis-is-based-on-different/m-p/940706#M369208</link>
      <description>&lt;P&gt;Thanks for the code lines.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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?&lt;/P&gt;</description>
      <pubDate>Sat, 24 Aug 2024 08:42:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Can-we-have-a-bar-plot-whose-x-axis-is-based-on-different/m-p/940706#M369208</guid>
      <dc:creator>TomHsiung</dc:creator>
      <dc:date>2024-08-24T08:42:11Z</dc:date>
    </item>
    <item>
      <title>Re: Can we have a bar plot whose x-axis is based on different category variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Can-we-have-a-bar-plot-whose-x-axis-is-based-on-different/m-p/940731#M369216</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/184847"&gt;@TomHsiung&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thanks for the code lines.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;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.&lt;/P&gt;</description>
      <pubDate>Sat, 24 Aug 2024 18:44:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Can-we-have-a-bar-plot-whose-x-axis-is-based-on-different/m-p/940731#M369216</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-08-24T18:44:02Z</dc:date>
    </item>
    <item>
      <title>Re: Can we have a bar plot whose x-axis is based on different category variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Can-we-have-a-bar-plot-whose-x-axis-is-based-on-different/m-p/940751#M369225</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;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,&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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 &amp;amp; Total cost (mean, 95%CI)';
label xvar = 'Spendings';
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Output:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Unknown-8.png" style="width: 640px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/99614i44A50FE9D4089041/image-size/large?v=v2&amp;amp;px=999" role="button" title="Unknown-8.png" alt="Unknown-8.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 25 Aug 2024 06:07:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Can-we-have-a-bar-plot-whose-x-axis-is-based-on-different/m-p/940751#M369225</guid>
      <dc:creator>TomHsiung</dc:creator>
      <dc:date>2024-08-25T06:07:08Z</dc:date>
    </item>
  </channel>
</rss>

