<?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 Create table for Missing and Filling Rate in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Create-table-for-Missing-and-Filling-Rate/m-p/597208#M172068</link>
    <description>&lt;P&gt;Hi, my data have more than 1,000 variables and I need to check for missing and filling rate for each variable.&amp;nbsp;&lt;/P&gt;&lt;P&gt;My aim is to output a sas table which lists all variables, along with number of missing, non missing and the rate of missing and non missing.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Sample data as per below:&lt;/P&gt;&lt;PRE&gt;data missingval;&lt;BR /&gt;length Country $ 10 TourGuide $ 10 Trip_Num 8;&lt;BR /&gt;input Country TourGuide Trip_Num;&lt;BR /&gt;datalines;&lt;BR /&gt;Japan Yamada 3&lt;BR /&gt;. Militello .&lt;BR /&gt;Australia Edney 4&lt;BR /&gt;Venezuela . 2&lt;BR /&gt;Brazil Cardoso .&lt;BR /&gt;;run;&lt;/PRE&gt;&lt;P&gt;Expected output:&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Variable_Name&lt;/TD&gt;&lt;TD&gt;Variable_Type&lt;/TD&gt;&lt;TD&gt;Fill_Num&lt;/TD&gt;&lt;TD&gt;Miss_Num&lt;/TD&gt;&lt;TD&gt;Fill_Rate&lt;/TD&gt;&lt;TD&gt;Miss_Rate&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Country&lt;/TD&gt;&lt;TD&gt;Char&lt;/TD&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;0.8&lt;/TD&gt;&lt;TD&gt;0.2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;TourGuide&lt;/TD&gt;&lt;TD&gt;Char&lt;/TD&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;0.8&lt;/TD&gt;&lt;TD&gt;0.2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Trip_Num&lt;/TD&gt;&lt;TD&gt;Num&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;0.6&lt;/TD&gt;&lt;TD&gt;0.4&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Really appreciate your help on this!&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 17 Oct 2019 08:28:06 GMT</pubDate>
    <dc:creator>AieuYuhara</dc:creator>
    <dc:date>2019-10-17T08:28:06Z</dc:date>
    <item>
      <title>Create table for Missing and Filling Rate</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-table-for-Missing-and-Filling-Rate/m-p/597208#M172068</link>
      <description>&lt;P&gt;Hi, my data have more than 1,000 variables and I need to check for missing and filling rate for each variable.&amp;nbsp;&lt;/P&gt;&lt;P&gt;My aim is to output a sas table which lists all variables, along with number of missing, non missing and the rate of missing and non missing.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Sample data as per below:&lt;/P&gt;&lt;PRE&gt;data missingval;&lt;BR /&gt;length Country $ 10 TourGuide $ 10 Trip_Num 8;&lt;BR /&gt;input Country TourGuide Trip_Num;&lt;BR /&gt;datalines;&lt;BR /&gt;Japan Yamada 3&lt;BR /&gt;. Militello .&lt;BR /&gt;Australia Edney 4&lt;BR /&gt;Venezuela . 2&lt;BR /&gt;Brazil Cardoso .&lt;BR /&gt;;run;&lt;/PRE&gt;&lt;P&gt;Expected output:&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Variable_Name&lt;/TD&gt;&lt;TD&gt;Variable_Type&lt;/TD&gt;&lt;TD&gt;Fill_Num&lt;/TD&gt;&lt;TD&gt;Miss_Num&lt;/TD&gt;&lt;TD&gt;Fill_Rate&lt;/TD&gt;&lt;TD&gt;Miss_Rate&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Country&lt;/TD&gt;&lt;TD&gt;Char&lt;/TD&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;0.8&lt;/TD&gt;&lt;TD&gt;0.2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;TourGuide&lt;/TD&gt;&lt;TD&gt;Char&lt;/TD&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;0.8&lt;/TD&gt;&lt;TD&gt;0.2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Trip_Num&lt;/TD&gt;&lt;TD&gt;Num&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;0.6&lt;/TD&gt;&lt;TD&gt;0.4&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Really appreciate your help on this!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 17 Oct 2019 08:28:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-table-for-Missing-and-Filling-Rate/m-p/597208#M172068</guid>
      <dc:creator>AieuYuhara</dc:creator>
      <dc:date>2019-10-17T08:28:06Z</dc:date>
    </item>
    <item>
      <title>Re: Create table for Missing and Filling Rate</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-table-for-Missing-and-Filling-Rate/m-p/597211#M172069</link>
      <description>&lt;P&gt;Use a double transpose, followed by a summarizing data step that merges the types in:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data missingval;
length Country $ 10 TourGuide $ 10 Trip_Num 8;
input Country TourGuide Trip_Num;
datalines;
Japan Yamada 3
. Militello .
Australia Edney 4
Venezuela . 2
Brazil Cardoso .
;

proc sql;
create table types as
select name as _name_, type
from dictionary.columns
where libname = 'WORK' and memname = 'MISSINGVAL';
quit;

proc transpose data=missingval out=trans;
var _all_;
run;

proc transpose data=trans out=freq;
by _name_;
var col:;
run;

