<?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: Calculating relative frequencies in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Calculating-relative-frequencies/m-p/727889#M226460</link>
    <description>&lt;P&gt;I am quite unsure regarding what you are asking because the relative frequency (as defined by you) is just the % of the total of the year by group, and therefore the stacks will always add up to 100%, anyway below is my try:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	infile datalines;
	input year $ group $ nbr_of_cases;
	datalines;
2015 A 10
2015 B 20
2016 A 15
2016 B 5
2017 A 16
2017 B 30
2017 C 50
2018 B 13
2018 C 5
;
run;

proc means data=want noprint nway;
	class year;
	var nbr_of_cases;
	output out=have_sum (drop=_TYPE_ _FREQ_) sum= / autoname;
run;

data want;
	merge have have_sum;
	by year;
	format rel_freq 8.2;
	rel_freq = divide(nbr_of_cases,nbr_of_cases_sum);
	drop nbr_of_cases nbr_of_cases_sum;
run;

proc sgplot data=want;
	vbar year / response=rel_freq group=group;
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 20 Mar 2021 03:07:08 GMT</pubDate>
    <dc:creator>qoit</dc:creator>
    <dc:date>2021-03-20T03:07:08Z</dc:date>
    <item>
      <title>Calculating relative frequencies</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-relative-frequencies/m-p/727869#M226448</link>
      <description>&lt;P&gt;Dear all,&lt;/P&gt;
&lt;P&gt;I need to plot a histogram for a sample dataset like this using sgplot (rel. frequencies per year for each group):&lt;/P&gt;
&lt;TABLE style="height: 250px; width: 350px;" width="350"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD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&lt;TR&gt;
&lt;TD&gt;2018&lt;/TD&gt;
&lt;TD&gt;B&lt;/TD&gt;
&lt;TD&gt;13&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;2018&lt;/TD&gt;
&lt;TD&gt;C&lt;/TD&gt;
&lt;TD&gt;5&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;my first question is that can I use proc sql or proc means to calculate the relative frequencies per year? If yes, how?&lt;/P&gt;
&lt;P&gt;If I use proc freq, it calculates the sum over all years but I need the rel. frequencies for group per year. For example for 2015, the rel. frequency should be 10/30*100 for group A and 20/30*100 for group B&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;the sample plot will look like this: in my case this will be group A, B&amp;nbsp; and C&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Anita_n_1-1616189849095.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/56185iEE3AAAFFB9DC4FAA/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Anita_n_1-1616189849095.png" alt="Anita_n_1-1616189849095.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 19 Mar 2021 21:40:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-relative-frequencies/m-p/727869#M226448</guid>
      <dc:creator>Anita_n</dc:creator>
      <dc:date>2021-03-19T21:40:19Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating relative frequencies</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-relative-frequencies/m-p/727889#M226460</link>
      <description>&lt;P&gt;I am quite unsure regarding what you are asking because the relative frequency (as defined by you) is just the % of the total of the year by group, and therefore the stacks will always add up to 100%, anyway below is my try:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	infile datalines;
	input year $ group $ nbr_of_cases;
	datalines;
2015 A 10
2015 B 20
2016 A 15
2016 B 5
2017 A 16
2017 B 30
2017 C 50
2018 B 13
2018 C 5
;
run;

proc means data=want noprint nway;
	class year;
	var nbr_of_cases;
	output out=have_sum (drop=_TYPE_ _FREQ_) sum= / autoname;
run;

data want;
	merge have have_sum;
	by year;
	format rel_freq 8.2;
	rel_freq = divide(nbr_of_cases,nbr_of_cases_sum);
	drop nbr_of_cases nbr_of_cases_sum;
run;

proc sgplot data=want;
	vbar year / response=rel_freq group=group;
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 20 Mar 2021 03:07:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-relative-frequencies/m-p/727889#M226460</guid>
      <dc:creator>qoit</dc:creator>
      <dc:date>2021-03-20T03:07:08Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating relative frequencies</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-relative-frequencies/m-p/727896#M226463</link>
      <description>yes, this is exactly what I wanted, thankyou for the help. Just a little correction in your code. It should be proc means data =have.</description>
      <pubDate>Sat, 20 Mar 2021 07:54:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-relative-frequencies/m-p/727896#M226463</guid>
      <dc:creator>Anita_n</dc:creator>
      <dc:date>2021-03-20T07:54:50Z</dc:date>
    </item>
  </channel>
</rss>

