Hello everyone,
I need to get Proc Freq report output as data set. I know that it is not possible to get the SAS procedures reports being data set with same structure. At this point, I need your help to create my desired output with alternative methods with alternative design. I created a sample data set as below and I need to get the results as data set in my following desired image.( As I mentioned as above, it shouldn't be in same view, It helps me to reach my goal)
If I get the data set then I need to make some additional calculation over consisted data set. I’m not sure whether I can do the following calculation or not.
I tried to express my calculation over the Excel cells. If I get the first output as data set maybe I can do the following calculation easily.
Frequency(1)/TotalFrequency(1)==>D6/D7==> 0.857143
Frequency(0)/TotalFrequency(0)==>C5/C7==> 0.8
Frequency(1)/Total==>D6/E6==>0.857143
(Frequency(0)+ Frequency(1))/(TotalFrequency(0)+ TotalFrequency(0))==> =(C5+D6)/(D7+C7) ==> 0.833333
(Frequency(1)+ Frequency(0))/(TotalFrequency(0)+ TotalFrequency(0))==> =(C6+D5)/(C7+D7)==> 0.166667
Data Have;
Length Target 8 Predicted 8;
Infile Datalines Missover;
Input Target Predicted;
Datalines;
1 0
1 1
1 1
0 0
0 0
0 0
0 0
0 1
1 1
1 1
1 1
1 1
;
Run;
PROC FREQ Data=Have;
Tables Target*Predicted / TotPct;
Ods Output OneWayFreqs=Want;
Run;
Thank you very much
Proc Tabulate will show the Total-Total cell with total
proc tabulate data=have;
class target predicted;
table (target all='Total')*n="Frequency",
predicted all='Total'
;
run;
Your probably best off writing it in SQL and then transposing it. You may also want to look into the sensitivity/specificity output available from proc freq.
http://support.sas.com/kb/24/170.html
Proc SQL;
Create table want as
Select
Sum(predicted=1 and target=1)/sum(predicted=1) as col1,
Sum(predicted=0 and target=0)/sum(predicted=0) as col2,
Etc....
From have:
Quit;
Hello Reeza,
Thank you for your suggestion, but I couldn't understand what should I do, sorry 😞
I also think to do some addtional steps instead of get the output directly. But I can't get proper data set to calculate "Total" columns.
Actually, If I used PROC TABULATE out= statement and then If I do PROC TRANSPASE, I get some kind of data set, if I get the following data set, I can do your calculation which you did in your previous data set. But I'm little bir confused 😞
Proc Tabulate DATA=Have Out=Have2;
Class Target /Order=Unformatted Missing;
Class Predicted /Order=Unformatted Missing;
Table Predicted, Target*N;
Run;
PROC TRANSPOSE DATA=Have2
OUT=Have3
PREFIX=Column
NAME=Source
LABEL=Label;
VAR N;
COPY Target Predicted;
RUN;
QUIT;
The foregoing code creates this data set,
The following output can provide me to calculate my desired output but I'm not use if it is the right method or if it is possible
Thank you,
Can Lütfü Yılmazer
Hi again Reeza,
You want me to do this calculation over the Raw data, I understood now. ı thought that you meant Output of Proc Freq .Let me try to reach my aim.
Thank you
Thank you very much Reeza,
The following code provides my desired output
PROC SQL;
CREATE TABLE Want AS
SELECT
Sum(Target=1 And Predicted=1)/((Sum(Target=1 And Predicted=1))+(Sum(Target=1 And Predicted=0))) As Sensitivity
,Sum(Target=0 And Predicted=0)/((Sum(Target=0 And Predicted=1))+(Sum(Target=0 And Predicted=0))) As Specificity
,Sum(Target=1 And Predicted=1)/(Sum((Target=1 And Predicted=1))+(Sum(Target=0 And Predicted=1))) As Precision
,((Sum(Target=1 And Predicted=1))+(Sum(Target=0 And Predicted=0)))/
((Sum(Target=0 And Predicted=1))+(Sum(Target=0 And Predicted=0))+(Sum(Target=1 And Predicted=1))+(Sum(Target=1 And Predicted=0))) As Accuracy
,((Sum(Target=1 And Predicted=0))+(Sum(Target=0 And Predicted=1)))/
((Sum(Target=0 And Predicted=1))+(Sum(Target=0 And Predicted=0))+(Sum(Target=1 And Predicted=1))+(Sum(Target=1 And Predicted=0))) As ErrorRate
FROM Have;
QUIT;
Lastly,
Is it possible to get just following report output values on PROC FREQ or PROC TABULATE? Not being data set, being Report
Thank you
It may be possible in proc tabulate but it would be difficult.
I would generate my dataset and then use proc report to display the data.
Proc Tabulate will show the Total-Total cell with total
proc tabulate data=have;
class target predicted;
table (target all='Total')*n="Frequency",
predicted all='Total'
;
run;
Vaov, ballardw, thank you very much this values are which I want.
To add some additional cosmetics in the report, is it possible to don't show the Frequency label or is it possible to show the frequency column for Predicted values?
To understand me better, you can check the following output.
Or
If I write empty string, borders are still came
proc tabulate data=Have;
class Target predicted;
table (Target all='Total')*n="",
predicted all='Total';
run;
,Thank you
turcay wrote:
Vaov, ballardw, thank you very much this values are which I want.
To add some additional cosmetics in the report, is it possible to don't show the Frequency label or is it possible to show the frequency column for Predicted values?
I will say that I attempted to make the output as you requested. If you had provided the different appearance earlier I would would have put the labels in column instead of row and used the / row=float option to suppress the empty cell for the row labels.
Ballardw,
You are right, I realized my additional demand after you provided me the code, sorry. The following code suppressed the empty cell, thank you.
proc tabulate data=Have;
class Target predicted;
table (Target all='Total')*n="",
predicted all='Total'
/Row=Float;
run;
Lastly, is it possible to create the following one?
,Thank you
Do you want both a Row labeled "Frequency" and a Column with "Frq" or just the column label?
Hello ballardw,
Your response met my demand, thank you and sorry for changing my request after you created my desired output.
Thank you,
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.