<?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 the number of nonmissing values in a macro in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Count-the-number-of-nonmissing-values-in-a-macro/m-p/437861#M282300</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/193735"&gt;@mk5&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;I want s1 to be the number of nonmissing values for the given quantitative variable, and s2 to be the number of nonmissing values for the given categorical variable. I am supposed to find these within the macro.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Then have the macro generate some SAS code that could find those counts for you.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro ttest2(dsn= , quantvar= , catvar= , siglev= , doplot= );
%local s1 s2 ;
proc sql noprint;
%let s1=0;
%let s2=0;
select count(&amp;amp;quantvar)
     , count(&amp;amp;catvar)
  into :s1 trimmed
     , :s2 trimmed
  from &amp;amp;dsn 
;
quit;
%if (&amp;amp;s1 &amp;lt; 30 or &amp;amp;s2 &amp;lt; 30) %then %do;
  title2 "Warning: sample sizes of &amp;amp;s1 and &amp;amp;s2 may be too small for valid inference";
%end;
... rest of macro code ...&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you mean the number of distinct non-missing values then add the DISTINCT keyword inside the COUNT() aggregate function.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;count(distinct &amp;amp;quantvar)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 16 Feb 2018 01:49:11 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2018-02-16T01:49:11Z</dc:date>
    <item>
      <title>Count the number of nonmissing values in a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-the-number-of-nonmissing-values-in-a-macro/m-p/437830#M282297</link>
      <description>&lt;P&gt;Hi, I am trying to check the sample sizes (number of non-missing values) for both a categorical and quantitative variable, within a macro, and print a subtitle if&amp;nbsp;either of them is less than 30. I have tried probably 30 variations of assigning the sample sizes to macro variables&amp;nbsp; and always have some kind of error.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro ttest2(dsn= , quantvar= , catvar= , siglev= , doplot= );&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;%let s1 = %sysfunc(count(&amp;amp;quantvar));&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;%let s2 = %sysfunc(count(&amp;amp;catvar));&lt;BR /&gt;&amp;nbsp; &amp;nbsp; %if (&amp;amp;s1 &amp;lt; 30 or &amp;amp;s2 &amp;lt; 30) %then %do;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; title2 "Warning: sample sizes of &amp;amp;s1 and &amp;amp;s2 may be too small for valid inference";&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;%end;&lt;/P&gt;&lt;P&gt;...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If anyone could tell me how to assign the sample sizes to variables within my macro, I would really appreciate it! Thank you!&lt;/P&gt;</description>
      <pubDate>Thu, 15 Feb 2018 23:46:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-the-number-of-nonmissing-values-in-a-macro/m-p/437830#M282297</guid>
      <dc:creator>mk5</dc:creator>
      <dc:date>2018-02-15T23:46:35Z</dc:date>
    </item>
    <item>
      <title>Re: Count the number of nonmissing values in a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-the-number-of-nonmissing-values-in-a-macro/m-p/437855#M282298</link>
      <description>&lt;P&gt;As a general rule, macro language does not process data.&amp;nbsp; It constructs programming statements.&amp;nbsp; So the %LET statements have nothing to do with the contents of any variables in any SAS data sets.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What values are you hoping to assign to S1 and S2?&lt;/P&gt;</description>
      <pubDate>Fri, 16 Feb 2018 01:19:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-the-number-of-nonmissing-values-in-a-macro/m-p/437855#M282298</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-02-16T01:19:45Z</dc:date>
    </item>
    <item>
      <title>Re: Count the number of nonmissing values in a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-the-number-of-nonmissing-values-in-a-macro/m-p/437860#M282299</link>
      <description>&lt;P&gt;I want s1 to be the number of nonmissing values for the given quantitative variable, and s2 to be the number of nonmissing values for the given categorical variable. I am supposed to find these within the macro.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 16 Feb 2018 01:29:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-the-number-of-nonmissing-values-in-a-macro/m-p/437860#M282299</guid>
      <dc:creator>mk5</dc:creator>
      <dc:date>2018-02-16T01:29:29Z</dc:date>
    </item>
    <item>
      <title>Re: Count the number of nonmissing values in a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-the-number-of-nonmissing-values-in-a-macro/m-p/437861#M282300</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/193735"&gt;@mk5&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;I want s1 to be the number of nonmissing values for the given quantitative variable, and s2 to be the number of nonmissing values for the given categorical variable. I am supposed to find these within the macro.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Then have the macro generate some SAS code that could find those counts for you.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro ttest2(dsn= , quantvar= , catvar= , siglev= , doplot= );
%local s1 s2 ;
proc sql noprint;
%let s1=0;
%let s2=0;
select count(&amp;amp;quantvar)
     , count(&amp;amp;catvar)
  into :s1 trimmed
     , :s2 trimmed
  from &amp;amp;dsn 
;
quit;
%if (&amp;amp;s1 &amp;lt; 30 or &amp;amp;s2 &amp;lt; 30) %then %do;
  title2 "Warning: sample sizes of &amp;amp;s1 and &amp;amp;s2 may be too small for valid inference";
%end;
... rest of macro code ...&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you mean the number of distinct non-missing values then add the DISTINCT keyword inside the COUNT() aggregate function.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;count(distinct &amp;amp;quantvar)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 16 Feb 2018 01:49:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-the-number-of-nonmissing-values-in-a-macro/m-p/437861#M282300</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-02-16T01:49:11Z</dc:date>
    </item>
    <item>
      <title>Re: Count the number of nonmissing values in a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-the-number-of-nonmissing-values-in-a-macro/m-p/437862#M282301</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/193735"&gt;@mk5&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;Always try to first write your code without macro and only when that works wrap macro code around. As others already wrote: SAS Macros don't process SAS tables. They are mainly use to implement dynamic code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here a way to go:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
%macro ttest2(dsn= , quantvar= , catvar= , siglev= , doplot= );
  data _null_;
    set &amp;amp;dsn end=last;
    array _NonMiss [2] 8 _temporary_;
    _NonMiss[1]=sum(_NonMiss[1],missing(&amp;amp;quantvar) = 0);
    _NonMiss[2]=sum(_NonMiss[2],missing(&amp;amp;catvar) = 0);
    if last then
      do;
        call symputx('s1',_NonMiss[1], l);
        call symputx('s2',_NonMiss[2], l);
      end;
  run;

  %if (%eval(&amp;amp;s1 &amp;lt; 30) or %eval(&amp;amp;s2 &amp;lt; 30)) %then 
    %do;
      title2 "Warning: sample sizes of &amp;amp;quantvar and &amp;amp;catvar may be too small for valid inference";
    %end;
  %else 
    %do;
      title2 "all good";
    %end;
  proc print data=sashelp.class(obs=1);
  run;
%mend;


data sampleDS1 sampleDS2;
  set sashelp.class;
  if _n_&amp;gt;10 then call missing(age);
  output; output;
  output sampleDS2;
run;

%ttest2(
  dsn=sampleDS1 , 
  quantvar=age , 
  catvar=name 
  );

%ttest2(
  dsn=sampleDS2 , 
  quantvar=age , 
  catvar=name 
  );
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 16 Feb 2018 01:50:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-the-number-of-nonmissing-values-in-a-macro/m-p/437862#M282301</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2018-02-16T01:50:42Z</dc:date>
    </item>
  </channel>
</rss>

