<?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: How to find distinct number of rows in macro? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-distinct-number-of-rows-in-macro/m-p/397229#M95988</link>
    <description>&lt;P&gt;Very cumbersome to modify that code as you would have to examine every single value and write code for tracking.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please consider:&lt;/P&gt;
&lt;PRE&gt;proc sql noprint;
    select count(*) into : distinctcount
    from (select distinct yourvariablename 
          from yourdatasetname)
   ;
quit;&lt;/PRE&gt;
&lt;P&gt;creates a macro variable distinctcount with the count of distict values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 19 Sep 2017 20:41:09 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2017-09-19T20:41:09Z</dc:date>
    <item>
      <title>How to find distinct number of rows in macro?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-distinct-number-of-rows-in-macro/m-p/397226#M95986</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am using SAS Studio 3.6 with SAS release 9.04.01&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to find the number of unique observations under a column in a table (distinct values) inside a macro-statement, then saving the value as a macro variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I found a script to find the number of observations in a table in a macro statement like below. How can I change the macro to find the number of distinct values under a column like Column_1, then save the value as a macro variable?&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro obscnt(lib_table);
%local nobs tableid;
%let nobs=.;
 
* Open the data set of interest;
%let tableid = %sysfunc(open(&amp;amp;lib_table.));
 
* If the open was successful get the;
* number of observations and CLOSE &amp;amp;dsn;
%if &amp;amp;tableid %then %do;
     %let nobs=%sysfunc(attrn(&amp;amp;tableid,nlobs));  
     %let rc  =%sysfunc(close(&amp;amp;tableid));
%end;
%else %do;
     %put Unable to open &amp;amp;tableid. - %sysfunc(sysmsg());
%end;
 
* Return the number of observations;
%put &amp;amp;nobs;
%mend obscnt;
%obscnt(table_name);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 19 Sep 2017 18:58:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-distinct-number-of-rows-in-macro/m-p/397226#M95986</guid>
      <dc:creator>cercig</dc:creator>
      <dc:date>2017-09-19T18:58:27Z</dc:date>
    </item>
    <item>
      <title>Re: How to find distinct number of rows in macro?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-distinct-number-of-rows-in-macro/m-p/397229#M95988</link>
      <description>&lt;P&gt;Very cumbersome to modify that code as you would have to examine every single value and write code for tracking.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please consider:&lt;/P&gt;
&lt;PRE&gt;proc sql noprint;
    select count(*) into : distinctcount
    from (select distinct yourvariablename 
          from yourdatasetname)
   ;
quit;&lt;/PRE&gt;
&lt;P&gt;creates a macro variable distinctcount with the count of distict values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 19 Sep 2017 20:41:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-distinct-number-of-rows-in-macro/m-p/397229#M95988</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-09-19T20:41:09Z</dc:date>
    </item>
    <item>
      <title>Re: How to find distinct number of rows in macro?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-distinct-number-of-rows-in-macro/m-p/397234#M95990</link>
      <description>Hi &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&lt;BR /&gt;I want to do the counting inside a macro-statement like %macro; and %mend;&lt;BR /&gt;Because I want to use the same macro for different tables repeatedly. &lt;BR /&gt;Or maybe it is easier to do the counting at the end of every code.</description>
      <pubDate>Tue, 19 Sep 2017 19:12:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-distinct-number-of-rows-in-macro/m-p/397234#M95990</guid>
      <dc:creator>cercig</dc:creator>
      <dc:date>2017-09-19T19:12:53Z</dc:date>
    </item>
    <item>
      <title>Re: How to find distinct number of rows in macro?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-distinct-number-of-rows-in-macro/m-p/397240#M95992</link>
      <description>&lt;P&gt;You can make&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;code into a macro, by replacing the table and column names with macro variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro count_distinct(var=, dset=, out=);

%global &amp;amp;out; *make it global to use outside of the macro;

*count distinct;
proc sql noprint;
select count (distinct age) into :&amp;amp;out
from &amp;amp;dset;
quit;

%mend;

%count_distinct(var=age, dset=sashelp.class, out=n_age);

%put &amp;amp;n_age;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 19 Sep 2017 19:33:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-distinct-number-of-rows-in-macro/m-p/397240#M95992</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-09-19T19:33:13Z</dc:date>
    </item>
    <item>
      <title>Re: How to find distinct number of rows in macro?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-distinct-number-of-rows-in-macro/m-p/397245#M95996</link>
      <description>Thanks a lot &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;</description>
      <pubDate>Tue, 19 Sep 2017 19:49:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-distinct-number-of-rows-in-macro/m-p/397245#M95996</guid>
      <dc:creator>cercig</dc:creator>
      <dc:date>2017-09-19T19:49:54Z</dc:date>
    </item>
  </channel>
</rss>

