I am trying to learn SAS and specifically PROC REPORT. I am using SASHELP.CARS dataset.
What I want to achieve in the 6th column of the output, labelled as 'Number of Cars > Mean(Invoice)' to compute number of cars whose Invoice is greater than the Group's mean of Invoice. I am using the code below.
PROC REPORT DATA=sashelp.CARS NOWD OUT=learning.MyFirstReport;
COLUMNS Type Origin INVOICE=Max_INVOICE INVOICE=Mean_Invoice
INVOICE=Count_Invoice TEST DriveTrain;
DEFINE Type / Group 'Type of Car' CENTER;
DEFINE Origin / Group 'Origin of Car' CENTER;
DEFINE Max_Invoice / ANALYSIS MAX 'Max of Invoice';
DEFINE Mean_Invoice / ANALYSIS MEAN 'Mean of Invoice';
DEFINE Count_Invoice / ANALYSIS N FORMAT=5.0 'Total Number of Cars' center;
DEFINE DriveTrain / ACROSS 'Type of DriveTrain of Car';
DEFINE TEST / COMPUTED 'Number of Cars > Mean(Invoice)' center;
COMPUTE TEST;
TEST=N(_c7_>Mean_Invoice);
ENDCOMP;
RUN;
The Output that I am getting is in the image below.
I don't think that is the correct output since all the rows in the column show a value of 1. How do I get the desired output in the 6th column of the output?
Hi, there is an easy way to get this number, but it requires that EVERY row in SASHELP.CARS is compared to the overall invoice mean. You can do this but you need a little help from other procedures....probably PROC SQL or PROC MEANS. I prefer PROC MEANS, since I avoid remerging statistics back, which is what SQL would do. I prefer to be in control of the MERGE:
And here's the output:
Cynthia
Hi!
I realised my mistake. I thought that _c7_ is a way to refer to the 7th column in the Original (incoming) dataset but I waswrong. _c7_, I think is the 7th column in the output of PROC REPORT. Thank you for correcting me.
It is sad that such a value can't be computed. It would be nice to know for each Type and Origin combination, how many cars' invoice was over that specific group combination's Mean(Invoice). I thought there is an easy way to get that but looks like I was wrong on this one too ..
Hi, there is an easy way to get this number, but it requires that EVERY row in SASHELP.CARS is compared to the overall invoice mean. You can do this but you need a little help from other procedures....probably PROC SQL or PROC MEANS. I prefer PROC MEANS, since I avoid remerging statistics back, which is what SQL would do. I prefer to be in control of the MERGE:
And here's the output:
Cynthia
Thank you Cynthia for the example.
Thank you very much. I have been staring at the screen for some time now, in search of a solution. Really appreciate.
I had asked the same question on Stackoverflow as well. I saw that reply today. I guess in both the places, the initial logic of adding a flag column is there (if I am not wrong). I am writing that link for cross reference if somebody needs it. https://stackoverflow.com/questions/52351214/sas-proc-report-conditional-output-in-a-column/52382463...
That particular code produces output as below.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.