<?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: Count a categorical variable by group in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Count-a-categorical-variable-by-group/m-p/231484#M42101</link>
    <description>&lt;P&gt;In addition, if you like to save it in a SAS dataset, you may create a Table within the PROC SQL as shown below...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL PRINT;
   CREATE TABLE CARRIER_COUNT AS
          SELECT DISTINCT CARRIER,COUNT(*) AS FLIGHT_COUNT FROM HAVE GROUP BY CARRIER;
QUIT;

PROC PRINT DATA=CARRIER_COUNT NOOBS;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sat, 24 Oct 2015 14:55:39 GMT</pubDate>
    <dc:creator>kannand</dc:creator>
    <dc:date>2015-10-24T14:55:39Z</dc:date>
    <item>
      <title>Count a categorical variable by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-a-categorical-variable-by-group/m-p/231479#M42097</link>
      <description>&lt;P&gt;My dataset is like this&amp;nbsp;&lt;/P&gt;&lt;P&gt;carrier&amp;nbsp;&lt;/P&gt;&lt;P&gt;B1&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;B1&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;B1&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;B1&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;A9&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;A9&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;OO&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;OO&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;OO&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;OO&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;OO&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;I want the output like this&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;B1 &amp;nbsp; 4&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;A9 &amp;nbsp; 2&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;OO &amp;nbsp;5&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;and then make a barplot, the following is my codes:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;PROC MEANS DATA=schedule_Jan NOPRINT;
BY carrier ;
OUTPUT OUT= flight_count COUNT(carrier) =number_of_flights;
RUN;

PROC SGPLOT DATA= flight_count;
HBAR CARRIER;
RESPONSE = number_of_flights;
RUN;&lt;/PRE&gt;&lt;P&gt;but it stop at this line &amp;nbsp; and the log is as below, could anybody give some explanation and solution aobut this ?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;222  OUTPUT OUT= flight_count COUNT(unique_carrier) =number_of_flights;
                              -----
                              22
                              76
