<?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 count non-missing rows for *all* columns and store results in a single data set in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-non-missing-rows-for-all-columns-and-store-results/m-p/404750#M278965</link>
    <description>&lt;P&gt;Thank you everyone for your responses, this was really helpful! I've some great ideas to go away and try now.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Adam&lt;/P&gt;</description>
    <pubDate>Tue, 17 Oct 2017 11:32:10 GMT</pubDate>
    <dc:creator>adam_t</dc:creator>
    <dc:date>2017-10-17T11:32:10Z</dc:date>
    <item>
      <title>How to count non-missing rows for *all* columns and store results in a single data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-non-missing-rows-for-all-columns-and-store-results/m-p/403982#M278958</link>
      <description>&lt;P&gt;Hi everyone,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am trying to find an elegant way to collect some metadata for all columns in a data set. What I want to know is, &lt;STRONG&gt;for each and every variable in a data set, how many rows are populated&lt;/STRONG&gt;? I would like the result of this query to be output into a single data set which contains one row per variable in the original data, and a column giving the number of non-missing rows for that variable. Here's some example data and output:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data demo_data;
  infile datalines dsd truncover;
  input Make:$13. Model:$40. Type:$8. Origin:$6. DriveTrain:$5. MSRP:DOLLAR8. Invoice:DOLLAR8. EngineSize:32. Cylinders:32. Horsepower:32. MPG_City:32. MPG_Highway:32. Weight:32. Wheelbase:32. Length:32.;
datalines4;
Acura,,SUV,Asia,All,"$36,945","$33,337",3.5,6,,17,23,4451,106,189
Acura,RSX Type S 2dr,Sedan,Asia,Front,"$23,820","$21,761",2,4,,24,31,2778,101,172
Acura,TSX 4dr,Sedan,Asia,Front,"$26,990","$24,647",2.4,4,,22,29,3230,105,183
Acura,TL 4dr,Sedan,Asia,Front,"$33,195","$30,299",,6,270,20,28,3575,108,186
Acura,3.5 RL 4dr,Sedan,Asia,Front,"$43,755","$39,014",,6,225,18,24,3880,115,197
;;;;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Desired output:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;name&lt;/TD&gt;&lt;TD&gt;num_not_missing&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Cylinders&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;DriveTrain&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;EngineSize&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Horsepower&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Invoice&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Length&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;MPG_City&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;MPG_Highway&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;MSRP&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Make&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Model&lt;/TD&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Origin&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Type&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Weight&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Wheelbase&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;... where I have inserted missing values in the demo dataset&amp;nbsp;and the resulting metadata shows&amp;nbsp;that some columns contain missing values.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I do have a solution to this problem already, but I would like to know &lt;STRONG&gt;whether there is a more elegant or SAS-like way to get the&amp;nbsp;same results&lt;/STRONG&gt; (if only to satisfy my own curiosity!). My current (inelegant) solution is as follows:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc contents data=demo_data out=cont_demo_data(keep=name varnum type length) noprint; run;
proc sql noprint;
	select catx(" ", "count(", name, ") as ", name)
	into :list separated by ', '
	from cont_demo_data;
quit;
proc sql;
	create table counts_demo_data as
	select
		&amp;amp;list.
	from demo_data;
quit;
proc transpose 
	data = counts_demo_data
	out = counts_demo_data_t(rename=(_NAME_=name COL1=num_not_missing));
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;So as you can see, it's somewhat inelegant but it does at least get to the correct answer.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Other approaches I've tried that don't give me the result I need:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;Using proc means as this will only work for numeric variables&lt;/LI&gt;&lt;LI&gt;proc freq with _NUMERIC_ or _CHAR_ in the tables statement - this gives a separate table for each variable, not all in the same table&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;Thanks!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Adam&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;p.s. I'm using base SAS 9.2&lt;/P&gt;</description>
      <pubDate>Fri, 13 Oct 2017 15:34:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-count-non-missing-rows-for-all-columns-and-store-results/m-p/403982#M278958</guid>
      <dc:creator>adam_t</dc:creator>
      <dc:date>2017-10-13T15:34:53Z</dc:date>
    </item>
    <item>
      <title>Re: How to count non-missing rows for *all* columns and store results in a single data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-non-missing-rows-for-all-columns-and-store-results/m-p/403985#M278959</link>
      <description>&lt;P&gt;In SAS 9.2 that's probably as elegant as you're going to get, and even in 9.4.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The solution I was going to suggest, isn't a succinct. The only risk is for big data sets, where the macro variable may be longer than the 32K character limit.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://gist.github.com/statgeek/2de1faf1644dc8160fe721056202f111" target="_blank"&gt;https://gist.github.com/statgeek/2de1faf1644dc8160fe721056202f111&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Full non macro solution&lt;/P&gt;</description>
      <pubDate>Fri, 13 Oct 2017 15:39:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-count-non-missing-rows-for-all-columns-and-store-results/m-p/403985#M278959</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-10-13T15:39:20Z</dc:date>
    </item>
    <item>
      <title>Re: How to count non-missing rows for *all* columns and store results in a single data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-non-missing-rows-for-all-columns-and-store-results/m-p/404086#M278960</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data= your_data ;
