I have a comma delimited data set whose first 2 rows is like this:
1/13/2010 21:09,3.3
11/30/2010 7:33,7.2
....
In trying to read the data in SAS, I have done this data step below:
data myDataSet;
infile 'sampleData.csv' dlm=',';
input timestamp :mmddyy16. value;
run;
Now that the data is now a SAS data set, I try to view it by doing:
data viewData;
set myDataSet;
format timestamp date9.;
run;
proc print data=viewData;
run;
I observed that the timestamp column output only contains the date and not with the time. I want the timestamp to be read and displayed in a format like "dd-mm-yyyy HH:MM:SS". How do I ensure that in reading the file, the informat is correctly specified and no component of the timestamp is lost?
MMDDYYw is a date informat, not a datetime informat - it only reads days and larger and ignores the time. It has a practical maximum length of 10 (although it allows up to 32) as a result.
You can use MDYAMPM. to read those two dates in, among other informats. That might be the ultimately correct informat, or it might not, depending on the totality of your data; several NLS specific informats might also work. See the SAS informat documentation for more details.
Related
All,
I am trying to nest a macro within a macro but am unsuccessful. The Start_Cycle variable is set every few months and updated manually. I want to create a start_point variable that goes back 6 months and I successfully created it, however, the output includes a space after the %STR as seen below
%let start_cycle = '01JUL2022:00:00:00'dt; /set to beginning month of this cycle/
%let start_point = %STR(%')%sysfunc(intnx(DTMONTH,&start_cycle.,-6,b),datetime19.)%STR(%')dt;
%put &start_point;
Output below
%let start_cycle = '01JUL2022:00:00:00'dt; /set to beginning month of this cycle/
%let start_point =
%STR(%')%sysfunc(intnx(DTMONTH,&start_cycle.,-6,b),datetime19.)%STR(%')dt;
%put &start_point; ' 01JAN2022:00:00:00'dt
^^Does anyone know why there is a space after the single quote? ' 01JAN2022:00:00:00'dt
Since it runs without issues, I decided to create another macro variable that does the same thing, but instead, the output needs to be converted to a character string in this format below (current Macro)
%let start_pointSales = '2022/01';
I tried multiple times using different ways of going about this, spent many hours looking through forum from SAS Communities to StackOverflow and even SAS youtube videos to no luck. Anyone have any luck in combating this?
To-Be Macro:
%let NEW_start_pointSales = %sysfunc(intnx(month,&start_cycle.,-6,b),yymms.);
%put &NEW_start_pointSales;
The NEW_start_pointSales will be used in the WHERE clause with Data type Varchar (21) using PROC SQL.
left join EDWSALE.DSCOE_LLD (where=( &NEW_start_pointSales. <= SALES_MONTH < &end_pointSales.
Output Error below:
NOTE: Writing TAGSETS.SASREPORT13(EGSR) Body file: EGSR
24
25 GOPTIONS ACCESSIBLE;
WARNING: An argument to the function INTNX referenced by the %SYSFUNC or %QSYSFUNC macro function is out of range.
NOTE: Mathematical operations could not be performed during %SYSFUNC function execution. The result of the operations have been set
to a missing value.
Any help is appreciated!
You cannot apply a format, YYMMS, designed to work on DAYS to a value that is in SECONDS. If you want to use a date format on a datetime value you need to convert the value from seconds to days. You could use the DATEPART() function or just divide by the number of seconds in a day.
Why are you trying to compare a variable that is CHARACTER to numbers? If you generate a macro variable with a string like 2022/01 and then use it to generate code like:
&NEW_start_pointSales. <= SALES_MONTH
that will result in code like:
2022/01 <= SALES_MONTH
which is comparing the number 2022 (any number divided by 1 is itself) to the value SALES_MONTH, which you just said was a character string.
What types of strings does SALES_MONTH contain? That will determine how (or whether) you can make inequality tests against it.
PS There is space in the output of DATETIME19 because that is how that particular format works. Note that there is a bug in the DATETIME format and you cannot use DATETIME18. to produce a string with four digits for the year even though only 18 characters would be used. The extra space does not matter to using the string in a datetime literal as the DATETIME informat will recognize the string even with the extra space.
I have some code snippet as below that is not accepted by Scala, it would be appreciated if someone can help to fix it, thanks.
train_no_header is an RDD generated from a csv file, its first line is shown as below:
scala> train_no_header.first
res4: String = 87540,12,1,13,497,2017-11-07 09:30:38,,0
Now, I want to generate another RDD to parse and transform records with null or empty value for the 6th field which should be a DateTime (in the above sample the field is empty), some records might have that and some might not, for those having that, the format is same as 5th which is a UTC DateTime.
I need to calculate the delta between the two DateTime, I plan to convert them into Unixtime format, that being said, the final RDD should have both the two date fields converted into Unixtime format.
So my question is:
with the sample data and format, how do I create the RDD with the needed result?
for records with empty value in the 6th field, how should I handle it so that no exception would be generated in the future query in data frame (which is what I intend to work in)
Thank you very much in advance, any clue is appreciated.
I have a datetime22.3 variable which I would like to display as date.
for eg I want to display 17JUL2006:00:00:00.000 as 07/17/2006
How do I do this?
Thanks.
additional info:
Thanks for all the replies.
Actually, I tried to output it in the date format within proc sql. The output is being printed as ********** (stars). I am not sure what is going on.
I am trying to use INTCK in the following manner but get error. I am not sure what I am doing wrong. I would appreciate your help. Thanks.
PROC FORMAT;
PICTURE DTFMT LOW-HIGH='%0m/%0d/%Y' (DATATYPE=DATETIME);
RUN;
data want;
dt_val1='17JUL2006:00:00:00.000'dt;
dt_val2='17AUG2012:00:00:00.000'dt;
format dt_val1 dt_val2 dt_val3 dtfmt.;
dt_val3=intck('MONTH',dt_val1,dt_val2);
put dt_val3;
run;
You can't apply a standard date format directly against a datetime value, although there are some date formats you can prefix with 'DT' which will display a datetime as a date. Unfortunately the MMDDYY format is not one of these, however you could use DTDATE9. which would format your datetime as '17JUL2006'.
Another option is create your own format using the PICTURE statement, the example below will display the datetime as required.
proc format;
picture dtfmt low-high='%0m/%0d/%Y' (datatype=datetime);
run;
data want;
dt_val='17JUL2006:00:00:00.000'dt;
format dt_val dtfmt.;
run;
put(datepart(datetimevariable),yymmdd10.)
use the same princpiple in a data step
data _null_;
a='17JUL2006:00:00:00.000'd;
put a;
put 'formatted date='a MMDDYY10.;
run;
This is the output from my SAS 9.3
44 data _null_;
45 a = '17JUL2006:00:00:00:000'D;
46 put a;
47 put 'formatted ' a MMDDYY10.;
48 run;
16999
formatted 07/17/2006
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
Formatting dates in SAS can be tricky. One method I've used in a macro is this:
/* get the date time */
%let start_date=%sysfunc(datetime().,10);
/* use the DATETIME informat to format the date */
%let fmt_start_date=%sysfunc(putn(&start_date, DATETIME.));
/* format the datetime as a date */
%put "&fmt_start_date."d;
There's a bunch of different ways to format dates. You could also use the FORMAT statement if you're in a data step:
FORMAT STARTDATE YYMMDD10.;
In this case, the format of the column in the data step would give you YYYY-MM-DD and then you can separate the values and reconstruct from there.
There's additional information about SAS informats for dates here:
http://support.sas.com/documentation/cdl/en/etsug/60372/HTML/default/viewer.htm#etsug_intervals_sect008.htm
And here:
http://support.sas.com/documentation/cdl/en/etsug/63348/HTML/default/viewer.htm#etsug_intervals_sect009.htm
If you need more info or examples, please let me know.
Best of luck!
If you want to display the variable's contents as a date (without changing the underlying value), use a FORMAT statement.
proc print;
format dte mmddyys10.;
run;
proc means;
class dte;
format dte mmddyys10.;
run;
etc. Note that you can also put the FORMAT in a data step, in which case any uses of the variable will automatically pick it up.
data foo;
format dte mmddyys10.;
run;
My answer from a duplicate question:
You need to convert original SAS DATETIME value (think of it as data type) to SAS DATE value using DATEPART() function and apply appropriate format:
proc sql;
create table work.abc2
as select *, DATEPART(a.Billing_Dt) format ddmmyy10. as Bill_date
from abc;
quit;
So the point is, as Keith pointed above, to apply appropriate type of format (e.g. ddmmyy10. is the SAS Date format) to appropriate values = variable content (e.g. (unformatted) 10 is 11th January 1960 if used as date, while it's 01JAN60:00:00:10 if used as Datetime value), so you have to be sure what your values represent and convert the values if needed.
data want;
dt_val1='17JUL2006:00:00:00.000'dt;
dt_you_want=input(substr(put(dt_val1,datetime22.3),1,9),date9.);
format dt ddmmyy10.;
run;
Converts date/time var to char date var:
BLEndDatex = put(datepart(BLEndDateTime),yymmdd10.);
Create numeric sas date without time:
BLEndDate = mdy(SUBSTR(BLEndDatex,6,2),SUBSTR(BLEndDatex,9,2),SUBSTR(BLEndDatex,1,4));
Thanks to Rizier123 & Heemin posting above to the first portion.
Good Morning,
A program in SAS is about to select/merge/sort dates/times in alphanumeric value (ex : 14-Jan-2013 07:00:00.479) inside a lot of tables and to create a single table.
This program use the instruction "format E8601" and several lines after "format type $10.;
informat type $10.;", what transforms the dates in a numeric value (ex : 2013-01-14T07:02:03.647).
When this table is exported in Excel 2007, the value becomes " " and can't be modified in a traditional date/time format.
How to do it ? Is there any other format (instead of E8601) which can be used to keep the date in text or in a alphanumeric value ?
Thanks for your help.
SAS datetime values are internally represented as floating point values equal to the number of seconds since January 1, 1960. FORMATS are used to control how those numeric values are externally represented. For example, consider this:
data have;
myDateTime1 = '14-Jan-2013 07:00:00.479'dt;
myDateTime2 = '14-Jan-2013 07:00:00.479'dt;
myDateTime3 = '14-Jan-2013 07:00:00.479'dt;
format myDateTime2 datetime23.3
myDateTime3 E8601DT23.3;
put myDateTime1= 'as a number'
/ myDateTime2= 'as a normal SAS datetime'
/ myDateTime3= 'as an ISO 8601 datetime'
;
run;
When run, this is shown in the SAS log:
myDateTime1=1673766000.5 as a number
myDateTime2=14JAN2013:07:00:00.479 as a normal SAS datetime
myDateTime3=2013-01-14T07:00:00.479 as an ISO 8601 datetime
Note the three myDateTime variables have the same value but are displayed differently based on the format specified.
Assuming you have SAS Access to PC File Formats licensed, you can just use PROC EXPORT to create an Excel workbook:
proc export data=have
outfile='c:\temp\test_dates.xlsx'
replace;
run;
The data values in the Excel workbook for the two variables formatted as "datetime" values will appear correctly as Excel columns. However, the default formatting in Excel only shows the "date" portion; to display the complete value in Excel you will need to change the Excel column formats.
Can anyone advise on the appropriate SAS informat to read in a datetime (dd/mm/yyyy hh:mm) ???
eg
data _null_;
informat from_dt datetime????.;
input from_dt ;
put from_dt=;
cards;
01/01/1960 00:00
;run;
The anydt* family of informats do work, usually.
data _null_;
from_dt = input("01/01/1960 00:00", anydtdtm.);
put from_dt= :datetime20.;
run;
/* on log
from_dt=01JAN1960:00:00:00
*/
I don't think that specific informat is built-in, but it is relatively easy to roll your own. Below is an example of a creating a custom datetime informat (this requires a cntlin data set) and a custom format (using the picture statement) that reads in your specific datetime and then formats it back out to look the same as the input. I simplified it by assuming the time part was always midnight (00:00), but it can be easily expanded if you need to keep track of the time parts as well (just change the 86400 number to 3600 to get every hour, and 60 for every minute). It helps to see what is going on if you open the work.infmt data set to see what it looks like.
/* Create a custom datetime format as Month/Day/Year<space>Hours:Minutes*/
proc format;
picture mydt other='%0m/%0d/%0Y %0H:%0M' (datatype=datetime);
run;
/* Create a custom informat using the format above */
data infmt ;
retain fmtname "dtwithspace" type "I" ;
do label = "1jan1960:00:00"dt to
"2jan2059:00:00"dt by 86400 ;
start = trim(left(put(label,mydt.)));
output ;
end ;
run ;
proc format cntlin = infmt ;
run ;
/* Now apply the informat and format to the data */
data _null_;
input from_dt $ 1-20;
format from_dt2 mydt.;
from_dt2=input(from_dt, dtwithspace.);
put from_dt2=;
cards;
01/01/1960 00:00
01/02/1999 00:00
;
run;
This gave an output like this:
278 data _null_;
279 input from_dt $ 1-20;
280 format from_dt2 mydt.;
281 from_dt2=input(from_dt, dtwithspace.);
282 put from_dt2=;
283 cards;
from_dt2=01/01/1960 00:00
from_dt2=01/02/1999 00:00
SAS might not support the specific datetime format your data is in. You could either try to convert the incoming data to a frendlier format or you could parse the datetime using the substr, DHMS and MDY functions:
data test;
format dtstr $16. dt datetime22.4;
dtstr='01/01/1960 00:00';
day=substr(dtstr,1,2);
month=substr(dtstr,4,2);
year=substr(dtstr,7,4);
hour=substr(dtstr,11,2);
minute=substr(dtstr,15,2);
dt=DHMS(MDY(1*month,1*day,1*year),1*hour,1*minute,0);
output;
run;
Or alternatively you could convert the datetime string into a datetimew.d format and input the formatted string:
data test;
format dtstr $16. dstr $8. tstr $5. indtstr $14. dt datetime22.4;
dtstr='01/01/1960 00:00';
dstr=put(input(substr(dtstr,1,10),mmddyy10.),date8.);
tstr=substr(dtstr,12);
indtstr=dstr!!':'!!tstr;
dt=input(indtstr,datetime14.0);
output;
run;
The conversion can be compressed to a single but complex statement, so creating a macro for this might be a good decision.
This entry from the SAS knowledgebase includes code for parsing and formatting datetime. Looks like SAS has a great online help system.
The third message in this exchange on Google groups may be helpful as well. It talks about inputting datetime, and provides code.
Your question is so hard to decipher, and I know so little about SAS, that's about all I can offer. Hope it helps.
Going by the list here, I don't think there is one.
Conceivably you could create your own with proc format, but I think that would be very difficult. Ville Koskinen's suggestion is probably your best bet.