I would like my data that is created in SAS to be imported into Excel rounded to two decimal places, I tried with proc export, now by ods excel, but still the numbers have many decimal places. these are my codes
proc sql;
create table COREP_CR_&gv_tbl_date as
select distinct
a.DATA_DANYCH as REPORTING_DATE,
"" as ID,
sum(a.EXP_PIERWOTNA_NETTO) as EAD_PRE_CCF format=9.2,
sum(a.KOREKTA) as PROVISION format=9.2,
group by
a.EXP_PIERWOTNA_NETTO
a.KOREKTA
;
quit;
ods excel file="&glb_path2./reports/mth_ak/COREP_CR_&gv_tbl_date..xlsx"
;
proc print data=COREP_CR_&gv_tbl_date;
run;
ods excel close;
Solutions :
Use round()
round(sum(a.EXP_PIERWOTNA_NETTO), 0.01)
Related
I have a SAS data set with a Number field. When I do proc export dbms=xlsx, it converts the number to scientific notation.
Now I know I can change the number to a text in SAS and that will preserve the format as desired. But changing it to text creates issues with joins because it needs to join to another table where the column is numeric.
In short, I am not too keen on changing the format using put(Colname,z5.)
But I want to show in excel as a text...if I cannot get leading zeroes, at least I don't want the scientific notation. Is it possible?
Proc EXPORT DBMS=XLSX output
Does NOT produce formatted values in the target. The raw values will appear.
The numeric format is Excel General, so values >= 1E+11 will be displayed in scientific notation.
Does MAINTAIN SAS date formatted variable values as Excel date values formatted similarily to the original SAS date format.
ODS EXCEL output
Does produce formatted values in the target.
Allows customized Excel rendering with style option tagattr=
Example:
proc format;
value $lettered
'A'-'B' = 'Aaaaaaaaaa'
'B'-'C' = 'Bbbbbbbbbb'
'C'-'D' = 'Cccccccccc'
'D'-'E' = 'Dddddddddd'
'E'-'F' = 'Eeeeeeeeee'
;
;
data class(keep=id date: name: x age);
format id z9.
date1 date9.
date2 mmddyy10.
date3 yymmdd10.
date4 ddmmyy10.
name $10.
name2 $lettered.
x 9.4
;
set sashelp.class;
id + 1;
date1 = today() - id;
date2 = date1;
date3 = date2;
date4 = date3;
name2=name;
x = rand('uniform', 100);
run;
proc export data=class dbms=xlsx replace file='c:\temp\export.xlsx';
run;
ods excel file='c:\temp\print.xlsx';
proc print noobs data=class;
run;
ods excel close;
options noxwait xsync xmin;
%sysexec start "Preview" /D C:\Temp export.xlsx;
%sysexec start "Preview" /D C:\Temp print.xlsx;
Excel from PROC Export - Some date value formatting similar to original SAS date formatting, otherwise raw data values
Excel from ODS Excel & PROC Print - SAS date/custom/numeric formats in output
Use PROC REPORT and ODS instead.
ods excel file="path/to/output.xlsx" ;
proc report data=mydata ;
columns _ALL_ ;
run ;
ods excel close ;
If you wanted to force a particular variable to Excel Text, add the below to the PROC REPORT :
define numvar / style={tagattr='format:text'} ;
https://support.sas.com//rnd/base/ods/templateFAQ/office91.pdf
I have a question on format in SAS. Below is my code
PROC SQL;
CREATE TABLE OUT.FIN_POP_4a AS
SELECT sum(CORR_AM) as correction_am
FROM OUT.FIN_POP_4
;
QUIT;
correction_am is format numeric 8. How do I change this to Comma10.2 or Dollar10.2 in SAS in data step or proc sql code? I have tried various methods that did not work.
I have tried below code which worked but did not retain the two decimal places.
PROC SQL;
CREATE TABLE OUT.FIN_POP_4a AS
SELECT sum(CORR_AM) as correction_am format comma10.
FROM OUT.FIN_POP_4
;
QUIT;
you need formatw.d but you have only formatw. w = width d = decimal. So you do not have any decimal part that is why you do not see decimals. See below example and try both of them. first one gives no decimals and second gives you decimal values.
/* no decimal part i.e comma10.*/
PROC SQL;
CREATE TABLE FIN_POP_4a
AS SELECT sum(weight) as correction_am format= comma10.
FROM sashelp.class ; QUIT;
/* decimal part i.e comma10.2*/
PROC SQL;
CREATE TABLE FIN_POP_4a
AS SELECT sum(weight) as correction_am format= comma10.2
FROM sashelp.class ; QUIT;
use format comma10.2 instead of format comma10. ...
You may need to increase length as well since if you have millions, billions, or trillions or dollars then there is an increase in length ie comma15.2 or comma20.2. Below recommendation:
PROC SQL; CREATE TABLE FIN_POP_4a AS SELECT sum(weight) as correction_am format= comma15.2 FROM sashelp.class ; QUIT;
I have a macro which looks like this:
%macro mac_name (st, en=);
%do j=1 %to &en.;
%let k=%eval(&j.+1);
proc freq data=data_name;
tables status&j. * status&k. / nocol norow nopercent missing;
run;
%end;
%mend;
%mac_name (st=1, en=%sysfunc(week(%sysfunc(today()), u)));
The output produces multiple proc freq tables with the same title.
I need this output put into a excel spreadsheet. Ideally all proc freqs in one sheet, one above the other or separate sheets.
Is this possible?
Thanks in advance!!!
The easiest way to do this is to use ODS EXCEL, if you have SAS 9.4.
ods excel file="yourfilename.xlsx";
proc freq data=sashelp.class;
tables age;
run;
proc freq data=sashelp.class;
tables sex;
run;
ods excel close;
You have options for whether they're all on one sheet or separate sheets. You can use ODS TAGSETS.EXCELXP if you have an earlier version of SAS, though they're less "true excel" files. You can also make CSV files or various other things with ODS.
In your case you'd put the opening ODS EXCEL line before the first call of the macro (doesn't have to precede the definition of the macro) and then the ODS EXCEL CLOSE line after the last call.
I am trying to prepare a report in Excel using ODS Excel in SAS. The report contains two sheets with three tables place side by side. I used Panelcol option but it is not available in ODS Excel. Also, the Start_at= option does not seem to be working?
Is there anyway it can be done in ODS Excel? Please let me know.
Any help will be very much appreciated.
Thank you,
Shankar
Shankar:
The ODS EXCEL docs for 9.4 state the start_at= options can not be changed mid-sheet. Link
(START_AT='string') specifies a starting cell for the report. The
default is to start at column 1 and row 1. Default 1,1 Tip This option
cannot be changed in the middle of a sheet. Example ods excel
options(start_at="2,2");
So this example does not work as you expect. The first start_at is honored, and subsequent output on the same page is stacked while still honoring the first start at column.
ods _all_ close;
ods excel file='%temp%\3-tables-in-first-tab-sample.xlsx'
options (sheet_interval='none')
;
ods excel options (start_at="2,2");
* top of output is upper left corner;
proc print noobs data=sashelp.class(keep=name age);run;
* top of output is to the right and down three rows from the first output;
ods excel options (start_at="5,5");
proc print noobs data=sashelp.class(keep=name weight height);run;
ods excel options (start_at="1,9");
proc print noobs data=sashelp.class(keep=name sex);run;
ods excel close;
One way to put multiple tables together is to create a faux table that appends data in a sidewise manner.
Example:
%macro RHS_append (out=, data=);
%if not %sysfunc(exist(&out)) %then %do;
data &out; set &data; run;
%return;
%end;
%local nout names labels;
proc contents noprint data=&out out=outvar(keep=name varnum label);
proc contents noprint data=&data out=datavar(keep=name varnum label);
proc sql noprint; select count(*) into :nout trimmed from outvar;
select
cats(name,'=v',&nout+varnum+1)
, case
when label=''
then cats('v',&nout+varnum+1,'=',quote(trim(name)))
else ''
end
into
:names separated by ' '
, :labels separated by ' '
from datavar order by varnum
;
create table gap (v%eval(&nout+1) char(1) label='a0'x);
quit;
data &out;
merge &out gap &data(rename=(&names)); %* rare use of merge without by;
label &labels;
run;
%mend;
options mprint;
proc delete data=foo;
run;
%RHS_append (out=foo, data=sashelp.class);
%RHS_append (out=foo, data=sashelp.gas);
%RHS_append (out=foo, data=sashelp.cars);
ods excel file='%temp%\3-tables-in-first-tab-sample.xlsx';
proc print label data=foo;
run;
ods excel close;
I am trying to report my proc means output with 10 decimal places by specifying maxdec=10. However, SAS does not report more than 7 decimal places.
Here is the warning I get:
WARNING: NDec value is inappropriate, BEST format will be used.
I appreciate any suggestion.
If you look at the documentation, it states that MEANS will print out 0-8 decimal places based on the value of MAXDEC. If you want more, you will need to save the results and print them yourself.
Try this:
data test;
format x 12.11;
do i=1 to 1000;
x = rannor(0);
output;
end;
drop i;
run;
proc means data=test noprint;
var x;
output out=means_out mean=mean std=std;
run;
proc print data=means_out noobs;
var mean std;
format mean std 12.11;
run;
As already mentioned, maxdec= works for limiting the number of decimal places below 8. Proc means isn't going to let you do too much to change the format of the summary statistics. I'd suggest using proc tabulate:
If your proc means looks like:
proc means data=yourdata;
var yourvariable;
run;
Than use something like:
proc tabulate data=yourdata;
var yourvariable;
table yourvariable*
(n
mean*format=15.10
stddev*format=15.10
min*format=15.10
max*format=15.10);
run;