<?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: calculate frequencies in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/calculate-frequencies/m-p/827348#M326807</link>
    <description>&lt;P&gt;Thanks PaigeMiller for your reply.&lt;/P&gt;
&lt;P&gt;The data has variables brain heart liver stomack &lt;STRONG&gt;llung lkidney lovary rlung rkidney rovary ucolon lcolon&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;llung, rlung&amp;nbsp;&lt;/STRONG&gt;need to be put as lung, same for lkidney, rkidney, etc.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How do do that before calculating the frequencies?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;Maggie&lt;/P&gt;</description>
    <pubDate>Fri, 05 Aug 2022 13:07:23 GMT</pubDate>
    <dc:creator>urban58</dc:creator>
    <dc:date>2022-08-05T13:07:23Z</dc:date>
    <item>
      <title>calculate frequencies</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-frequencies/m-p/827340#M326801</link>
      <description>&lt;P&gt;Hello SAS community,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a large dataset with lots of variables - see a sample below&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Data have;&lt;BR /&gt;input id brain heart liver stomack llung lkidney lovary rlung rkidney rovary ucolon lcolon; &lt;BR /&gt;cards&lt;BR /&gt;;&lt;BR /&gt;1 1 0 1 0 1 0 0 0 1 0 1 1&lt;BR /&gt;2 0 0 0 1 0 1 0 0 0 1 0 1 &lt;BR /&gt;3 0 1 0 0 1 0 1 1 1 0 0 0 &lt;BR /&gt;4 0 0 0 0 0 1 0 0 0 0 0 1 &lt;BR /&gt;5 1 1 1 1 1 1 1 1 1 1 1 1 &lt;BR /&gt;6 0 0 1 0 0 0 0 0 1 0 1 1 &lt;BR /&gt;7 0 0 1 1 0 0 0 0 0 0 0 0 &lt;BR /&gt;8 1 1 1 1 0 0 0 1 1 1 0 1 &lt;BR /&gt;9 0 1 0 1 0 0 0 0 0 0 0 0 &lt;BR /&gt;10 0 0 0 0 1 1 1 1 0 1 1 0 &lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;desired output&lt;BR /&gt;&lt;STRONG&gt;Body part&amp;nbsp; &amp;nbsp;% with&amp;nbsp;XXX=1&lt;/STRONG&gt;&lt;BR /&gt;Brain&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;BR /&gt;Heart&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;BR /&gt;Liver &lt;BR /&gt;Stomack &lt;BR /&gt;Lung&lt;BR /&gt;Kidney&lt;BR /&gt;Ovary&lt;BR /&gt;Colon&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Appreciate any help!&lt;/P&gt;
&lt;P&gt;Maggie&lt;/P&gt;</description>
      <pubDate>Fri, 05 Aug 2022 12:46:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-frequencies/m-p/827340#M326801</guid>
      <dc:creator>urban58</dc:creator>
      <dc:date>2022-08-05T12:46:11Z</dc:date>
    </item>
    <item>
      <title>Re: calculate frequencies</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-frequencies/m-p/827342#M326803</link>
      <description>&lt;P&gt;Do you want this as a report or a SAS data set?&lt;/P&gt;</description>
      <pubDate>Fri, 05 Aug 2022 12:51:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-frequencies/m-p/827342#M326803</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2022-08-05T12:51:02Z</dc:date>
    </item>
    <item>
      <title>Re: calculate frequencies</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-frequencies/m-p/827343#M326804</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=have;
    var brain -- lcolon;
    output out=want mean=;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You can transpose it if you want the information displayed vertically.&lt;/P&gt;</description>
      <pubDate>Fri, 05 Aug 2022 12:56:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-frequencies/m-p/827343#M326804</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-08-05T12:56:25Z</dc:date>
    </item>
    <item>
      <title>Re: calculate frequencies</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-frequencies/m-p/827348#M326807</link>
      <description>&lt;P&gt;Thanks PaigeMiller for your reply.&lt;/P&gt;