data want (rename=(_name_=variable_name));
length _name_ $32; * prevents WARNING;
merge
  freq
  types
;
by _name_;
if first._name_
then do;
  fill_num = 0;
  miss_num = 0;
end;
if strip(col1) not in (' ','.')
then fill_num + 1;
else miss_num + 1;
if last._name_;
fill_rate = fill_num / (fill_num + miss_num);
miss_rate = miss_num / (fill_num + miss_num);
keep _name_ type fill_num miss_num fill_rate miss_rate;
run;

proc print data=want noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;variable_                                    fill_    miss_
  name       type    fill_num    miss_num     rate     rate

Country      char        4           1        0.8      0.2 
TourGuide    char        4           1        0.8      0.2 
Trip_Num     num         3           2        0.6      0.4 
&lt;/PRE&gt;</description>
      <pubDate>Thu, 17 Oct 2019 08:45:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-table-for-Missing-and-Filling-Rate/m-p/597211#M172069</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-10-17T08:45:04Z</dc:date>
    </item>
    <item>
      <title>Re: Create table for Missing and Filling Rate</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-table-for-Missing-and-Filling-Rate/m-p/597214#M172070</link>
      <description>&lt;P&gt;alternately please try&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data missingval;
length Country $ 10 TourGuide $ 10 Trip_Num 8;
input Country TourGuide Trip_Num;
datalines;
Japan Yamada 3
. Militello .
Australia Edney 4
Venezuela . 2
Brazil Cardoso .
;
run;

proc transpose data=missingval out=test;
var Country TourGuide Trip_Num;
run;

data want;
set test;
array cols(*) col1-col5;
do i = 1 to dim(cols);
obs=sum(i);
if compress(cols(i))='.' then cols(i)=' ';
if compress(cols(i))^=' ' then cols(i)='1';
end;
miss_Num=cmiss(of col1-col5);
fill_Num=sum(of col1-col5);
fill_rate=fill_Num/obs;
miss_rate=miss_Num/obs;
drop col1-col5;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 17 Oct 2019 08:55:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-table-for-Missing-and-Filling-Rate/m-p/597214#M172070</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2019-10-17T08:55:34Z</dc:date>
    </item>
    <item>
      <title>Re: Create table for Missing and Filling Rate</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-table-for-Missing-and-Filling-Rate/m-p/597225#M172074</link>
      <description>&lt;P&gt;Mind that both methods require you to have enough memory to store a number of variables equal to the number of observations in your original dataset during the transpose.&lt;/P&gt;</description>
      <pubDate>Thu, 17 Oct 2019 09:35:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-table-for-Missing-and-Filling-Rate/m-p/597225#M172074</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-10-17T09:35:19Z</dc:date>
    </item>
    <item>
      <title>Re: Create table for Missing and Filling Rate</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-table-for-Missing-and-Filling-Rate/m-p/597238#M172077</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Mind that both methods require you to have enough memory to store a number of variables equal to the number of observations in your original dataset during the transpose.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;So, do you think that solution skipping transposing would be better?&lt;/P&gt;
&lt;P&gt;That one got a bit lengthy, but i am out of time right now.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
   select quote(trim(Name)) into :varList separated by ','
      from sashelp.vcolumn
         where LibName = 'WORK' and MemName = 'MISSINGVAL'
   ;
quit;


data _null_;
   set missingval end=lastObs;

   length 
      Variable_Name $ 32
      Variable_Type $ 4
      Fill_Num Miss_Num 8
      _isMissing 8
   ;

   if _n_ = 1 then do;
      declare hash h(ordered:'yes');
      declare hiter bob('h');
      h.defineKey('Variable_Name');
      h.defineData('Variable_Name', 'Variable_Type', 'Fill_Num', 'Miss_Num');
      h.defineDone();

      do Variable_Name = &amp;amp;varList;
         Variable_Type = vtypex(Variable_Name);
         Fill_Num = 0;
         Miss_Num = 0;
         h.add();
      end;
   end;

   do Variable_Name = &amp;amp;varList;
      _rc = h.find();

      if Variable_Type = 'C' then do;
         _isMissing = missing(vvaluex(Variable_Name));
      end;
      else do;
         _isMissing = (strip(vvaluex(Variable_Name)) = '.');
      end;
      
      Fill_Num = sum(Fill_Num, (not _isMissing));
      Miss_Num = sum(Miss_Num, _isMissing);

      h.replace();
   end;

   if lastObs then do;
      h.output(dataset: 'want');
   end;
run;


data work.want;
   set work.want;

   length Fill_Rate Miss_Rate 8;

   Fill_Rate = Fill_Num / (Fill_Num + Miss_Num);
   Miss_Rate = Miss_Num / (Fill_Num + Miss_Num);
run;

proc print data=work.want;run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 17 Oct 2019 10:25:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-table-for-Missing-and-Filling-Rate/m-p/597238#M172077</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2019-10-17T10:25:00Z</dc:date>
    </item>
    <item>
      <title>Re: Create table for Missing and Filling Rate</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-table-for-Missing-and-Filling-Rate/m-p/597240#M172078</link>
      <description>&lt;P&gt;Nice. One of the things that work fine with a hash. And it is equally free of any assumptions as my double transpose code.&lt;/P&gt;