ERROR 22-322: Syntax error, expecting one of the following: ;, (, /, CSS, CV, IDGROUP, IDGRP,
              KURTOSIS, LCLM, MAX, MAXID, MEAN, MEDIAN, MIN, MINID, MODE, N, NMISS, OUT, P1,
              P10, P20, P25, P30, P40, P5, P50, P60, P70, P75, P80, P90, P95, P99, PROBT, Q1,
              Q3, QRANGE, RANGE, SKEWNESS, STDDEV, STDERR, SUM, SUMWGT, T, UCLM, USS, VAR.

ERROR 76-322: Syntax error, statement will be ignored.&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 24 Oct 2015 14:11:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-a-categorical-variable-by-group/m-p/231479#M42097</guid>
      <dc:creator>DingDing</dc:creator>
      <dc:date>2015-10-24T14:11:28Z</dc:date>
    </item>
    <item>
      <title>Re: Count a categorical variable by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-a-categorical-variable-by-group/m-p/231482#M42099</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input CARRIER $2.;
datalines;
B1
B1
B1
B1
A9
A9
OO
OO
OO
OO
OO
;RUN;
PROC SQL;
   SELECT DISTINCT CARRIER,COUNT(*) FROM HAVE GROUP BY CARRIER;
QUIT;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 24 Oct 2015 14:44:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-a-categorical-variable-by-group/m-p/231482#M42099</guid>
      <dc:creator>kannand</dc:creator>
      <dc:date>2015-10-24T14:44:43Z</dc:date>
    </item>
    <item>
      <title>Re: Count a categorical variable by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-a-categorical-variable-by-group/m-p/231483#M42100</link>
      <description>&lt;P&gt;Use PROC FREQ instead of PROC MEANS&lt;/P&gt;</description>
      <pubDate>Sat, 24 Oct 2015 14:51:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-a-categorical-variable-by-group/m-p/231483#M42100</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2015-10-24T14:51:19Z</dc:date>
    </item>
    <item>
      <title>Re: Count a categorical variable by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-a-categorical-variable-by-group/m-p/231484#M42101</link>
      <description>&lt;P&gt;In addition, if you like to save it in a SAS dataset, you may create a Table within the PROC SQL as shown below...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL PRINT;
   CREATE TABLE CARRIER_COUNT AS
          SELECT DISTINCT CARRIER,COUNT(*) AS FLIGHT_COUNT FROM HAVE GROUP BY CARRIER;
QUIT;

PROC PRINT DATA=CARRIER_COUNT NOOBS;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 24 Oct 2015 14:55:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-a-categorical-variable-by-group/m-p/231484#M42101</guid>
      <dc:creator>kannand</dc:creator>
      <dc:date>2015-10-24T14:55:39Z</dc:date>
    </item>
    <item>
      <title>Re: Count a categorical variable by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-a-categorical-variable-by-group/m-p/231485#M42102</link>
      <description>&lt;P&gt;Thank you PaigeMiller, but&amp;nbsp;&lt;SPAN class="login-bold"&gt;if it is a column contains just several observations, "&lt;SPAN&gt;PROC FREQ" my work, yet&amp;nbsp;&lt;/SPAN&gt;the fact is that the variable "unique_carrier" in a dataset contarins many many different observations that I can not just use "table" to &amp;nbsp;list all of them, so is there any solution?&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="login-bold"&gt;I may make my example more clear, &amp;nbsp;the carrier contains more observations not just the&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;B1,&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;A9 and&amp;nbsp;&lt;/SPAN&gt;OO that i enumerate.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;carrier&amp;nbsp;&lt;/P&gt;&lt;P&gt;B1&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;B1&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;B1&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;B1&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;A9&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;A9&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;OO&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;OO&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;OO&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;OO&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;OO&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;.........&lt;/P&gt;</description>
      <pubDate>Sat, 24 Oct 2015 15:03:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-a-categorical-variable-by-group/m-p/231485#M42102</guid>
      <dc:creator>DingDing</dc:creator>
      <dc:date>2015-10-24T15:03:21Z</dc:date>
    </item>
    <item>
      <title>Re: Count a categorical variable by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-a-categorical-variable-by-group/m-p/231486#M42103</link>
      <description>&lt;P&gt;Thank you kannand! I am sorry that I dont understand your code very much since I am just at the begining in using&amp;nbsp;SAS, so could you add some explanation on the codes ? Thanks in advance.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 24 Oct 2015 15:08:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-a-categorical-variable-by-group/m-p/231486#M42103</guid>
      <dc:creator>DingDing</dc:creator>
      <dc:date>2015-10-24T15:08:03Z</dc:date>
    </item>
    <item>
      <title>Re: Count a categorical variable by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-a-categorical-variable-by-group/m-p/231488#M42104</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* 
========================================
This next step creates a SAS dataset of 
your data specified in the "datalines" 
========================================
*/

data have;
input CARRIER $2.;
datalines;
B1
B1
B1
B1
A9
A9
OO
OO
OO
OO
OO
;RUN;
/* 
========================================
This next step Uses "PROC SQL" to summarize 
your data specified in the "datalines and 
also saves the result in a SAS dataset 
called  CARRIER_SUMMARY" 
========================================
*/
PROC SQL PRINT;
   CREATE TABLE CARRIER_SUMMARY AS
          SELECT DISTINCT CARRIER,COUNT(*) AS FLIGHT_COUNT FROM HAVE GROUP BY CARRIER;
QUIT;
/*
========================================
This step just prints the summary created 
========================================
*/
PROC PRINT DATA=CARRIER_SUMMARY NOOBS;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hope these added comments in the code help. &amp;nbsp;&lt;/P&gt;
&lt;P&gt;I've attached the SAS Log for your reference&amp;nbsp;here:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; 
 1          OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
 55         
 56         /*
 57         ========================================
 58         This next step creates a SAS dataset of
 59         your data specified in the "datalines"
 60         ========================================
 61         */
 62         
 63         data have;
 64         input CARRIER $2.;
 65         datalines;
 
 NOTE: The data set WORK.HAVE has 11 observations and 1 variables.
 NOTE: DATA statement used (Total process time):
       real time           0.06 seconds
       cpu time            0.05 seconds
       
 77         ;RUN;
 
 78         /*
 79         ========================================
 80         This next step Uses "PROC SQL" to summarize
 81         your data specified in the "datalines and
 82         also saves the result in a SAS dataset
 83         called  CARRIER_SUMMARY"
 84         ========================================
 85         */
 86         PROC SQL PRINT;
 87            CREATE TABLE CARRIER_SUMMARY AS
 88                   SELECT DISTINCT CARRIER,COUNT(*) AS FLIGHT_COUNT FROM HAVE GROUP BY CARRIER;
 NOTE: Table WORK.CARRIER_SUMMARY created, with 3 rows and 2 columns.
 
 89         QUIT;
 NOTE: PROCEDURE SQL used (Total process time):
       real time           0.04 seconds
       cpu time            0.04 seconds
       
 
 90         /*
 91         ========================================
 92         This step just prints the summary created
 93         ========================================
 94         */
 95         PROC PRINT DATA=CARRIER_SUMMARY NOOBS;
 96         
 97         
 98         OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
 110        &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The code produced the following output. Hope this helps...!!! Good Luck to you DingDing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE class="table"&gt;&lt;COLGROUP&gt;&lt;COL /&gt;&lt;COL /&gt;&lt;/COLGROUP&gt;
&lt;THEAD&gt;
&lt;TR&gt;
&lt;TH class="header" scope="col"&gt;CARRIER&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;FLIGHT_COUNT&lt;/TH&gt;
&lt;/TR&gt;
&lt;/THEAD&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD class="data"&gt;A9&lt;/TD&gt;
&lt;TD class="r data"&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="data"&gt;B1&lt;/TD&gt;
&lt;TD class="r data"&gt;4&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="data"&gt;OO&lt;/TD&gt;
&lt;TD class="r data"&gt;5&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;</description>
      <pubDate>Sat, 24 Oct 2015 15:39:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-a-categorical-variable-by-group/m-p/231488#M42104</guid>
      <dc:creator>kannand</dc:creator>
      <dc:date>2015-10-24T15:39:28Z</dc:date>
    </item>
    <item>
      <title>Re: Count a categorical variable by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-a-categorical-variable-by-group/m-p/231490#M42106</link>
      <description>&lt;P&gt;Thank you ,&amp;nbsp;kannand! I am inspired by your code and then change them as:&lt;/P&gt;&lt;P&gt;proc sql ;&lt;BR /&gt;create table flight_count as&lt;BR /&gt;select unique_carrier, count(*) as number_of_flights&lt;BR /&gt;from schedule_Jan&lt;BR /&gt;group by unique_carrier&lt;BR /&gt;order by unique_carrier&lt;BR /&gt;;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;then it works!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;really very appreciate for helping me !!!&lt;/P&gt;</description>
      <pubDate>Sat, 24 Oct 2015 15:38:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-a-categorical-variable-by-group/m-p/231490#M42106</guid>
      <dc:creator>DingDing</dc:creator>
      <dc:date>2015-10-24T15:38:48Z</dc:date>
    </item>
    <item>
      <title>Re: Count a categorical variable by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-a-categorical-variable-by-group/m-p/231514#M42123</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/59466"&gt;@DingDing&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;Thank you PaigeMiller, but&amp;nbsp;&lt;SPAN class="login-bold"&gt;if it is a column contains just several observations, "&lt;SPAN&gt;PROC FREQ" my work, yet&amp;nbsp;&lt;/SPAN&gt;the fact is that the variable "unique_carrier" in a dataset contarins many many different observations that I can not just use "table" to &amp;nbsp;list all of them, so is there any solution?&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="login-bold"&gt;I may make my example more clear, &amp;nbsp;the carrier contains more observations not just the&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;B1,&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;A9 and&amp;nbsp;&lt;/SPAN&gt;OO that i enumerate.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;carrier&amp;nbsp;&lt;/P&gt;
&lt;P&gt;B1&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;B1&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;B1&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;B1&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;A9&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;A9&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;OO&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;OO&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;OO&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;OO&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;OO&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;.........&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Sorry, but I don't think that makes anything clearer, and while I'm glad you seem to have found a solution, from my understanding of what you have said, PROC FREQ fits perfectly.&lt;/P&gt;</description>
      <pubDate>Sun, 25 Oct 2015 01:04:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-a-categorical-variable-by-group/m-p/231514#M42123</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2015-10-25T01:04:40Z</dc:date>
    </item>
  </channel>
</rss>

