<?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: proc freq and percent in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/proc-freq-and-percent/m-p/611703#M178332</link>
    <description>&lt;PRE&gt;total_count / &amp;amp;tc. as percent format=percent7.2,&lt;/PRE&gt;
&lt;P&gt;I always point out that this calculation fails to produce the proper answer if there are missing values in the data.&lt;/P&gt;</description>
    <pubDate>Fri, 13 Dec 2019 19:13:28 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2019-12-13T19:13:28Z</dc:date>
    <item>
      <title>proc freq and percent</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-freq-and-percent/m-p/611697#M178327</link>
      <description>&lt;P&gt;I have the following dataset. I would like to extract first and last date per city and doing a count by city as well&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Have:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;City&lt;/TD&gt;&lt;TD&gt;dates&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;New York&lt;/TD&gt;&lt;TD&gt;1/5/2019&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Boston&lt;/TD&gt;&lt;TD&gt;4/20/2019&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Washington DC&lt;/TD&gt;&lt;TD&gt;8/20/2019&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Washington DC&lt;/TD&gt;&lt;TD&gt;9/1/2019&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Washington DC&lt;/TD&gt;&lt;TD&gt;10/19/2019&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Washington DC&lt;/TD&gt;&lt;TD&gt;12/13/2019&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Hartford&lt;/TD&gt;&lt;TD&gt;5/5/2019&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Pittsburgh&lt;/TD&gt;&lt;TD&gt;6/4/2019&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Philadelphia&lt;/TD&gt;&lt;TD&gt;4/6/2019&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Philadelphia&lt;/TD&gt;&lt;TD&gt;6/14/2019&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Philadelphia&lt;/TD&gt;&lt;TD&gt;9/8/2019&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Want&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;City&lt;/TD&gt;&lt;TD&gt;Count&lt;/TD&gt;&lt;TD&gt;firstDate&lt;/TD&gt;&lt;TD&gt;LastDate&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;New York&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1/5/2019&lt;/TD&gt;&lt;TD&gt;1/5/2019&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Boston&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;4/20/2019&lt;/TD&gt;&lt;TD&gt;4/20/2019&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Washington DC&lt;/TD&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;8/20/2019&lt;/TD&gt;&lt;TD&gt;12/13/2019&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Hartford&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;5/5/2019&lt;/TD&gt;&lt;TD&gt;5/5/2019&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Pittsburgh&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;6/4/2019&lt;/TD&gt;&lt;TD&gt;6/4/2019&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Philadelphia&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;4/6/2019&lt;/TD&gt;&lt;TD&gt;9/8/2019&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Possibly also percent per city? like NY will have 1/11, Boston 1/11 (9.1%), etc&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is waht i have done so far&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;select distinct city, count(*) as total_count, min(dates) as firstDate, max(dates) as lastDate&lt;/P&gt;&lt;P&gt;from table group by city;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However when I run it, all the dates are converted into NUMBERS, not dates. How do I add n/total % ?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;THanks!!&lt;/P&gt;</description>
      <pubDate>Fri, 13 Dec 2019 18:59:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-freq-and-percent/m-p/611697#M178327</guid>
      <dc:creator>radhikaa4</dc:creator>
      <dc:date>2019-12-13T18:59:49Z</dc:date>
    </item>
    <item>
      <title>Re: proc freq and percent</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-freq-and-percent/m-p/611702#M178331</link>
      <description>&lt;P&gt;Add formats to display dates correctly, and use a preliminary step to get the overall count.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
select nobs into :tc
from dictionary.tables
where libname = "WORK" and memname = "TABLE";
select
  city,
  count(*) as total_count,
  calculated total_count / &amp;amp;tc. as percent format=percent7.2,
  min(dates) as firstDate format=e8601da10.,
  max(dates) as lastDate format=e8601da10.
from table
group by city;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="1 2 3 4 5 6 7"&gt;&lt;EM&gt;Edit: added "calculated" in the SQL to prevent ERROR&lt;/EM&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 15 Dec 2019 10:48:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-freq-and-percent/m-p/611702#M178331</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-12-15T10:48:20Z</dc:date>
    </item>
    <item>
      <title>Re: proc freq and percent</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-freq-and-percent/m-p/611703#M178332</link>
      <description>&lt;PRE&gt;total_count / &amp;amp;tc. as percent format=percent7.2,&lt;/PRE&gt;
