Hi,
I am new to SAS and I have the below code where the output is written to a new data set called 'totals'. Instead of creating a new data set 'totals', I want to write this to a file called 'totals.pdf' using only FILE statement and only ODS statement. Please, advice how to write using (i) FILE statement (ii) ODS
Here is the code that writes to the data set 'totals':
data sales;
infile 'H:\SAS Training\MyPractice\datafiles\Flowers_proc_means.dat';
input CustomerID $ @9 SaleDate MMDDYY10. Petunia SnapDragon Marigold;
proc sort data=sales;
by CustomerID;
proc means data=sales noprint;
by CustomerID;
var Petunia SnapDragon Marigold;
output out=totals SUM(Petunia SnapDragon Marigold)= Petunia_Sum SnapDragon_Sum Marigold_Sum
MEAN(Petunia SnapDragon Marigold)=Petunia_Mean SnapDragon_Mean Marigold_Mean;
proc print data=totals;
Title 'Sum of Flowers Data over customer ID';
Format Petunia_Mean SnapDragon_Mean Marigold_Mean 3.;
run;
Thanks
I showed you the code that produces a PDF file.
You could also just do a PROC PRINT of data set TOTALS
ods pdf file='yourfilename.pdf';
proc print data=totals;
run;
ods pdf close;
ods pdf file='yourfilename.pdf' n sum mean;
proc means data=sales;
by CustomerID;
var Petunia SnapDragon Marigold;
run;
ods pdf close;
Please note that it is usually a good idea to end your PROCs and DATA steps with a RUN; statement — although a few PROCs should end with a QUIT; statement. But always end your PROCs with either RUN; or QUIT; as appropriate.
Hi Paige Miller,
Thank you very much for your advise. The The output of my code where the output is written to data set 'totals' contains the output of as shown below:
SUM(Petunia SnapDragon Marigold)= Petunia_Sum SnapDragon_Sum Marigold_Sum
MEAN(Petunia SnapDragon Marigold)=Petunia_Mean SnapDragon_Mean Marigold_Mean;
756-01 | 0 | 3 | 305 | 350 | 285 | 102 | 117 | 95 |
834-01 | 0 | 2 | 170 | 220 | 160 | 85 | 110 | 80 |
901-02 | 0 | 2 | 110 | 160 | 135 | 55 | 80 | 68 |
How to get the same output in pdf file? Can you please advise?
Thank you
I showed you the code that produces a PDF file.
You could also just do a PROC PRINT of data set TOTALS
ods pdf file='yourfilename.pdf';
proc print data=totals;
run;
ods pdf close;
Hi Paige Miller,
This works, thank you. However, I think I didn't mention clearly what I am trying to do in the proc means. I am trying to write
SUM(Petunia SnapDragon Marigold)= Petunia_Sum SnapDragon_Sum Marigold_Sum
MEAN(Petunia SnapDragon Marigold)=Petunia_Mean SnapDragon_Mean Marigold_Mean;
and I don't want to create an output data set 'totals' but instead I want to write this to a file using FILE or ODS.
I mean, I don't want to create any output data set like totals. But, want to write
SUM(Petunia SnapDragon Marigold)= Petunia_Sum SnapDragon_Sum Marigold_Sum
MEAN(Petunia SnapDragon Marigold)=Petunia_Mean SnapDragon_Mean Marigold_Mean; to a file in proc means step.
Or, to write such kind of output, is it the only way that I have to first create a data set like 'totals' and then write to a pdf file using the code given by you and there is no other way of achieving this using FILE statement in proc means?
Sorry, if I am asking a silly question.
Thank you
It is not at all clear what you are asking for.
Do you want to write a REPORT for humans to read? Like a PDF file or an HTML file.
Or do you want to create a data file for a program to read? Like a CSV file.
What exactly do you want to write into the file?
Show some simple example input data or use some available dataset in SASHELP, like SASHELP.CLASS and show exactly what output you want for that input.
Just a simple point to consider. If you want to write out the MEAN (or any other statistic) of a variable you have to first calculate the value and save it somewhere.
Hi Tom,
I am trying to write the
SUM(Petunia SnapDragon Marigold)= Petunia_Sum SnapDragon_Sum Marigold_Sum;
MEAN(Petunia SnapDragon Marigold)=Petunia_Mean SnapDragon_Mean Marigold_Mean;
to the FILE "Means_SUMMARY_REPORT.rep", as I do not want to create output data set "totals" but getting the following errors:
575 proc means data=sales1;
576 by CustomerID;
577 var Petunia SnapDragon Marigold;
578 /*Write the following to the Means_SUMMARY_REPORT.rep file*/
579 SUM(Petunia SnapDragon Marigold)= Petunia_Sum SnapDragon_Sum Marigold_Sum;
---
180
580 MEAN(Petunia SnapDragon Marigold)=Petunia_Mean SnapDragon_Mean Marigold_Mean;
----
180
ERROR 180-322: Statement is not valid or it is used out of proper order.
581
CODE that I am trying is:
data sales1;
infile 'H:\SAS Training\MyPractice\datafiles\Flowers_proc_means.txt';
input CustomerID $ @9 SaleDate MMDDYY10. Petunia SnapDragon Marigold;
FILE 'H:\SAS Training\MyPractice\outputfiles\Means_SUMMARY_REPORT.rep' PRINT OLD;
run;
proc sort data=sales1;
by CustomerID;
proc means data=sales1;
by CustomerID;
var Petunia SnapDragon Marigold;
/*Write the following to the Means_SUMMARY_REPORT.rep file*/
SUM(Petunia SnapDragon Marigold)= Petunia_Sum SnapDragon_Sum Marigold_Sum;
MEAN(Petunia SnapDragon Marigold)=Petunia_Mean SnapDragon_Mean Marigold_Mean;
run;
I am attaching the Flowers_proc_means.txt file.
But, as you have mentioned "Just a simple point to consider. If you want to write out the MEAN (or any other statistic) of a variable you have to first calculate the value and save it somewhere", I think, the only way to write to an external file is to create some output data set like "totals" storing the SUM and MEAN statistics and as Paige Miller has suggested use ODS to write it to a pdf file.
Thank you
I fail to see what is wrong with the solution I gave of creating the data set using your code and then using my code to do a PROC PRINT. Could you explain what that is an unacceptable solution? If you accepted that solution, you'd be done and able to move on to something else, but instead you continue to ask for a different solution.
Hi Paige Miller,
Actually, what I was thinking that instead of creating an out dataset 'totals', write the SUM and Mean to a file using FILE statement. But, after clarifications to my query by you and Tom, I understand that I have to create out data set 'totals' and use ODS PDF to write it to an external pdf file. Hence, your advise to use the ODS PDF file write 'total's data set to a file is a solution for my query.
Thank you very much.
@Moksha wrote:
Hi Tom,
I am trying to write the
SUM(Petunia SnapDragon Marigold)= Petunia_Sum SnapDragon_Sum Marigold_Sum;
MEAN(Petunia SnapDragon Marigold)=Petunia_Mean SnapDragon_Mean Marigold_Mean;
to the FILE "Means_SUMMARY_REPORT.rep", as I do not want to create output data set "totals" but getting the following errors:
575 proc means data=sales1;
576 by CustomerID;
577 var Petunia SnapDragon Marigold;
578 /*Write the following to the Means_SUMMARY_REPORT.rep file*/
579 SUM(Petunia SnapDragon Marigold)= Petunia_Sum SnapDragon_Sum Marigold_Sum;
---
180
580 MEAN(Petunia SnapDragon Marigold)=Petunia_Mean SnapDragon_Mean Marigold_Mean;
----
180
ERROR 180-322: Statement is not valid or it is used out of proper order.
581CODE that I am trying is:
data sales1;
infile 'H:\SAS Training\MyPractice\datafiles\Flowers_proc_means.txt';
input CustomerID $ @9 SaleDate MMDDYY10. Petunia SnapDragon Marigold;
FILE 'H:\SAS Training\MyPractice\outputfiles\Means_SUMMARY_REPORT.rep' PRINT OLD;
run;
proc sort data=sales1;
by CustomerID;proc means data=sales1;
by CustomerID;
var Petunia SnapDragon Marigold;
/*Write the following to the Means_SUMMARY_REPORT.rep file*/
SUM(Petunia SnapDragon Marigold)= Petunia_Sum SnapDragon_Sum Marigold_Sum;
MEAN(Petunia SnapDragon Marigold)=Petunia_Mean SnapDragon_Mean Marigold_Mean;run;
I am attaching the Flowers_proc_means.txt file.
But, as you have mentioned "Just a simple point to consider. If you want to write out the MEAN (or any other statistic) of a variable you have to first calculate the value and save it somewhere", I think, the only way to write to an external file is to create some output data set like "totals" storing the SUM and MEAN statistics and as Paige Miller has suggested use ODS to write it to a pdf file.
Thank you
You are part of the syntax for an OUTPUT statement but do not have an output statement. The only time that you name output variables such as with
SUM(Petunia SnapDragon Marigold)= Petunia_Sum SnapDragon_Sum Marigold_Sum;
is as part of an output statement that might look like
output out=datasetname SUM(Petunia SnapDragon Marigold)= Petunia_Sum SnapDragon_Sum Marigold_Sum;
Additionally you can only have one set of statistics requested in the output. When you ended the SUM statistics with ; then the next statement has to be another valid statement. If you want all of the output in one data set then all the statistics must be requested on a single statement.
You may want to consider using Proc Summary instead of Means if the purpose is to create a data set and control output of that set as the default data sets created are a bit different. Though you still have not provided any example of what you actually want your output file to look like.
Note: if using Proc Means/Summary to create an output data set and you want the statistic as a suffix to the variable name you can use the AUTONAME option.
MAYBE
proc means data=sales1; by CustomerID; var Petunia SnapDragon Marigold; output out=mydataset SUM = MEAN= /autoname; run;
This will create sum and mean statistics of all of the variable on the VAR statement suffixed with the statistic.
However this will also create ODS output. The output data set will, by default, contain additional variables _freq_ and _type_. _Freq_ counts the observations used per output observation and _type_ indicates combinations of Class variables. There will be an observation, _type_=0 that has the sum/mean for the entire data set as well as each level of CustomerId.
If you want to WRITE the result of the Means created data set, which you haven't created yet, then a separate step using that set is needed.
Thank you ballardw.
You say you want to write a text file that looks like: (Notice how much easier it is to share text without attaching files!)
756-01 05/04/2001 120 80 110 834-01 05/12/2001 90 160 60 901-02 05/18/2001 50 100 75 834-01 06/01/2001 80 60 100 756-01 06/11/2001 100 160 75 901-02 06/19/2001 60 60 60 756-01 06/25/2001 85 110 100
Then it seems you have 5 variables.
So assuming that the first one your CUSTOMER_ID variable and the second is probably a DATE variable of some sort.
You probably want to do something like:
proc summary data=sales1 nway ;
by customerid;
class sales_date ;
var Petunia SnapDragon Marigold;
output out=for_report sum= ;
run;
data _null_;
set for_report;
file 'H:\SAS Training\MyPractice\datafiles\Flowers_proc_means.txt';
put CustomerID SaleDate Petunia SnapDragon Marigold;
run;
If you need columns headers you might have to do a little more work.
Hi Tom,
The suggestion of Paige Miller to use ODS PDF and proc print to create the dataset 'totals' and the suggestion "Just a simple point to consider. If you want to write out the MEAN (or any other statistic) of a variable you have to first calculate the value and save it somewhere." provided by you earlier helped me to understand that I have to first create the data set like 'totals' and then use ODS PDF.
Thank you.
Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.
If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website.
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.