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
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!
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;
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.
@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;
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:
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!
all the solutions are great. But i really liked your one. Thanks.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.