<?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 Using proc sql to group a formatted range of values in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Using-proc-sql-to-group-a-formatted-range-of-values/m-p/614883#M18674</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I have a sample dataset follows:&lt;/P&gt;&lt;P&gt;********************************************&lt;/P&gt;&lt;P&gt;data sample;&lt;BR /&gt;infile datalines delimiter=',';&lt;BR /&gt;input Email $ Product_Type Quantity;&lt;BR /&gt;datalines;&lt;BR /&gt;E1,1,2&lt;BR /&gt;E1,1,2&lt;BR /&gt;E1,1,2&lt;BR /&gt;E1,1,2&lt;BR /&gt;E1,1,2&lt;BR /&gt;E1,3,5&lt;BR /&gt;E2,1,1&lt;BR /&gt;E2,1,1&lt;BR /&gt;E2,1,1&lt;BR /&gt;E2,1,1&lt;BR /&gt;E2,2,3&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;********************************************&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;I divided users into different groups based of number of orders they made:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;********************************************&lt;/P&gt;&lt;P&gt;proc format;&lt;BR /&gt;value Orders&lt;BR /&gt;1 = '1 order'&lt;BR /&gt;2 = '2 orders'&lt;BR /&gt;3 - high = '3+ orders'&lt;BR /&gt;. = 'No purchase';&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;********************************************&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Then I used proc sql to first group data by email and product type, then put them in a subquery to group it again based on product_type and formatted range.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;********************************************&lt;/P&gt;&lt;P&gt;proc sql;&lt;BR /&gt;select product_type, put(number_of_orders, Orders.) as number_of_orders, count(*) as number_of_email_accounts&lt;BR /&gt;from (select email, product_type, count(*) as number_of_orders&lt;BR /&gt;from sample&lt;BR /&gt;group by email, product_type) a&lt;BR /&gt;group by number_of_orders, a.product_type;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;********************************************&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Both E1 and E2 have more than 3 orders for Product_Type 1. Output of subquery:&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;&lt;STRONG&gt;Email&lt;/STRONG&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;&lt;STRONG&gt;Product_Type&lt;/STRONG&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;&lt;STRONG&gt;number_of_orders&lt;/STRONG&gt;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;E1&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;1&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;5&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;E1&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;3&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;1&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;E2&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;1&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;4&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;E2&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;2&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;1&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;Final Output:&lt;/P&gt;&lt;P&gt;********************************************&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;&lt;STRONG&gt;Product_Type&lt;/STRONG&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;&lt;STRONG&gt;number_of_orders&lt;/STRONG&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;&lt;STRONG&gt;number_of_email_accounts&lt;/STRONG&gt;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;1&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;3+ orders&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;1&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;1&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;3+ orders&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;1&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;2&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;1 order&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;1&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;3&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;1 order&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;1&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&lt;BR /&gt;********************************************&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;&lt;P&gt;It does give the correct group name, but why the group by clause is not working? Can I get a result like&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;&lt;STRONG&gt;Product_Type&lt;/STRONG&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;&lt;STRONG&gt;number_of_orders&lt;/STRONG&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;&lt;STRONG&gt;number_of_email_accounts&lt;/STRONG&gt;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;1&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;3+ orders&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;2&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;2&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;1 order&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;1&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;3&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;1 order&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;1&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
    <pubDate>Fri, 03 Jan 2020 00:35:38 GMT</pubDate>
    <dc:creator>qqian</dc:creator>
    <dc:date>2020-01-03T00:35:38Z</dc:date>
    <item>
      <title>Using proc sql to group a formatted range of values</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Using-proc-sql-to-group-a-formatted-range-of-values/m-p/614883#M18674</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I have a sample dataset follows:&lt;/P&gt;&lt;P&gt;********************************************&lt;/P&gt;&lt;P&gt;data sample;&lt;BR /&gt;infile datalines delimiter=',';&lt;BR /&gt;input Email $ Product_Type Quantity;&lt;BR /&gt;datalines;&lt;BR /&gt;E1,1,2&lt;BR /&gt;E1,1,2&lt;BR /&gt;E1,1,2&lt;BR /&gt;E1,1,2&lt;BR /&gt;E1,1,2&lt;BR /&gt;E1,3,5&lt;BR /&gt;E2,1,1&lt;BR /&gt;E2,1,1&lt;BR /&gt;E2,1,1&lt;BR /&gt;E2,1,1&lt;BR /&gt;E2,2,3&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;********************************************&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;I divided users into different groups based of number of orders they made:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;********************************************&lt;/P&gt;&lt;P&gt;proc format;&lt;BR /&gt;value Orders&lt;BR /&gt;1 = '1 order'&lt;BR /&gt;2 = '2 orders'&lt;BR /&gt;3 - high = '3+ orders'&lt;BR /&gt;. = 'No purchase';&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;********************************************&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Then I used proc sql to first group data by email and product type, then put them in a subquery to group it again based on product_type and formatted range.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;********************************************&lt;/P&gt;&lt;P&gt;proc sql;&lt;BR /&gt;select product_type, put(number_of_orders, Orders.) as number_of_orders, count(*) as number_of_email_accounts&lt;BR /&gt;from (select email, product_type, count(*) as number_of_orders&lt;BR /&gt;from sample&lt;BR /&gt;group by email, product_type) a&lt;BR /&gt;group by number_of_orders, a.product_type;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;********************************************&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Both E1 and E2 have more than 3 orders for Product_Type 1. Output of subquery:&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;&lt;STRONG&gt;Email&lt;/STRONG&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;&lt;STRONG&gt;Product_Type&lt;/STRONG&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;&lt;STRONG&gt;number_of_orders&lt;/STRONG&gt;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;E1&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;1&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;5&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;E1&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;3&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;1&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;E2&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;1&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;4&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;E2&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;2&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;1&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;Final Output:&lt;/P&gt;&lt;P&gt;********************************************&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;&lt;STRONG&gt;Product_Type&lt;/STRONG&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;&lt;STRONG&gt;number_of_orders&lt;/STRONG&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;&lt;STRONG&gt;number_of_email_accounts&lt;/STRONG&gt;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;1&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;3+ orders&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;1&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;1&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;3+ orders&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;1&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;2&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;1 order&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;1&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;3&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;1 order&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;1&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&lt;BR /&gt;********************************************&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;&lt;P&gt;It does give the correct group name, but why the group by clause is not working? Can I get a result like&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;&lt;STRONG&gt;Product_Type&lt;/STRONG&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;&lt;STRONG&gt;number_of_orders&lt;/STRONG&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;&lt;STRONG&gt;number_of_email_accounts&lt;/STRONG&gt;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;1&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;3+ orders&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;2&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;2&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;1 order&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;1&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;3&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;1 order&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;1&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Fri, 03 Jan 2020 00:35:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Using-proc-sql-to-group-a-formatted-range-of-values/m-p/614883#M18674</guid>
      <dc:creator>qqian</dc:creator>
      <dc:date>2020-01-03T00:35:38Z</dc:date>
    </item>
    <item>
      <title>Re: Using proc sql to group a formatted range of values</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Using-proc-sql-to-group-a-formatted-range-of-values/m-p/614885#M18675</link>
      <description>&lt;P&gt;The data set you show is NOT the dataset used in the sub-query&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;(select email, product_type, count(*) as number_of_orders
from db1
group by email, product_type) &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;since you don't show a variable named email. Thus, we can't replicate the result you are getting or explain why it is happening.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So please, give us (a portion of) the actual data used by this code in the data set DB1, and not some "close facsimile" of the data set. Please use &lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712" target="_self"&gt;these instructions&lt;/A&gt; to provide the data set as a SAS data step. DO NOT USE ANY OTHER METHOD TO PROVIDE THE DATA.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, I don't see any reason for you to turn the variable QUANTITY to character, unnecessary effort to turn a numeric variable into character here.&lt;/P&gt;</description>
      <pubDate>Thu, 02 Jan 2020 23:58:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Using-proc-sql-to-group-a-formatted-range-of-values/m-p/614885#M18675</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-01-02T23:58:56Z</dc:date>
    </item>
    <item>
      <title>Re: Using proc sql to group a formatted range of values</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Using-proc-sql-to-group-a-formatted-range-of-values/m-p/614886#M18676</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/298735"&gt;@qqian&lt;/a&gt;&amp;nbsp; &amp;nbsp;Breaking your SQL into steps&amp;nbsp; makes me wonder how do you expect what you are looking for&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input Email $	Product_Type	Quantity;
