DATA Step, Macro, Functions and more

Getting summary statistics without procfreq

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 9
Accepted Solution

Getting summary statistics without procfreq

The dataset looks like below. 

 

colx      coly    colz  

0           1          0      

0           1          1      

0           1          0       

 

Required output:

Colname      value    count

colx                0          3

coly                1          3

colz                0          2

colz                1           1

 

I have a code that works perfectly fine and here is how it looks.

ods output onewayfreqs=outfreq;

proc freq data= final;

tables colx coly colz /nocum nofreq;

run;

data freq;

retain colname column_value;

keep colname column_value  frequency percent;

set outfreq;

colname=scan(tables,2,' ');

column_Value=trim(left(vvaluex(colname)));

run;

 

The above code works. but i believe is not efficient. Say i have 1000 columns and running prof freq on all 1000 columns is not 

efficient. So is there any other efficient way with out using the proc freq that accomplishes my desired output. 

 

Thanks in advance.

colname

 

 


Accepted Solutions
Solution
‎02-07-2018 11:40 AM
PROC Star
Posts: 1,357

Re: Getting summary statistics without procfreq

I should have posted this last night, but i was too lazy

data have;
input colx      coly    colz  ;
datalines;
0           1          0      
0           1          1      
0           1          0  
;

data _null_;
 if _N_ = 1 then do;
 	length Colname $8 count 8;
     declare hash h(ordered:'y' );
     h.defineKey('Colname','value');
     h.defineData('Colname','value','count');
     h.defineDone( );
     call missing(value, count);
   end;
set have end=last;
array t(*) colx--colz;
do n=1 to dim(t);
 Colname=vname(t(n));
 value=t(n);
 if h.check() ne 0 then do;count=1;h.add();end;
 else if h.find()=0 then do; count=count+1;h.replace();end;
end;
if last then h.output(dataset:'want');
run;

@tej123  I hope you accept and mark one of anybody's response that you most prefer. The acknowledgement is what makes people like me interested to participate here. I guess it's only fair. Thank you! 

View solution in original post


All Replies
Trusted Advisor
Posts: 1,270

Re: Getting summary statistics without procfreq

Hi,

 

Something like this may produce the desired output.

 

data have;
input colx coly colz;
datalines;
0 1 0
0 1 1
0 1 0
;

 

data want(keep=name value);
set have;
array v(*) col:;
do i = 1 to dim(v);
    name=vname(v(i));
   value=v(i);
   output;
end;
run;


proc sql;
select name,value,count(*) as count from want
group by name, value;
quit;

Super User
Posts: 6,543

Re: Getting summary statistics without procfreq

I really don't know if this would work or not, but I can't test it until Monday.  I would experiment with having PROC TABULATE create an output data set:

 

proc tabulate data=have noprint out=experiment;

class colx coly colz;

tables colx coly colz;

run;

 

It might need to be tweaked, it might be tremendously successful, or it might fail miserably.  That's where I would start.

PROC Star
Posts: 1,357

Re: Getting summary statistics without procfreq

@tej123  I personally don't think proc freq is slowing your execution, rather your datastep. 

data have;
input colx      coly    colz  ;
datalines;
0           1          0      
0           1          1      
0           1          0  
;

proc transpose data=have out=_have;
var col:;
run;

proc transpose data=_have out=_have1;
by _name_;
var col:;
run; 

proc freq data=_have1;
by _name_;
tables col1/out=want(drop=percent);
run;
Trusted Advisor
Posts: 1,294

Re: Getting summary statistics without procfreq

As one of the other responses suggest, make a data set with NAME and VALUE.  But make a data set VIEW, not a data set FILE. Then submit it to proc summary:

 

data vneed (keep=name value) / view=vneed;
  set have;
  array nam {1000} $32 _temporary_;
  array v{*} col:;

  if _n_=1 then do i=1 to dim(v);
    nam{i}=vname(v{i});
  end;

  do i = 1 to dim(v);
   name=nam{i};
   value=v(i);
   output;
  end;
run;

proc summary data=vneed nway;
  class name value;
  output out=t (drop=_type_);
run;

Notes:

  1. The data set view is not activated until called by proc summary. As a result, the data in VNEED is not written to disk, but streamed directly to proc summary.
  2. Make the array NAM at least as big as the array V.
  3. Don't repeatedly call function VNAME X times for every obs. Do it once and retrieve the resulting names from the NAM array.
Solution
‎02-07-2018 11:40 AM
PROC Star
Posts: 1,357

Re: Getting summary statistics without procfreq

I should have posted this last night, but i was too lazy

data have;
input colx      coly    colz  ;
datalines;
0           1          0      
0           1          1      
0           1          0  
;

data _null_;
 if _N_ = 1 then do;
 	length Colname $8 count 8;
     declare hash h(ordered:'y' );
     h.defineKey('Colname','value');
     h.defineData('Colname','value','count');
     h.defineDone( );
     call missing(value, count);
   end;
set have end=last;
array t(*) colx--colz;
do n=1 to dim(t);
 Colname=vname(t(n));
 value=t(n);
 if h.check() ne 0 then do;count=1;h.add();end;
 else if h.find()=0 then do; count=count+1;h.replace();end;
end;
if last then h.output(dataset:'want');
run;

@tej123  I hope you accept and mark one of anybody's response that you most prefer. The acknowledgement is what makes people like me interested to participate here. I guess it's only fair. Thank you! 

Occasional Contributor
Posts: 9

Re: Getting summary statistics without procfreq

Posted in reply to novinosrin

all the solutions are great. But i really liked your one. Thanks. 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 209 views
  • 5 likes
  • 5 in conversation