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
Related
I'm attempting to convert a bunch of columns which contain seconds to HH:MM:SS. Most entries are within a 24 hour period such as 78000 seconds.
For these I have used format time10. Which gives 21 hours and 42 minutes in this case.
However when I have something like 528846 seconds, sas returns its fine when I open the data set, however when I proc export:
Proc export data = myset
Outfile =mylocation\about.xlsx
Dbms=excel2000 replace;
Sheet="mysheet";
Run;
When I open the excel output my value shows as 00/01/1900 02:54:06. I've tried use the date formats in excel but cant get it do display as the 146:54:06 (528846 seconds to hours, mins and secs)
Any suggestions pls?
Do you really need the value as a number in EXCEL? If not then convert it to a string in that style in your SAS dataset before exporting it.
data for_export ;
set myset ;
time_string = put(time_var,time12.);
run;
proc export data=for_export file='mylocation\about.xlsx' dbms=xlsx replace;
sheet="mysheet";
run;
Excel will display hours > 24 if you use custom format [h]:mm:ss.
You can coerce output to use a custom format by using a Proc that allows ODS styling where in the style attribute setting tagattr="format:'[h]:mm:ss'" can be used.
Sample data (time10.)
data have;
format elapsed time10.;
input elapsed ##; datalines;
78000 86399 86400 86401 528846
;
Fix: Use ODS style attribute tagattr
ods excel file='try-2.xlsx';
proc print noobs label data=have;
var elapsed / style=[tagattr="format:'[h]:mm:ss'"];
run;
ods excel close;
Problem: Proc EXPORT
proc export data=have replace outfile='try-1.xlsx' dbms=xlsx;
sheet="My Sheet";
run;
options noxwait noxsync xmin;
%sysexec start "Preview" try-1.xlsx;
The EXPORT DBMS=XLSX engine outputs SAS time10. formatted data values as cells with Excel custom format h:mm. (Note: Output made with SAS 9.4 M6)
I'm wondering if it's possible to save the query results to csv? Without creating views.
I have a large table but need only 2 columns from there to process with python then. Maybe someone can help with it?
Here are three ways
ODS
SQL query can be output to an ODS CSV destination. This approach encompasses the widest possibilities of querying.
ods csv file='c:\temp\query-results.csv';
proc sql;
select name, age
from sashelp.class
where name like 'J%'
;
quit;
ods csv close;
EXPORT Procedure
Where clause can be applied using kept columns of 'a large table' (data=)
proc export
data = sashelp.class(
keep=name age
where = (
name like 'J%'
)
)
replace
file = 'c:\temp\class-subset.csv'
dbms = csv
;
run;
DATA _null_
Where statement can be applied using any columns of 'a large table' (SET). The PUT statement manages which columns are output.
data _null_;
set sashelp.class;
where name like 'J%';
file 'c:\temp\subset-per-datastep.csv' dlm=',' dsd;
if _n_ = 1 then put 'name,age';
put name age;
run;
I think you can use ods to create file with results, for example:
ods csv file="C:\test.csv" options(delimiter=';');
proc sql;
select * from sashelp.class;
quit;
ods csv close;
I need to export a data set from SAS to Excel 2013 as a .csv file. However, I need the file name to be dynamic. In this instance, I need it to appear as:
in_C000000_013117_65201.csv
where the string, "in_C000000_" will remain constant, the string "013117_" will be the current day's date, and the string "65201" will be the row count of the data set itself.
Any help that you can provide would be much appreciated!
Thanks!
Here's a modified macro I wrote in the past that does almost exactly what you're asking for. If you want to replace sysdate with a date in your desired format, that's easy to do as well:
%let path = [[desired destination]];
%macro exporter(dataset);
proc sql noprint;
select count(*) into: obs
from &dataset.;
quit;
data temp;
format date mmddyy6.;
date = today();
run;
proc sql noprint;
select date format mmddyy6. into: date_formatted
from temp;
quit;
proc export data = &dataset.
file = "&path.in_C000000_&date_formatted._%sysfunc(compress(&obs.)).csv"
dbms = csv replace;
run;
%mend exporter;
%exporter(your_dataset_here);
Produces datasets in the format: in_C000000_020117_50000.csv
I ´have a dataset with formats attached to it and I dont want to remove the formats from the dataset and when I use proc freq or proc print, I want the original values and not the formats attached.
Proc print data=mylib.data;
run;
is there any format=no option?
proc freq data=mylib.data;
tables gender;
format?????
run;
You can remove a format by specifying a null format on the PROC strep:
proc freq data=mylib.data ;
tables gender ;
format _ALL_ ;
run ;
_ALL_ is a list of all variables in the dataset.
How can I print (and export to file) monthly and weekly average of value? The data is stored in a library and the form is following:
Obs. Date Value
1 08FEB2016:00:00:00 29.00
2 05FEB2016:00:00:00 29.30
3 04FEB2016:00:00:00 29.93
4 03FEB2016:00:00:00 28.65
5 02FEB2016:00:00:00 28.40
(...)
3078 08MAR2004:00:00:00 32.59
3079 05MAR2004:00:00:00 32.75
3080 04MAR2004:00:00:00 32.05
3081 03MAR2004:00:00:00 31.82
EDIT: I somehow managed to get the monthly data but I'm returning average for each month separately. I would to have it done as one result, namely Month-Average+export it to a file or a data set. And still I have no idea how to deal with weeks.
%macro printAvgM(start,end);
proc summary data=sur1.dane(where=(Date>=&start
and Date<=&end)) nway;
var Value;
output out=want (drop=_:) mean=;
proc print;
run;
%mend printAvgM;
%printAvgM('01jan2003'd,'31jan2003'd);
EDIT2: Here is my code, step by step:
libname sur 'C:\myPath';
run;
proc import datafile="C:\myPath\myData.csv"
out=SUR.DANE
dbms=csv replace;
getnames=yes;
run;
proc sort data=sur.dane out=sur.dane;
by Date;
run;
libname sur1 "C:\myPath\myDB.accdb";
run;
proc datasets;
copy in=sur out=sur1;
select dane;
run;
data sur1.dane2;
set sur1.dane;
date2=datepart(Date);
format date2 WEEKV11.;
run;
The last step results in NOTE: SAS variable labels, formats, and lengths are not written to DBMS tables. and the format of dane2 variable is DATETIME19..
Ok, it's small enough to handle easily then. I would recommend first converting your datetime variable to a date variable using DATEPART() function and then use a format within PROC MEANS. You can look up the WEEKU and WEEKV formats to see if they meet your needs. The code below should be enough to get you started. You could do the monthly without the date conversion, but I couldn't find a weekly format for the datetime variable.
*Fake data generated;
data fd;
start=datetime();
do i=1 to 3000000 by 120;
datetime=start+(i-1)*30;
var=rand('normal', 25, 5);
output;
end;
keep datetime var;
format datetime datetime21.;
run;
*Get date variable;
data fd_date;
set fd;
date_var = datepart(datetime);
date_month = put(date_var, yymon7,);
Date_week = put(date_var, weekv11.);
run;
*Monthly summary;
proc means data=fd_date noprint nway;
class date_var;
var var;
output out=want_monthly mean(var)=avg_var std(var)=std_var;
format date_var monyy7.;
run;
*Weekly summary;
proc means data=fd_date noprint nway;
class date_var;
var var;
output out=want_weekly mean(var)=avg_var std(var)=std_var;
format date_var weekv11.;
run;
Replace date_var with the new monthly and weekly variables. Because these are character variables they won't sort properly.