<?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: Counting unique observations based on another variable in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Counting-unique-observations-based-on-another-variable/m-p/418229#M67724</link>
    <description>&lt;P&gt;Use SQL and count distinct or a double proc freq.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://github.com/statgeek/SAS-Tutorials/blob/master/count_distinct_by_group" target="_blank"&gt;https://github.com/statgeek/SAS-Tutorials/blob/master/count_distinct_by_group&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*This demonstrates how to count the number of unique occurences of a variable
across groups. It uses the SASHELP.CARS dataset which is available with any SAS installation.
The objective is to determine the number of unique car makers by origin/

Note: The SQL solution can be off if you have a large data set and these are not the only two ways to calculate distinct counts.
If you're dealing with a large data set other methods may be appropriate.*/

*Count distinct IDs;
proc sql;
create table distinct_sql as
select origin, count(distinct make) as n_make
from sashelp.cars
group by origin;
quit;

*Double PROC FREQ;
proc freq data=sashelp.cars noprint;
table origin * make / out=origin_make;
run;

proc freq data=origin_make noprint;
table origin / out= distinct_freq;
run;

title 'PROC FREQ';
proc print data=distinct_freq;
run;
title 'PROC SQL';
proc print data=distinct_sql;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 04 Dec 2017 17:34:05 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2017-12-04T17:34:05Z</dc:date>
    <item>
      <title>Counting unique observations based on another variable</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Counting-unique-observations-based-on-another-variable/m-p/418226#M67723</link>
      <description>&lt;P&gt;Thank you for taking the time to help out! I'd like to count the number of unique personal_ids that purchased something, by year. There are multiple purchases for personal_ids on separate lines. I just want to know how many people bought something (anything), each year.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Variables:&lt;/P&gt;&lt;P&gt;year&lt;/P&gt;&lt;P&gt;personal_id&lt;/P&gt;&lt;P&gt;purchase&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Example of data:&lt;/P&gt;&lt;P&gt;Year&amp;nbsp; &amp;nbsp;Personal_ID&amp;nbsp; Purchase&lt;/P&gt;&lt;P&gt;2012&amp;nbsp; 123&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; corn&lt;/P&gt;&lt;P&gt;2012&amp;nbsp; 123&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; peas&lt;/P&gt;&lt;P&gt;2012&amp;nbsp; 126&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;corn&lt;/P&gt;&lt;P&gt;2013&amp;nbsp; 129&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;corn&lt;/P&gt;&lt;P&gt;2013&amp;nbsp; 129&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;carrots&lt;/P&gt;&lt;P&gt;2013&amp;nbsp; 142&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;2014&amp;nbsp; 150&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;corn&lt;/P&gt;&lt;P&gt;2014&amp;nbsp; 150&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;peas&lt;/P&gt;&lt;P&gt;2014&amp;nbsp; 150&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;peas&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Mon, 04 Dec 2017 17:28:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Counting-unique-observations-based-on-another-variable/m-p/418226#M67723</guid>
      <dc:creator>katesmith</dc:creator>
      <dc:date>2017-12-04T17:28:49Z</dc:date>
    </item>
    <item>
      <title>Re: Counting unique observations based on another variable</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Counting-unique-observations-based-on-another-variable/m-p/418229#M67724</link>
      <description>&lt;P&gt;Use SQL and count distinct or a double proc freq.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://github.com/statgeek/SAS-Tutorials/blob/master/count_distinct_by_group" target="_blank"&gt;https://github.com/statgeek/SAS-Tutorials/blob/master/count_distinct_by_group&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*This demonstrates how to count the number of unique occurences of a variable
across groups. It uses the SASHELP.CARS dataset which is available with any SAS installation.
The objective is to determine the number of unique car makers by origin/

Note: The SQL solution can be off if you have a large data set and these are not the only two ways to calculate distinct counts.
If you're dealing with a large data set other methods may be appropriate.*/

*Count distinct IDs;
proc sql;
create table distinct_sql as
select origin, count(distinct make) as n_make
from sashelp.cars
group by origin;
quit;

*Double PROC FREQ;
proc freq data=sashelp.cars noprint;
table origin * make / out=origin_make;
run;

proc freq data=origin_make noprint;
table origin / out= distinct_freq;
run;

title 'PROC FREQ';
proc print data=distinct_freq;
run;
title 'PROC SQL';
proc print data=distinct_sql;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 04 Dec 2017 17:34:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Counting-unique-observations-based-on-another-variable/m-p/418229#M67724</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-12-04T17:34:05Z</dc:date>
    </item>
  </channel>
</rss>