&lt;P&gt;I always point out that this calculation fails to produce the proper answer if there are missing values in the data.&lt;/P&gt;</description>
      <pubDate>Fri, 13 Dec 2019 19:13:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-freq-and-percent/m-p/611703#M178332</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-12-13T19:13:28Z</dc:date>
    </item>
    <item>
      <title>Re: proc freq and percent</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-freq-and-percent/m-p/611705#M178334</link>
      <description>&lt;P&gt;You have asked several very similar questions. Please tell us if you want a data set (for further manipulation or analysis)&amp;nbsp;or a report that people read.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Most of your questions if a report is needed can be answered by one of the reporting procedures like Proc Report or Proc tabulate:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data have;
infile datalines dlm=',';
input City :$25. dates mmddyy10.;
format dates mmddyy10.;
datalines;
New York,1/5/2019 
Boston,4/20/2019 
Washington DC,8/20/2019 
Washington DC,9/1/2019 
Washington DC,10/19/2019 
Washington DC,12/13/2019 
Hartford,5/5/2019 
Pittsburgh,6/4/2019 
Philadelphia,4/6/2019 
Philadelphia,6/14/2019 
Philadelphia,9/8/2019 
;

proc tabulate data=have;
   class city;
   var dates;
   tables city ,
          dates=' '*(n='Count' colpctn='%' min='First Date'*f=mmddyy10. max='Last Date'*f=mmddyy10.)
  ;
run;
&lt;/PRE&gt;
&lt;P&gt;If you do not specify a specific date format for a calculated result then it will default to BEST. Since SAS date values are the number of days from 1 Jan 1960 then unformatted values look like 21340 instead of a formatted date like 06/05/2018 or 05Jun2018.&lt;/P&gt;</description>
      <pubDate>Fri, 13 Dec 2019 19:21:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-freq-and-percent/m-p/611705#M178334</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-12-13T19:21:53Z</dc:date>
    </item>
    <item>
      <title>Re: proc freq and percent</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-freq-and-percent/m-p/611735#M178355</link>
      <description>&lt;P&gt;PROC SUMMARY has features that make this easy even when the dates are character in iso8601 format.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
   infile cards dsd;
   input City :$25. dates mmddyy10.;
   format dates yymmdd10.;
   dtc = put(dates,yymmdd10.);
   cards;
New York,1/5/2019 
Boston,4/20/2019 
Washington DC,8/20/2019 
Washington DC,9/1/2019 
Washington DC,10/19/2019 
Washington DC,12/13/2019 
Hartford,5/5/2019 
Pittsburgh,6/4/2019 
Philadelphia,4/6/2019 
Philadelphia,6/14/2019 
Philadelphia,9/8/2019 
;;;;
proc summary data=have nway missing;
   class city;
   output out=need(drop=_type_ rename=(_freq_=Count))
      idgroup(min(dtc) out(dtc)=Start)
      idgroup(max(dtc) out(dtc)=End)
      ;
   run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image.png" style="width: 340px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/34765i037CF3072A1AB1E2/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 13 Dec 2019 21:24:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-freq-and-percent/m-p/611735#M178355</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2019-12-13T21:24:28Z</dc:date>
    </item>
    <item>
      <title>Re: proc freq and percent</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-freq-and-percent/m-p/611862#M178440</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;PRE&gt;total_count / &amp;amp;tc. as percent format=percent7.2,&lt;/PRE&gt;
&lt;P&gt;I always point out that this calculation fails to produce the proper answer if there are missing values in the data.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Why?&lt;/P&gt;
&lt;P&gt;Missing values would just end up as another group, with a correct count. The user can then decide to filter them out, or not.&lt;/P&gt;
&lt;P&gt;See this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
select nobs into :tc
from dictionary.tables
where libname = "WORK" and memname = "CLASS";
create table want as
select
  sex,
  count(*) as total_count,
  calculated total_count / &amp;amp;tc. as percent format=percent7.2
from class
group by sex;
quit;

