BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
turcay
Lapis Lazuli | Level 10

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;

Desiredv3.png

 

Thank you very much

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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;

View solution in original post

12 REPLIES 12
Reeza
Super User

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;
turcay
Lapis Lazuli | Level 10

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,

 

Current.png

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 

 

Intermediate.png

 

Thank you,

 

Can Lütfü Yılmazer

turcay
Lapis Lazuli | Level 10

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

turcay
Lapis Lazuli | Level 10

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

 

Output.png

Thank you

Reeza
Super User

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. 

 

 

ballardw
Super User

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;
turcay
Lapis Lazuli | Level 10

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.

 

1st.png

Or

4th.png

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

Cynthia_sas
SAS Super FREQ
Hi:
I believe you are missing a TABULATE option. Generally, ROW=FLOAT will get rid of the "blank" header, such as you point to with your arrow. Please refer to this documenation example:
http://support.sas.com/documentation/cdl/en/proc/68954/HTML/default/viewer.htm#n0fvrniil70dmin1r4kee...

cynthia
ballardw
Super User

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.

 

turcay
Lapis Lazuli | Level 10

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?

original.png

,Thank you

ballardw
Super User

Do you want both a Row labeled "Frequency" and a Column with "Frq" or just the column label?

turcay
Lapis Lazuli | Level 10

Hello ballardw,

 

Your response met my demand, thank you and sorry for changing my request after you created my desired output.

 

Thank you,

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 12 replies
  • 1805 views
  • 8 likes
  • 4 in conversation