cards;
E1	1	2
E2	1	1
E2	2	3
E3	4	24
E4	3	5
E5	2	6
E5	2	8
E5	3	2
;

proc format;
value Orders
1 = '1 order'
2 = '2 orders'
3 - high = 'More than 3 orders'
. = 'No purchase';
run;

proc sql;
create table temp as
select email, product_type, count(*) as number_of_orders
from have
group by email, product_type;
quit;


proc sql;
create table w as
select *, put(number_of_orders, Orders.) as number_of_orders1
from temp
group by number_of_orders1, product_type;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Please take a look at Temp and W, and clarify&lt;/P&gt;</description>
      <pubDate>Fri, 03 Jan 2020 00:03:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Using-proc-sql-to-group-a-formatted-range-of-values/m-p/614886#M18676</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-01-03T00:03:29Z</dc:date>
    </item>
    <item>
      <title>Re: Using proc sql to group a formatted range of values</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Using-proc-sql-to-group-a-formatted-range-of-values/m-p/614889#M18677</link>
      <description>&lt;P&gt;Hi Paige,&lt;/P&gt;&lt;P&gt;Sorry for the confusing information. I modified my message and provided the dataset I created, the code and also the output.&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Quan&lt;/P&gt;</description>
      <pubDate>Fri, 03 Jan 2020 00:32:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Using-proc-sql-to-group-a-formatted-range-of-values/m-p/614889#M18677</guid>
      <dc:creator>qqian</dc:creator>
      <dc:date>2020-01-03T00:32:29Z</dc:date>
    </item>
    <item>
      <title>Re: Using proc sql to group a formatted range of values</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Using-proc-sql-to-group-a-formatted-range-of-values/m-p/614890#M18678</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for your response!! I modified my message above and provided the output I got and the ideal output I want to have.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Quan&lt;/P&gt;</description>
      <pubDate>Fri, 03 Jan 2020 00:34:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Using-proc-sql-to-group-a-formatted-range-of-values/m-p/614890#M18678</guid>
      <dc:creator>qqian</dc:creator>
      <dc:date>2020-01-03T00:34:21Z</dc:date>
    </item>
    <item>
      <title>Re: Using proc sql to group a formatted range of values</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Using-proc-sql-to-group-a-formatted-range-of-values/m-p/614892#M18679</link>
      <description>&lt;P&gt;Hi Thank you&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/298735"&gt;@qqian&lt;/a&gt;&amp;nbsp; Much better. Here you go, the following should meet your need&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;



