<?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 missing value for character variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/count-missing-value-for-character-variable/m-p/284677#M58082</link>
    <description>&lt;P&gt;'100' in my code represents total number of VARIABLES in the data set and NOT the number of observations as I wrote on the comments. The array will work even with 10 Million Variables. It just provides memory space to hold that many variables but in reality it will much less and the counting is done for as many NUMERIC and CHARACTER Variables.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If you know the numbers of numeric variables (say 15) and character variables (say 20) before running the data step, you can size the arrays as:&amp;nbsp;&lt;/P&gt;&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;   &lt;SPAN class="token statement"&gt;array&lt;/SPAN&gt; n_count&lt;SPAN class="token punctuation"&gt;[15&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;]&lt;/SPAN&gt; _temporary_ &lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;15&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;*&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;0&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt; &lt;SPAN class="token comment"&gt;* Assuming data set has no more than 100 OBS;&lt;/SPAN&gt;
   &lt;SPAN class="token statement"&gt;array&lt;/SPAN&gt; c_count&lt;SPAN class="token punctuation"&gt;[2&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;0&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;]&lt;/SPAN&gt; _temporary_ &lt;SPAN class="token punctuation"&gt;(2&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;0&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;*&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;0&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Hope this helps you.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 14 Jul 2016 20:49:14 GMT</pubDate>
    <dc:creator>KachiM</dc:creator>
    <dc:date>2016-07-14T20:49:14Z</dc:date>
    <item>
      <title>count missing value for character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-missing-value-for-character-variable/m-p/284212#M57986</link>
      <description>&lt;P&gt;Hi &lt;SPAN class="login-bold"&gt;All,&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="login-bold"&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="login-bold"&gt;The program:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="login-bold"&gt;proc tabulate data=sashelp.class f=comma6.0;&lt;BR /&gt;title ;&lt;BR /&gt;var _numeric_;&lt;BR /&gt;table _numeric_,&lt;BR /&gt;nmiss n/ box='Variable';&lt;BR /&gt;run;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="login-bold"&gt;can be used to count non-missing values for numerical variable, I also want count for character variable, could you help me modify this code to achive my goal?&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="login-bold"&gt;Thanks a lot!&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="login-bold"&gt;Best wishes,&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 13 Jul 2016 22:44:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-missing-value-for-character-variable/m-p/284212#M57986</guid>
      <dc:creator>Xiaoningdemao</dc:creator>
      <dc:date>2016-07-13T22:44:29Z</dc:date>
    </item>
    <item>
      <title>Re: count missing value for character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-missing-value-for-character-variable/m-p/284216#M57988</link>
      <description>&lt;P&gt;Character variables in Proc Tabulate may only be CLASS variables. Because of the default that any record with a missing value for a class variable is excluded you would need to add the missing option. The only statistics you may request for character variables in Proc Tabulate are N and the various N-related such a Pctn, ColPctN, RowPctn, PagePctn and RepPctn.&lt;/P&gt;
&lt;P&gt;The following code adds a separate table to your output that has each value of the character var and its count.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc tabulate data=sashelp.class f=comma6.0;
   title ;
   var _numeric_;
   table _numeric_,
      nmiss n/ box='Variable';
    class _character_ / missing;
    table _character_,n;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want a simplier count you might want to go through your data and add a numeric flag for when the character variable isn't missing and count (or sum) that. Though there isn't going be a short easy way to reference the character variable counted.&lt;/P&gt;</description>
      <pubDate>Wed, 13 Jul 2016 23:19:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-missing-value-for-character-variable/m-p/284216#M57988</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-07-13T23:19:52Z</dc:date>
    </item>
    <item>
      <title>Re: count missing value for character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-missing-value-for-character-variable/m-p/284217#M57989</link>
      <description>&lt;P&gt;Do you want to take another way to get what you want? This way may help you to get more information of the observations.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Be brave to do some programming with arrays.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   set sashelp.class end = eof;
   N + 1;
   array num[*] _numeric_;
   array char[*] _character_;
   array n_count[100] _temporary_ (100*0); * Assuming data set has no more than 100 OBS;
   array c_count[100] _temporary_ (100*0);
   do i = 1 to dim(num);
      if missing(num[i]) then n_count[i] + 1;
   end;
   do i = 1 to dim(char);
      if missing(char[i]) then c_count[i] + 1;
   end;
   if eof then do;
      do i = 1 to dim(num);
         Variable = vname(num[i]);
         NMiss = n_count[i];
         output;
      end;
      do i = 1 to dim(char);
         Variable = vname(char[i]);
         NMiss = c_count[i];
         output;
      end;
   end;
keep Variable N NMiss;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 13 Jul 2016 23:22:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-missing-value-for-character-variable/m-p/284217#M57989</guid>
      <dc:creator>KachiM</dc:creator>
      <dc:date>2016-07-13T23:22:46Z</dc:date>
    </item>
    <item>
      <title>Re: count missing value for character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-missing-value-for-character-variable/m-p/284237#M58004</link>
      <description>&lt;P&gt;You can use PROC SQL to get the count of missing values for character or numeric variables. &amp;nbsp;A coworker asked how to find all the variables in a data set that are missing for all observations. &amp;nbsp;The code example below can answer that question. &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options mprint;

/* Using SASHELP data sets while testing macro. 
   Macro should list variables that are 
   completely missing in data set, so need to
   create some data sets with such variables.  */

data baseball;
  set sashelp.baseball;
  suzanne_favorite_team='';
  cleveland_indians_rank=.;
run;

data class;
  set sashelp.class;
  teacher='';
  grade_level=.;
run;

%macro missing_summary(lib=, dsn=);

  /* use dummy macro so that syntax highlighting shows up within missing macro */

  %macro dummy();  
  %mend dummy;

  /* get data set variable names */

  proc sql;
    create table vars_to_summarize as
	  select name,
	         label
	    from sashelp.vcolumn
		  where libname="%upcase(&amp;amp;lib)" and
		        memname="%upcase(&amp;amp;dsn)";
  quit;

  /* count number of variables to process */

  proc sql noprint;
    select count(*) into
	  :var_counter
	     from vars_to_summarize;
  quit;

  /* do PROC SQL one variable at a time,
     append details for variable to summary
     report, then delete the variable's data
     set so that work library doesn't get 
     cluttered. */

  %do i=1 %to &amp;amp;var_counter;

    data _null_;
      obsnum=&amp;amp;i;
      set vars_to_summarize point=obsnum;
      if _error_ then abort;
      call symputx('field',name);
	  call symputx('description',label);
      stop; 
    run;

    proc sql;
      create table &amp;amp;field._report as
        select count(*) as n,
               count(&amp;amp;field) as not_missing,
	           nmiss(&amp;amp;field) as missing
          from &amp;amp;lib..&amp;amp;dsn;
    quit; 

    data &amp;amp;field._report;
      length variable $ 32 dataset $ 41 label $ 256;
	  set &amp;amp;field._report;
	  variable="&amp;amp;field";
      dataset="&amp;amp;lib..&amp;amp;dsn";
	  label="&amp;amp;description";
    run;
  
    proc append base=summary data=&amp;amp;field._report;
    run;

    proc datasets nolist;
      delete &amp;amp;field._report;
    quit;

  %end;

  proc datasets nolist;
    delete vars_to_summarize;
  quit;

%mend missing_summary;

%missing_summary(lib=work, dsn=class);
%missing_summary(lib=work, dsn=baseball);

proc print data=summary noobs;
  by dataset notsorted;
 * uncomment statement below if you only want to see the variables missing for ALL obs ;
 * where n=missing;   
  var dataset variable label n not_missing missing;
  title 'Missing and not-missing counts by variable';
run;

* clean up;

proc datasets nolist;
  delete summary;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;References:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A title="SAS Global Forum 2013 paper by Chao Huang and Yu FU" href="http://support.sas.com/resources/papers/proceedings13/257-2013.pdf" target="_blank"&gt;Top 10 Most Powerful Functions for PROC SQL&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A title="SUGI 20 paper by Frank Dilorio and Jeff Abolafia" href="http://www2.sas.com/proceedings/sugi29/237-29.pdf" target="_self"&gt;Dictionary Tables and Views: Essential Tools for Serious Applications&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 14 Jul 2016 04:13:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-missing-value-for-character-variable/m-p/284237#M58004</guid>
      <dc:creator>SuzanneDorinski</dc:creator>
      <dc:date>2016-07-14T04:13:24Z</dc:date>
    </item>
    <item>
      <title>Re: count missing value for character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-missing-value-for-character-variable/m-p/284402#M58052</link>
      <description>thank you your help!!! But the result looks like a proc freq for each character variable, but I just want a count of non-missing, instead add them up manually do you have other solution? since the data set I deal with now have almost 1000 variables, so go through them one by one is not very realistic....</description>
      <pubDate>Thu, 14 Jul 2016 17:36:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-missing-value-for-character-variable/m-p/284402#M58052</guid>
      <dc:creator>Xiaoningdemao</dc:creator>
      <dc:date>2016-07-14T17:36:01Z</dc:date>
    </item>
    <item>
      <title>Re: count missing value for character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-missing-value-for-character-variable/m-p/284547#M58054</link>
      <description>This is perfect! I got exactly what I want!! Just to double check with you, I just need to change all the "100" in this code to the number of obs in my data set, right? &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/17813"&gt;@KachiM&lt;/a&gt;</description>
      <pubDate>Thu, 14 Jul 2016 17:42:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-missing-value-for-character-variable/m-p/284547#M58054</guid>
      <dc:creator>Xiaoningdemao</dc:creator>
      <dc:date>2016-07-14T17:42:36Z</dc:date>
    </item>
    <item>
      <title>Re: count missing value for character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-missing-value-for-character-variable/m-p/284609#M58057</link>
      <description>Dear &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12496"&gt;@SuzanneDorinski&lt;/a&gt;,&lt;BR /&gt;&lt;BR /&gt;Thank you so much for working on this problem and wrote such a nice code with all comments on! But the data set i'm dealing with is very large, this code takes a lot time(about 10 minutes). Sorry I didn't accept this as solution......&lt;BR /&gt;Since i am a beginner in SAS, i did learn a lot from your code. Thanks again!!&lt;BR /&gt;&lt;BR /&gt;Best,</description>
      <pubDate>Thu, 14 Jul 2016 18:08:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-missing-value-for-character-variable/m-p/284609#M58057</guid>
      <dc:creator>Xiaoningdemao</dc:creator>
      <dc:date>2016-07-14T18:08:51Z</dc:date>
    </item>
    <item>
      <title>Re: count missing value for character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-missing-value-for-character-variable/m-p/284615#M58058</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/90105"&gt;@Xiaoningdemao&lt;/a&gt; wrote:&lt;BR /&gt;This is perfect! I got exactly what I want!! Just to double check with you, I just need to change all the "100" in this code to the number of obs in my data set, right? &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/17813"&gt;@KachiM&lt;/a&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Ideally the 100 would be replaced with the number of numeric variables for the numeric array and the number of character variables for the character array.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is an example code snippet to get the counts of char and num variables.:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
   select type, count(*) as n
   from dictionary.columns 
      where libname='SASHELP' and memname='CLASS'
   group by type;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 14 Jul 2016 18:22:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-missing-value-for-character-variable/m-p/284615#M58058</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-07-14T18:22:30Z</dc:date>
    </item>
    <item>
      <title>Re: count missing value for character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-missing-value-for-character-variable/m-p/284622#M58061</link>
      <description>Hi &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;,&lt;BR /&gt;Thank you!!&lt;BR /&gt;But I meant the code you fist provided me produced a proc freq of each character variable. Result like this:&lt;BR /&gt;N&lt;BR /&gt;Gender&lt;BR /&gt;11,972&lt;BR /&gt;male 1220&lt;BR /&gt;female 30&lt;BR /&gt;&lt;BR /&gt;But the value I wanted is the number of nonmissing values in this example 1220+30=1250.&lt;BR /&gt;&lt;BR /&gt;I'm wondering is there a solution to get this value?&lt;BR /&gt;&lt;BR /&gt;thanks again!!&lt;BR /&gt;&lt;BR /&gt;Best,&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Thu, 14 Jul 2016 18:40:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-missing-value-for-character-variable/m-p/284622#M58061</guid>
      <dc:creator>Xiaoningdemao</dc:creator>
      <dc:date>2016-07-14T18:40:29Z</dc:date>
    </item>
    <item>
      <title>Re: count missing value for character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-missing-value-for-character-variable/m-p/284676#M58081</link>
      <description>&lt;P&gt;For character variables add a variable to count or sum depending on how you are summarizing.&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; set have;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; GenderNotMiss= (not missing(gender)); /* creates a 1 for not missing, 0 for missing values of gender*/&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then in proc tabulate request SUM of GenderNotMiss;&lt;/P&gt;
&lt;P&gt;or: if not missing(gender0 then GenderNotMiss=1;&lt;/P&gt;
&lt;P&gt;and you could use the NMISS in tabulate along with the other numerics.&lt;/P&gt;
&lt;P&gt;I would suggest using a good label with these indicator variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 14 Jul 2016 20:44:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-missing-value-for-character-variable/m-p/284676#M58081</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-07-14T20:44:32Z</dc:date>
    </item>
    <item>
      <title>Re: count missing value for character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-missing-value-for-character-variable/m-p/284677#M58082</link>
      <description>&lt;P&gt;'100' in my code represents total number of VARIABLES in the data set and NOT the number of observations as I wrote on the comments. The array will work even with 10 Million Variables. It just provides memory space to hold that many variables but in reality it will much less and the counting is done for as many NUMERIC and CHARACTER Variables.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If you know the numbers of numeric variables (say 15) and character variables (say 20) before running the data step, you can size the arrays as:&amp;nbsp;&lt;/P&gt;&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;   &lt;SPAN class="token statement"&gt;array&lt;/SPAN&gt; n_count&lt;SPAN class="token punctuation"&gt;[15&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;]&lt;/SPAN&gt; _temporary_ &lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;15&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;*&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;0&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt; &lt;SPAN class="token comment"&gt;* Assuming data set has no more than 100 OBS;&lt;/SPAN&gt;
   &lt;SPAN class="token statement"&gt;array&lt;/SPAN&gt; c_count&lt;SPAN class="token punctuation"&gt;[2&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;0&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;]&lt;/SPAN&gt; _temporary_ &lt;SPAN class="token punctuation"&gt;(2&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;0&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;*&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;0&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Hope this helps you.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 14 Jul 2016 20:49:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-missing-value-for-character-variable/m-p/284677#M58082</guid>
      <dc:creator>KachiM</dc:creator>
      <dc:date>2016-07-14T20:49:14Z</dc:date>
    </item>
    <item>
      <title>Re: count missing value for character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-missing-value-for-character-variable/m-p/284862#M58139</link>
      <description>OK, I see. Thank you!!! &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/17813"&gt;@KachiM&lt;/a&gt;</description>
      <pubDate>Fri, 15 Jul 2016 16:04:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-missing-value-for-character-variable/m-p/284862#M58139</guid>
      <dc:creator>Xiaoningdemao</dc:creator>
      <dc:date>2016-07-15T16:04:47Z</dc:date>
    </item>
  </channel>
</rss>

