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)
Related
I have this code in SAS for an assignment. I am learning SAS and have completed all of the items already coded for and successfully have reviewed the output in the log and the results tab of SAS. I have the code included below:
ods listing close;
ods rtf file="C:\Users\barnedsm\Desktop\SAS
\Homework 8 hm.rtf" style=htmlblue;
1. read in the data into SAS to get a SAS data set trees using the proc import statement. (5
points)
proc import
datafile="C:\Users\barnedsm\Desktop\SAS\Trees.csv"
dbms=csv
out=trees;
run;
2. print the first 5 observations of the data set using the proc print statement. (5 points)
proc print data=trees (obs=5);
run;
3. get the summary for the three variables (Girth, Height and Volume) of the data using
the proc means statement. (5 points)
proc means data=trees;
var Girth Height Volume;
run;
4. sort the data by Girth, Height and Volume variables. (1.5 bonus points)
Girth
proc sort data=trees out=trees;
by Girth;
proc print data=trees (obs=5);
run;
Height
proc sort data=trees out=trees;
by Height;
proc print data=trees (obs=5);
run;
Volume
proc sort data=trees out=trees;
by Volume;
proc print data=trees (obs=5);
run;
Three Variables
proc sort data=trees out=trees;
by Girth Height Volume;
proc print data=trees (obs=5);
run;
libname mylibr "C:\Users\barnedsm\Desktop\SAS";
data mylibr.trees_permanent;
set trees;
run;
5. read in the data into SAS to get a SAS data set ToothGrowth using the proc import
statement. (5 points)
proc import
datafile="C:\Users\barnedsm\Desktop\SAS\ToothGrowth.csv"
dbms=csv
out=tooth;
proc print data=tooth (obs=5);
run;
6. create two SAS data sets ToothGrowth_OJ and ToothGrowth_VC for the animals with the
delivery method orange juice and ascorbic acid, respectively. (5 points)
data ToothGrowth_OJ;
set tooth;
where (supp="OJ");
proc print data=ToothGrowth_OJ (obs=5);
run;
data ToothGrowth_VC;
set tooth;
where (supp="VC");
proc print data=ToothGrowth_VC (obs=5);
run;
7. save the two SAS data sets in a permanent folder on your computer. (5 points)
libname mylibr "C:\Users\barnedsm\Desktop\SAS";
data mylibr.ToothGrowth_OJ_permanent;
set ToothGrowth_OJ;
run;
libname mylibr "C:\Users\barnedsm\Desktop\SAS";
data mylibr.ToothGrowth_VC_permanent;
set ToothGrowth_VC;
run;
ods rtf close;
ods listing;
This works just fine, giving me an output with tables, but I want the SAS code from the editor included, which is the code above. How would I accomplish this? This is my first time using SAS, so I am very new.
The picture is one of the charts that was outputted, so the code works. But I'd like the SAS code included in the exported word (rtf) file. I have tried also removing the ads listing code, but it made no difference. Can anyone point out why my code is not included or direct me on how to do so. I mostly use R and R Studio, so this exporting format is new to me.
I'm trying to export quartile information on a grouped dataset as a dataset in SAS but when I run this code my output is a table with the correct information displayed but the dataset WORK.TOP_1O_PERC is only summary statistics of the set (no quartiles). Does anyone know how I can export this as the CLASS (PDX) and its 25th and 75th percentiles? Thanks!
PROC MEANS DATA=WORK.TOP_10_DX P25 P75;
CLASS PDX;
VAR AmtPaid;
OUTPUT OUT = WORK.TOP_10_PERC;
RUN;
I like the STACKODS output that is a data set which is like the default printed output.
proc means data=sashelp.class n p25 p75 stackods;
ods output summary=summary;
run;
proc print;
run;
You can use output statement with <statistics>= options.
PROC MEANS DATA=WORK.TOP_10_DX NOPRINT;
CLASS PDX;
VAR AmtPaid;
OUTPUT OUT = WORK.TOP_10_PERC P25=P25 P75=P75;
RUN;
Compared to ods output, output statement is much faster but less flexible with multiple analysis variables or by statement specified situation.
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 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
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.