proc print data=want noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;Sex	total_count	percent
 	1	5.00%
F	9	45.0%
M	10	50.0%
&lt;/PRE&gt;</description>
      <pubDate>Sun, 15 Dec 2019 10:47:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-freq-and-percent/m-p/611862#M178440</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-12-15T10:47:17Z</dc:date>
    </item>
    <item>
      <title>Re: proc freq and percent</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-freq-and-percent/m-p/611867#M178441</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/153469"&gt;@radhikaa4&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can try this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
	create table want as
	select a.City,
   	 	   a.Count,
   	 	   a.Count/b.total as Percent format=Percent8.1,
   	 	   a.firstDate format=MMDDYY10.,
   	 	   a.LastDate  format=MMDDYY10.
	from (select City,
		  		 count(City) as Count,
		  		 min(Dates) as FirstDate,
		  		 max(Dates) as LastDate
		  from table
		  group by City) as a full join 
		 (select distinct City, count(*) as total from table) as b
	on a.City = b.City
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You can apply a format (e.g. MMDDYY10.) directly after the calculation so that dates can be human-readable : e.g. min(Dates) as FirstDate &lt;FONT color="#FF00FF"&gt;format = MMDDYY10.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;To&amp;nbsp;compute percent, I have merged you first query (alias 'a') with another another one (alias 'b') that retrieve the total number of records.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-left" image-alt="Alias &amp;quot;a&amp;quot;" style="width: 200px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/34781i8DF85D5DB52ABA63/image-size/small?v=v2&amp;amp;px=200" role="button" title="Capture d’écran 2019-12-15 à 12.31.43.png" alt="Alias &amp;quot;a&amp;quot;" /&gt;&lt;span class="lia-inline-image-caption" onclick="event.preventDefault();"&gt;Alias "a"&lt;/span&gt;&lt;/span&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-left" image-alt="Alias &amp;quot;b&amp;quot;" style="width: 127px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/34782i1D12E81366029526/image-size/small?v=v2&amp;amp;px=200" role="button" title="Capture d’écran 2019-12-15 à 12.31.48.png" alt="Alias &amp;quot;b&amp;quot;" /&gt;&lt;span class="lia-inline-image-caption" onclick="event.preventDefault();"&gt;Alias "b"&lt;/span&gt;&lt;/span&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-left" image-alt="Final table combining alias &amp;quot;a&amp;quot; and &amp;quot;b&amp;quot;" style="width: 200px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/34783iF6A2CDA8CEB5D192/image-size/small?v=v2&amp;amp;px=200" role="button" title="Capture d’écran 2019-12-15 à 12.32.08.png" alt="Final table combining alias &amp;quot;a&amp;quot; and &amp;quot;b&amp;quot;" /&gt;&lt;span class="lia-inline-image-caption" onclick="event.preventDefault();"&gt;Final table combining alias "a" and "b"&lt;/span&gt;&lt;/span&gt;&lt;/FONT&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;
&lt;P&gt;&amp;nbsp;&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;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 15 Dec 2019 11:34:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-freq-and-percent/m-p/611867#M178441</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2019-12-15T11:34:21Z</dc:date>
    </item>
    <item>
      <title>Re: proc freq and percent</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-freq-and-percent/m-p/611868#M178442</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;PRE&gt;total_count / &amp;amp;tc. as percent format=percent7.2,&lt;/PRE&gt;
&lt;P&gt;I always point out that this calculation fails to produce the proper answer if there are missing values in the data.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Why?&lt;/P&gt;
&lt;P&gt;Missing values would just end up as another group, with a correct count. The user can then decide to filter them out, or not.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Because people don't realize they have to make a decision to account for missings (or they don't even realize what their code is doing with missings). I have seen examples where people code count(*) in PROC SQL and they get the wrong answer. Or they develop the code on data that doesn't have missings, and then it fails later when run on code with missings. I consider using count(*) a dangerous thing to do.&lt;/P&gt;</description>
      <pubDate>Sun, 15 Dec 2019 12:05:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-freq-and-percent/m-p/611868#M178442</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-12-15T12:05:35Z</dc:date>
    </item>
  </channel>
</rss>

