<?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 distinct inputs in a column using proc sql in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Counting-distinct-inputs-in-a-column-using-proc-sql/m-p/952388#M372231</link>
    <description>&lt;P&gt;Using liternal name 'SPECIES NAME'&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;n&lt;/STRONG&gt;&lt;/FONT&gt; to represent this variable, otherwise sas would take 'SPECIES NAME' (without 'n') as a string ,therefore you would get ONLY ONE level.&lt;/P&gt;
&lt;PRE&gt;&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;option validvarname=any;&lt;/STRONG&gt;&lt;/FONT&gt;
proc sql;
  select count(distinct &lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;'SPECIES NAME'n&lt;/STRONG&gt;&lt;/FONT&gt; ) as Number_of_Species
  from mysas.MyData;
quit;&lt;/PRE&gt;</description>
    <pubDate>Tue, 03 Dec 2024 07:49:18 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2024-12-03T07:49:18Z</dc:date>
    <item>
      <title>Counting distinct inputs in a column using proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-distinct-inputs-in-a-column-using-proc-sql/m-p/952386#M372230</link>
      <description>&lt;P&gt;I have a SAS data set I read in:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;libname mysas "path to datafile location";

data mysas.MyData;
  set mysas.datafile; 
run;
&lt;/PRE&gt;
&lt;P&gt;There is a column named "SPECIES NAME" that has many different types of species. I want to get a count of how many species there are, and here is what I did:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;PRE&gt;proc sql;
  select count(distinct 'SPECIES NAME') as Number_of_Species
  from mysas.MyData;
quit;&lt;/PRE&gt;
&lt;P&gt;However, it tells me there is only 1 Number_of_Species. Looking at the data set, this is definitely not accurate. How can I fix this?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Because the 2nd (and 3rd I guess) thing I want to do is for each of the species, I want to get a count on how many years have been the data recorded for each species. Conversely, I also want to create an ordered list of all the years, then for each year, give the number of species that corresponds to that year.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There is a column in MyData called "YEAR" next to "SPECIES NAME" and it looks a bit like this:&lt;BR /&gt;&lt;BR /&gt;YEAR&amp;nbsp; &amp;nbsp; SPECIES NAME&lt;BR /&gt;2021&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; SPECIES A&lt;BR /&gt;2022&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;SPECIES A&lt;/P&gt;
&lt;P&gt;2023&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;SPECIES A&lt;/P&gt;
&lt;P&gt;2020&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;SPECIES B&lt;/P&gt;
&lt;P&gt;2022&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;SPECIES B&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am not quite sure how I would approach that in proc sql.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 03 Dec 2024 07:26:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-distinct-inputs-in-a-column-using-proc-sql/m-p/952386#M372230</guid>
      <dc:creator>unwashedhelimix</dc:creator>
      <dc:date>2024-12-03T07:26:43Z</dc:date>
    </item>
    <item>
      <title>Re: Counting distinct inputs in a column using proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-distinct-inputs-in-a-column-using-proc-sql/m-p/952388#M372231</link>
      <description>&lt;P&gt;Using liternal name 'SPECIES NAME'&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;n&lt;/STRONG&gt;&lt;/FONT&gt; to represent this variable, otherwise sas would take 'SPECIES NAME' (without 'n') as a string ,therefore you would get ONLY ONE level.&lt;/P&gt;
&lt;PRE&gt;&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;option validvarname=any;&lt;/STRONG&gt;&lt;/FONT&gt;
proc sql;
  select count(distinct &lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;'SPECIES NAME'n&lt;/STRONG&gt;&lt;/FONT&gt; ) as Number_of_Species
  from mysas.MyData;
quit;&lt;/PRE&gt;</description>
      <pubDate>Tue, 03 Dec 2024 07:49:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-distinct-inputs-in-a-column-using-proc-sql/m-p/952388#M372231</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2024-12-03T07:49:18Z</dc:date>
    </item>
    <item>
      <title>Re: Counting distinct inputs in a column using proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-distinct-inputs-in-a-column-using-proc-sql/m-p/952389#M372232</link>
      <description>&lt;P&gt;I tried this, but it looks like I am getting an error: &lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;ERROR: The following columns were not found in the contributing tables: "SPECIES NAME"n&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, when I remove the n, it runs again giving me the output of 1. I also added&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;option validvarname=any;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;before the proc sql.&lt;/P&gt;</description>
      <pubDate>Tue, 03 Dec 2024 07:57:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-distinct-inputs-in-a-column-using-proc-sql/m-p/952389#M372232</guid>
      <dc:creator>unwashedhelimix</dc:creator>
      <dc:date>2024-12-03T07:57:39Z</dc:date>
    </item>
    <item>
      <title>Re: Counting distinct inputs in a column using proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-distinct-inputs-in-a-column-using-proc-sql/m-p/952391#M372233</link>
      <description>So using the following code to check if your dataset include this variable:&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;proc contents data=mysas.MyData varnum;&lt;BR /&gt;run;</description>
      <pubDate>Tue, 03 Dec 2024 08:14:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-distinct-inputs-in-a-column-using-proc-sql/m-p/952391#M372233</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2024-12-03T08:14:03Z</dc:date>
    </item>
  </channel>
</rss>