var _numeric_ ;
output nmiss= ;
proc transpose data= _last_( drop= _FREQ_ _TYPE_ )
         out= nmiss_counts(rename= col1 = nmiss ) ;
run ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;should work in any old release&lt;/P&gt;
&lt;P&gt;unless you have columns in your_data&amp;nbsp; named _type_ or _freq_&amp;nbsp; --&amp;gt; then I wouldn't know&lt;/P&gt;</description>
      <pubDate>Fri, 13 Oct 2017 19:31:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-count-non-missing-rows-for-all-columns-and-store-results/m-p/404086#M278960</guid>
      <dc:creator>Peter_C</dc:creator>
      <dc:date>2017-10-13T19:31:31Z</dc:date>
    </item>
    <item>
      <title>Re: How to count non-missing rows for *all* columns and store results in a single data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-non-missing-rows-for-all-columns-and-store-results/m-p/404094#M278961</link>
      <description>&lt;PRE&gt;proc summary data= your_data ;
var _numeric_ ;
output n= ;
proc transpose data= _last_( drop= _FREQ_ _TYPE_ )
       out= non_miss_counts(rename= col1 = n ) ;
run ;&lt;/PRE&gt;
&lt;P&gt;revised to provide counts of non-missing (proc means / summary statistic N) rather than the NMISS statistic&lt;/P&gt;</description>
      <pubDate>Fri, 13 Oct 2017 19:37:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-count-non-missing-rows-for-all-columns-and-store-results/m-p/404094#M278961</guid>
      <dc:creator>Peter_C</dc:creator>
      <dc:date>2017-10-13T19:37:05Z</dc:date>
    </item>
    <item>
      <title>Re: How to count non-missing rows for *all* columns and store results in a single data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-non-missing-rows-for-all-columns-and-store-results/m-p/404142#M278962</link>
      <description>&lt;P&gt;Based on your data demo_data, I built an 'elegant'&amp;nbsp;macro to give you the desired output dataset.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The basic idea is to 1) use sas sql to count the number of&amp;nbsp;non-missing values; 2) use union to concatenate the names and the non-missing values; 3) use macro and do-loop to go through the variable list and repeat the step1 and 2.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So, as shown in the codes, I first&amp;nbsp;created the variable list, and then calculate the length of the list&amp;nbsp;that will be used for do-loop based on the position of each variable. With the help of do-loop, I combined each variable name and its non-missing value into one dataset, starting from the second variable (that's why I used 'if then' to control the loop). And voila. below is the output dataset.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Non-missing value dataset" style="width: 216px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/15890iA7D4EE107F58A08A/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture1013.PNG" alt="Non-missing value dataset" /&gt;&lt;span class="lia-inline-image-caption" onclick="event.preventDefault();"&gt;Non-missing value dataset&lt;/span&gt;&lt;/span&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let varlist=Make#Model#Type#Origin#DriveTrain#MSRP#Invoice#EngineSize#Cylinders#Horsepower#MPG_City#MPG_Highway#Weight#Wheelbase#Length;                                                                                         
                                                                                                                                                                                                                                  
%macro nmiss;                                                                                                                                                                                                                     
%let varnum=%eval(1+(%length(%sysfunc(compbl(&amp;amp;varlist))) - %length(%sysfunc(compress(&amp;amp;varlist,'#')))));                                                                                                                           
proc sql;                                                                                                                                                                                                                         
create table nmiss as                                                                                                                                                                                                             
%do i= 1 %to &amp;amp;varnum.;                                                                                                                                                                                                            
%if &amp;amp;i ne 1 %then union;                                                                                                                                                                                                          
select "%scan(&amp;amp;varlist.,&amp;amp;i,'#')" as name, count(%scan(&amp;amp;varlist.,&amp;amp;i,'#'))  as num_not_missing                                                                                                                                      
from demo_data                                                                                                                                                                                                                    
%end;                                                                                                                                                                                                                             
;                                                                                                                                                                                                                                 
quit;                                                                                                                                                                                                                             
%mend;                                                                                                                                                                                                                            
%nmiss; &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Well, if you are not comfortable with macro,&amp;nbsp; you can also&amp;nbsp;use the following codes in sas sql. The coding&amp;nbsp;could be&amp;nbsp;long depending on the number of the variables, but the idea is really straightforward.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example:&lt;/P&gt;&lt;P&gt;select 'Make' as name, count(Make) as num_not_missing from demo_data&lt;/P&gt;&lt;P&gt;union&lt;/P&gt;&lt;P&gt;select 'Model' as name, count(Model) as num_not_missing from demo_data&lt;/P&gt;&lt;P&gt;union&lt;/P&gt;&lt;P&gt;...&lt;/P&gt;</description>
      <pubDate>Fri, 13 Oct 2017 21:04:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-count-non-missing-rows-for-all-columns-and-store-results/m-p/404142#M278962</guid>
      <dc:creator>linkinsean</dc:creator>
      <dc:date>2017-10-13T21:04:32Z</dc:date>
    </item>
    <item>
      <title>Re: How to count non-missing rows for *all* columns and store results in a single data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-non-missing-rows-for-all-columns-and-store-results/m-p/404266#M278963</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/156966"&gt;@adam_t&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;Here a data step approach which should work for SAS 9.2 and for any number of variables in your source data set.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data demo_data;
  infile datalines dsd truncover;
  input Make:$13. Model:$40. Type:$8. Origin:$6. DriveTrain:$5. MSRP:DOLLAR8. Invoice:DOLLAR8. EngineSize:32. Cylinders:32. Horsepower:32. MPG_City:32. MPG_Highway:32. Weight:32. Wheelbase:32. Length:32.;
datalines4;
Acura,,SUV,Asia,All,"$36,945","$33,337",3.5,6,,17,23,4451,106,189
Acura,RSX Type S 2dr,Sedan,Asia,Front,"$23,820","$21,761",2,4,,24,31,2778,101,172
Acura,TSX 4dr,Sedan,Asia,Front,"$26,990","$24,647",2.4,4,,22,29,3230,105,183
Acura,TL 4dr,Sedan,Asia,Front,"$33,195","$30,299",,6,270,20,28,3575,108,186
Acura,3.5 RL 4dr,Sedan,Asia,Front,"$43,755","$39,014",,6,225,18,24,3880,115,197
;;;;
run;

data _null_;
  set demo_data end=last;
  array _c_var {*} _character_;
  array _n_var {*} _numeric_;

  /* hash to collect variable stats */
  if _n_=1 then 
    do;
      length name $32 num_not_missing 8;
      dcl hash h1(ordered:'y');
      h1.defineKey('name');
      h1.defineData('name','num_not_missing');
      h1.defineDone();
    end;

  /* stats for char vars */
  do i=1 to dim(_c_var);
    name=vname(_c_var[i]);
    num_not_missing=0;
    if h1.find() ne 0 then h1.add(); 
    if not missing(_c_var[i]) then 
      do;
        num_not_missing= sum(num_not_missing, 1);
        _rc=h1.replace();
      end;
  end;

  /* stats for num vars */
  do i=1 to dim(_n_var);
    name=vname(_n_var[i]);
    num_not_missing=0;
    if h1.find() ne 0 then h1.add(); 
    if not missing(_n_var[i]) then 
      do;
        num_not_missing= sum(num_not_missing, 1);
        _rc=h1.replace();
      end;
  end;

  /* write stats to table want */
  if last then h1.output(dataset:'want');

run;

proc print data=want noobs;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 15 Oct 2017 03:43:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-count-non-missing-rows-for-all-columns-and-store-results/m-p/404266#M278963</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2017-10-15T03:43:00Z</dc:date>
    </item>
    <item>
      <title>Re: How to count non-missing rows for *all* columns and store results in a single data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-non-missing-rows-for-all-columns-and-store-results/m-p/404312#M278964</link>
      <description>&lt;P&gt;First, it is possible to use PROC FREQ to generate a single dataset with all the variables and their frequencies.&amp;nbsp; But instead of the OUTPUT statement, you have to use ODS OUTPUT:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format;
  value nmis .='.' other='x';
  value $cmis ' '='.' other='x';
run;

ods output onewayfreqs=freq (keep=table frequency f_:);
ods listing close;
proc freq data=demo_data ;
  tables _all_ / missing ;
  format _numeric_ nmis.  _character_ $cmis.;
run;
ods listing;
ods output close;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But I think your program is more compact.&amp;nbsp; And you can make it even&amp;nbsp;more compact by dropping the PROC CONTENTS, and having the first proc sql read from dictionary.columns:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
	select catx(" ", "count(", name, ") as ", name)
	into :list separated by ', '
	from dictionary.columns 
          where libname='WORK' and memname='DEMO_DATA';
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And I suppose you can put the CREATE TABLE statement in the first&amp;nbsp;PROC SQL step.&lt;/P&gt;</description>
      <pubDate>Sun, 15 Oct 2017 18:11:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-count-non-missing-rows-for-all-columns-and-store-results/m-p/404312#M278964</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-10-15T18:11:03Z</dc:date>
    </item>
    <item>
      <title>Re: How to count non-missing rows for *all* columns and store results in a single data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-non-missing-rows-for-all-columns-and-store-results/m-p/404750#M278965</link>
      <description>&lt;P&gt;Thank you everyone for your responses, this was really helpful! I've some great ideas to go away and try now.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Adam&lt;/P&gt;</description>
      <pubDate>Tue, 17 Oct 2017 11:32:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-count-non-missing-rows-for-all-columns-and-store-results/m-p/404750#M278965</guid>
      <dc:creator>adam_t</dc:creator>
      <dc:date>2017-10-17T11:32:10Z</dc:date>
    </item>
  </channel>
</rss>

