BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
tej123
Fluorite | Level 6

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

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

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

6 REPLIES 6
stat_sas
Ammonite | Level 13

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;

Astounding
PROC Star

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.

novinosrin
Tourmaline | Level 20

@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;
mkeintz
PROC Star

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.
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
novinosrin
Tourmaline | Level 20

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! 

tej123
Fluorite | Level 6

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

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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