&lt;P&gt;And it will outperform the double transpose quite well with increasing dataset size.&lt;/P&gt;</description>
      <pubDate>Thu, 17 Oct 2019 10:38:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-table-for-Missing-and-Filling-Rate/m-p/597240#M172078</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-10-17T10:38:59Z</dc:date>
    </item>
    <item>
      <title>Re: Create table for Missing and Filling Rate</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-table-for-Missing-and-Filling-Rate/m-p/597301#M172082</link>
      <description>&lt;PRE&gt;data missingval;
length Country $ 10 TourGuide $ 10 Trip_Num 8;
input Country TourGuide Trip_Num;
datalines;
Japan Yamada 3
. Militello .
Australia Edney 4
Venezuela . 2
Brazil Cardoso .
;run;

data want;
 set sashelp.vcolumn(keep=libname memname name type
 where=(lowcase(libname)='work' and lowcase(memname)='missingval')) end=last;
 if _n_=1 then call execute('proc sql;create table want as');
 call execute(catt('select "',name,'" as name length=40,"',type,'" as type length=10,
 n(',name,') as fill_n,nmiss(',name,') as miss_n,
 calculated fill_n/count(*) as Fill_Rate,1- calculated Fill_Rate as Miss_Rate 
 from missingval'));
if last then call execute(';quit;');
 else call execute('union');
run;&lt;/PRE&gt;</description>
      <pubDate>Thu, 17 Oct 2019 12:43:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-table-for-Missing-and-Filling-Rate/m-p/597301#M172082</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2019-10-17T12:43:42Z</dc:date>
    </item>
    <item>
      <title>Re: Create table for Missing and Filling Rate</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-table-for-Missing-and-Filling-Rate/m-p/597578#M172199</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15475"&gt;@andreas_lds&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;It's actually not that lengthy, and because of the hash dynamics can be done in a single step:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;                                                                                                                             
  input (country tourguide) (:$10.) trip_num ;                                                                                          
  cards ;                                                                                                                               
Japan     Yamada    3                                                                                                                   
.         Militello .                                                                                                                   
Australia Edney     4                                                                                                                   
Venezuela .         2                                                                                                                   
Brazil    Cardoso   .                                                                                                                   
;                                                                                                                                       
run ;                                                                                                                                   
                                                                                                                                        
data want (drop = country tourguide trip_num)  ;                                                                                        
  if _n_ = 1 then do ;                                                                                                                  
    dcl hash h () ;                                                                                                                     
    h.definekey  ("variable_name") ;                                                                                                    
    h.definedata ("variable_name", "variable_type", "fill_num") ;                                                                       
    h.definedone () ;                                                                                                                   
    dcl hiter hi ("h") ;                                                                                                                
  end ;                                                                                                                                 
  set have nobs = n end = z ;                                                                                                           
  array cc _char_ ;                                                                                                                     
  array nn _numeric_ ;                                                                                                                  
  length variable_name $ 32 variable_type $ 4 ;                                                                                         
  call missing (variable_name, variable_type) ;                                                                                         
  do over nn ;                                                                                                                          
    if h.find (key:vname(nn)) ne 0 then fill_num = 0 ;                                                                                  
    fill_num + not cmiss(nn) ;                                                                                                          
    h.replace (key:vname(nn), data:vname(nn), data:vtype(nn), data:fill_num) ;                                                          
  end ;                                                                                                                                 
  do over cc ;                                                                                                                          
    if h.find (key:vname(cc)) ne 0 then fill_num = 0 ;                                                                                  
    fill_num + not cmiss(cc) ;                                                                                                          
    h.replace (key:vname(cc), data:vname(cc), data:vtype(cc), data:fill_num) ;                                                          
  end ;                                                                                                                                 
  if z then do while (hi.next() = 0) ;                                                                                                  
    variable_type = ifc (variable_type eq "N", "Num", "Char") ;                                                                         
    miss_num = n - fill_num ;                                                                                                           
    miss_rate = divide (miss_num, n) ;                                                                                                  
    output ;                                                                                                                            
  end ;                                                                                                                                 
run ;                      
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 18 Oct 2019 00:19:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-table-for-Missing-and-Filling-Rate/m-p/597578#M172199</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-10-18T00:19:17Z</dc:date>
    </item>
    <item>
      <title>Re: Create table for Missing and Filling Rate</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-table-for-Missing-and-Filling-Rate/m-p/597593#M172203</link>
      <description>&lt;P&gt;Thanks for your help!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;All the codes given by others are working too, and i ran your codes, it works way faster, and I do like it when we dont have to specify length or dropping any variables, because it wont help when working with more than 1,000 variables.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you everyone!&lt;/P&gt;</description>
      <pubDate>Fri, 18 Oct 2019 02:53:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-table-for-Missing-and-Filling-Rate/m-p/597593#M172203</guid>
      <dc:creator>AieuYuhara</dc:creator>
      <dc:date>2019-10-18T02:53:22Z</dc:date>
    </item>
  </channel>
</rss>