&lt;P&gt;The data has variables brain heart liver stomack &lt;STRONG&gt;llung lkidney lovary rlung rkidney rovary ucolon lcolon&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;llung, rlung&amp;nbsp;&lt;/STRONG&gt;need to be put as lung, same for lkidney, rkidney, etc.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How do do that before calculating the frequencies?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;Maggie&lt;/P&gt;</description>
      <pubDate>Fri, 05 Aug 2022 13:07:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-frequencies/m-p/827348#M326807</guid>
      <dc:creator>urban58</dc:creator>
      <dc:date>2022-08-05T13:07:23Z</dc:date>
    </item>
    <item>
      <title>Re: calculate frequencies</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-frequencies/m-p/827375#M326821</link>
      <description>&lt;P&gt;Do you expect the totals for variables like Lkidney and Rkidney as a single value?&lt;/P&gt;
&lt;P&gt;You really aren't clear whether you need a data set or report. I am providing a report done one of the many possible:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;Data have;
input id brain heart liver stomack llung lkidney lovary rlung rkidney rovary ucolon lcolon;
cards
;
1 1 0 1 0 1 0 0 0 1 0 1 1
2 0 0 0 1 0 1 0 0 0 1 0 1
3 0 1 0 0 1 0 1 1 1 0 0 0
4 0 0 0 0 0 1 0 0 0 0 0 1
5 1 1 1 1 1 1 1 1 1 1 1 1
6 0 0 1 0 0 0 0 0 1 0 1 1
7 0 0 1 1 0 0 0 0 0 0 0 0
8 1 1 1 1 0 0 0 1 1 1 0 1
9 0 1 0 1 0 0 0 0 0 0 0 0
10 0 0 0 0 1 1 1 1 0 1 1 0
;
run;

/* assumes data have is sorted by ID
AND that there are no duplicate values of ID
*/
proc transpose data=have out=trans;
   by id;
run;

proc format;
value $bodyparts
'llung','rlung'    ='Lung'
'lkidney','rkidney'='Kidney'
'lovary','rovary'  ='Ovary'
'brain'            ='Brain'             
'heart'            ='Heart'            
'liver'            ='Liver'
'stomack'          ='Stomack'
'ucolon','lcolon'  ='Colon'
;
run;

Proc tabulate data=trans;
   class _name_;
   format _name_ $bodyparts.;
   var col1;
   table _name_=' ',
         col1=' '*(Sum='Count'*f=best6. mean='%'*f=percent8.1)
         ;
run;&lt;/PRE&gt;
&lt;P&gt;Caution: The format assumes the current names of the variables are exactly as you have provided them here (stomach is the more typical English spelling if I recall correctly) and including the case. The defaults of the Proc Transpose create a variable named "_name_" containing the value of the name. You may need to double check spelling, upper and lower case included, to make the format text on the left side of each = to match your data. The default will also return the numeric value in Col1. These names can be changed but for an example like this should suffice.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Formats assigning common values to multiples or groups of values create groups that will be honored by most of the SAS report, analysis and graphing procedures. So I do not change any actual values of the _name_ but use the custom format to create the groups (and provide capitalization of the result).&lt;/P&gt;
&lt;P&gt;When you have variables that take a value of 1/0 then the SUM statistic gives you the count of 1's and the Mean is a decimal percentage OF the observations with values. Missing values will be excluded for both the Sum and Mean calculations. So if you have 10 observations and one of the bodypart variable value is missing then that part will only have 9 values. If you expect a percentage based on 10 values then you have more work involved defining what the numerator and denominator for the rate should be.&lt;/P&gt;</description>
      <pubDate>Fri, 05 Aug 2022 14:58:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-frequencies/m-p/827375#M326821</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-08-05T14:58:13Z</dc:date>
    </item>
    <item>
      <title>Re: calculate frequencies</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-frequencies/m-p/827413#M326835</link>
      <description>&lt;P&gt;Thank you ballardw, I will use this code on my data and ask if I don't understand something or accept as the solution your response.&lt;/P&gt;