data sample;
infile datalines delimiter=',';
input Email $ Product_Type Quantity;
datalines;
E1,1,2
E1,1,2
E1,1,2
E1,1,2
E1,1,2
E1,3,5
E2,1,1
E2,1,1
E2,1,1
E2,1,1
E2,2,3
;
run;



proc format;
value Orders
1 = '1 order'
2 = '2 orders'
3 - high = 'More than 3 orders'
. = 'No purchase';
run;


proc sql;
create table want as
select product_type,number_of_orders,count(*) as number_of_email_accounts
from 
(select email, product_type, put(count(*), Orders.) as number_of_orders 
from sample
group by email, product_type)
group by number_of_orders,product_type
order by product_type;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;EDITED: To include formatted value&lt;/P&gt;</description>
      <pubDate>Fri, 03 Jan 2020 00:46:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Using-proc-sql-to-group-a-formatted-range-of-values/m-p/614892#M18679</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-01-03T00:46:47Z</dc:date>
    </item>
    <item>
      <title>Re: Using proc sql to group a formatted range of values</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Using-proc-sql-to-group-a-formatted-range-of-values/m-p/614893#M18680</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'd like to add&amp;nbsp;&lt;CODE class="  language-sas"&gt;number_of_orders in the want table, then it works! I don't know it should be solved by creating a new table.&lt;/CODE&gt;&lt;/P&gt;&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token procnames"&gt;proc&lt;/SPAN&gt; &lt;SPAN class="token procnames"&gt;sql&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