&lt;P&gt;Maggie&lt;/P&gt;</description>
      <pubDate>Fri, 05 Aug 2022 18:04:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-frequencies/m-p/827413#M326835</guid>
      <dc:creator>urban58</dc:creator>
      <dc:date>2022-08-05T18:04:09Z</dc:date>
    </item>
    <item>
      <title>Re: calculate frequencies</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-frequencies/m-p/827463#M326864</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/322157"&gt;@urban58&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The data has variables brain heart liver stomack &lt;STRONG&gt;llung lkidney lovary rlung rkidney rovary ucolon lcolon&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;llung, rlung&amp;nbsp;&lt;/STRONG&gt;need to be put as lung, same for lkidney, rkidney, etc.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;This detail was not mentioned originally. Could you please re-write the problem to include this information and all other relevant information (including other relevant points you have not yet mentioned), so the entire problem is in one problem statement? Please include in your explanation what happens if llung and rlung are both 1, is the frequency = 1 or is the frequency = 2?&lt;/P&gt;</description>
      <pubDate>Sat, 06 Aug 2022 13:23:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-frequencies/m-p/827463#M326864</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-08-06T13:23:20Z</dc:date>
    </item>
    <item>
      <title>Re: calculate frequencies</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-frequencies/m-p/827580#M326919</link>
      <description>&lt;P&gt;a report would be great&lt;/P&gt;</description>
      <pubDate>Mon, 08 Aug 2022 11:32:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-frequencies/m-p/827580#M326919</guid>
      <dc:creator>urban58</dc:creator>
      <dc:date>2022-08-08T11:32:21Z</dc:date>
    </item>
    <item>
      <title>Re: calculate frequencies</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-frequencies/m-p/827658#M326935</link>
      <description>&lt;P&gt;your code is just great ballardw, it answered my question exactly - sorry for the late response. Is there a way I can display the %s so the largest is 1st, etc.&lt;/P&gt;
&lt;P&gt;Maggie&lt;/P&gt;</description>
      <pubDate>Mon, 08 Aug 2022 17:40:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-frequencies/m-p/827658#M326935</guid>
      <dc:creator>urban58</dc:creator>
      <dc:date>2022-08-08T17:40:29Z</dc:date>
    </item>
    <item>
      <title>Re: calculate frequencies</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-frequencies/m-p/827862#M327002</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/322157"&gt;@urban58&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;your code is just great ballardw, it answered my question exactly - sorry for the late response. Is there a way I can display the %s so the largest is 1st, etc.&lt;/P&gt;
&lt;P&gt;Maggie&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;One way is to calculate the numbers needed and sort.&lt;/P&gt;
&lt;PRE&gt;proc summary data=trans nway;
   class _name_;
   format _name_ $bodyparts.;
   var col1;
   output out=summary sum= mean= /autoname;
run;
proc sort data=summary;
   by descending col1_mean;
run;

proc print data=summary noobs label;
   var _name_ col1_sum col1_mean;
   format col1_mean percent8.1;
   label
      col1_sum='Count'
      col1_mean='%'
   ;
run;&lt;/PRE&gt;
&lt;P&gt;Caution: this summary data set only has one value of each of the two part elements and you can't always be sure which one it should be.&lt;/P&gt;
&lt;P&gt;Proc summary (or Means) will only keep one of the values of the Class variable(s). The format still applies though. The autoname options on the requested statistics appends the _ and statistic to the variable (assumes the result is less than 32 characters, longer variables will get truncated to fit 32 characters).&lt;/P&gt;
&lt;P&gt;Note that by default you will get two additional variables in the result of Proc Summary: _type_ , which indicates which combination(s) of class variables are present, the NWAY option means only the largest type is in the result, and _freq_ which is a raw count of the number of values used in the summary for each row of output.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Proc Tabulate does not have a "sort order by statistic" option so a different approach is needed.&lt;/P&gt;</description>
      <pubDate>Tue, 09 Aug 2022 14:04:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-frequencies/m-p/827862#M327002</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-08-09T14:04:58Z</dc:date>
    </item>
    <item>
      <title>Re: calculate frequencies</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-frequencies/m-p/827892#M327022</link>
      <description>Thanks for all your help with the code and the caution explanation - much appreciated!</description>
      <pubDate>Tue, 09 Aug 2022 15:36:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-frequencies/m-p/827892#M327022</guid>
      <dc:creator>urban58</dc:creator>
      <dc:date>2022-08-09T15:36:53Z</dc:date>
    </item>
  </channel>
</rss>