create &lt;SPAN class="token statement"&gt;table&lt;/SPAN&gt; want as
&lt;SPAN class="token statement"&gt;select&lt;/SPAN&gt; product_type&lt;SPAN class="token punctuation"&gt;, number_of_orders, &lt;/SPAN&gt;&lt;SPAN class="token function"&gt;count&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;*&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt; as number_of_email_accounts
&lt;SPAN class="token keyword"&gt;from&lt;/SPAN&gt; 
&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token statement"&gt;select&lt;/SPAN&gt; email&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt; product_type&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt; &lt;SPAN class="token keyword"&gt;put&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;count&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;*&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt; Orders&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt; as number_of_orders 
&lt;SPAN class="token keyword"&gt;from&lt;/SPAN&gt; sample
&lt;SPAN class="token keyword"&gt;group&lt;/SPAN&gt; &lt;SPAN class="token statement"&gt;by&lt;/SPAN&gt; email&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt; product_type&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;
&lt;SPAN class="token keyword"&gt;group&lt;/SPAN&gt; &lt;SPAN class="token statement"&gt;by&lt;/SPAN&gt; number_of_orders&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;product_type&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token procnames"&gt;quit&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Thank you so much!!&lt;/P&gt;</description>
      <pubDate>Fri, 03 Jan 2020 00:50:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Using-proc-sql-to-group-a-formatted-range-of-values/m-p/614893#M18680</guid>
      <dc:creator>qqian</dc:creator>
      <dc:date>2020-01-03T00:50:11Z</dc:date>
    </item>
    <item>
      <title>Re: Using proc sql to group a formatted range of values</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Using-proc-sql-to-group-a-formatted-range-of-values/m-p/614894#M18681</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/298735"&gt;@qqian&lt;/a&gt;&amp;nbsp; No need to create a new table WANT. That has become a habit for us in the coommunity to use HAVE and WANT everywhere lol&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Feel free to remove the create table statement as i tested below&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
proc sql;
select product_type,number_of_orders,count(*) as number_of_email_accounts
from 
(select email, product_type, put(count(*), Orders.) as number_of_orders 
from sample
group by email, product_type)
group by number_of_orders,product_type
order by product_type;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;DIV class="branch"&gt;
&lt;DIV&gt;
&lt;DIV align="center"&gt;
&lt;TABLE class="table" summary="Procedure SQL: Query Results" frame="box" rules="all" cellspacing="0" cellpadding="5"&gt;
&lt;THEAD&gt;
&lt;TR&gt;
&lt;TH class="r b header" scope="col"&gt;Product_Type&lt;/TH&gt;
&lt;TH class="l b header" scope="col"&gt;number_of_orders&lt;/TH&gt;
&lt;TH class="r b header" scope="col"&gt;number_of_email_accounts&lt;/TH&gt;
&lt;/TR&gt;
&lt;/THEAD&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;1&lt;/TD&gt;
&lt;TD class="l data"&gt;More than 3 orders&lt;/TD&gt;
&lt;TD class="r data"&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;2&lt;/TD&gt;
&lt;TD class="l data"&gt;1 order&lt;/TD&gt;
&lt;TD class="r data"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;3&lt;/TD&gt;
&lt;TD class="l data"&gt;1 order&lt;/TD&gt;
&lt;TD class="r data"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;</description>
      <pubDate>Fri, 03 Jan 2020 00:54:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Using-proc-sql-to-group-a-formatted-range-of-values/m-p/614894#M18681</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-01-03T00:54:54Z</dc:date>
    </item>
    <item>
      <title>Re: Using proc sql to group a formatted range of values</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Using-proc-sql-to-group-a-formatted-range-of-values/m-p/614896#M18682</link>
      <description>&lt;P&gt;gotcha, so the key is to move 'put' in the subquery. Learned a lot.!&lt;/P&gt;</description>
      <pubDate>Fri, 03 Jan 2020 00:57:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Using-proc-sql-to-group-a-formatted-range-of-values/m-p/614896#M18682</guid>
      <dc:creator>qqian</dc:creator>
      <dc:date>2020-01-03T00:57:07Z</dc:date>
    </item>
  </channel>
</rss>

