I am trying to create a SAS informat for the following date format:
"yyyy-mm-dd hh:ii:ss.SSS UTC", example: "2016-01-14 10:31:01.456 UTC"
I've gotten close using the following format code:
PROC FORMAT;
PICTURE MyDate other='%0Y-%0m-%0d %0H:%0M:%0s UTC' (datatype=datetime);
RUN;
Unfortunately when I try and use this as an INFORMAT I get an error "can't find format MyDate", as it hasn't been defined as an informat, just an output format.
I can try and create an informat from a dataset created from this format, but due to the milliseconds constraint it will only create values that map to times with .000 in the milliseconds section. For example:
DATA MyInDate ;
RETAIN FMTNAME "MyInputDate" type "I" ;
do label = "1jan2016:00:00:00"dt to
"2feb2016:00:00:00"dt by 1;
start = trim(left(put(label,MyDate.)));
output ;
end ;
RUN;
PROC FORMAT CNTLIN=MyInDate;
RUN;
Even if I were able to enumerate a dataset with milliseconds it would be prohibitively large. Since my dates can span years.
Is it possible to truncate my input data BEFORE passing it to the informat? I don't care about the milliseconds or the UTC qualifier. I can't change the input data.
EDIT: Using anydtdtm. as an informat results in empty values without error messages. Here is the data step making use of this informat:
DATA WORK.ImportDatTest;
LENGTH
'Event Time'n 8
;
FORMAT
'Event Time'n DATETIME25.
;
INFORMAT
'Event Time'n anydtdtm.
;
INFILE DATALINES DLM=','
;
INPUT
'Event Time'n : ANYDTDTM.
;
DATALINES;
2016-01-11 17:23:34.834 UTC
2016-01-11 17:23:34.834 UTC
2016-01-11 17:23:34.834 UTC
;
RUN;
Unfortunately, there is no way to create a picture informat in SAS currently. You would need to convert your data to a format SAS has a built-in informat for, or use a function or similar to format the data.
However, yours already is in such a format, so you shouldn't need to create an informat.
data test;
x="2015-10-05 10:12:24.333 UTC";
y=input(x,anydtdtm.);
put y= datetime17.;
run;
You can certainly truncate data while using an informat; by specifying a length in the informat, it will truncate to that length.
Here's an example using input from datalines:
data test;
infile datalines dlm=',' dsd;
input y :anydtdtm32.;
put y= datetime22.3;
datalines;
2015-10-05 10:12:24.333 UTC
2014-03-01 08:08:05.435 UTC
2013-01-01 23:02:05.445 UTC
;;;
run;
Related
Im trying load data from csv. I have a few formats: date, time, numeric, string. I dont have problem to convert data to this format except time format.
Data looks:
Date,Time,Transaction,Item
2016-10-30,9:58:12,1,Bread
2016-10-30,10:05:36,2,Scandinavian
2016-10-30,10:08:00,3,Hot chocolate
My code:
data lab0.piekarnia;
INFILE 'path_to_csv' delimiter=',' firstobs=2;
format Date yymmdd10.;
format Time time8.;
INPUT
Date yymmdd10.
Time time8.
Transaction
Item $;
run;
Result
What I try?
I try to manually convert string '12:22:22', This method give good results, but I dont know how can I implement it when load csv.
data ds1;
j = input('12:22:22',HHMMSS8.);
format j time8.;
run;
data have;
INFILE "path_to_csv" truncover delimiter=',' firstobs=2 ;
format Date yymmdd10.;
format Time time8.;
INPUT date time transaction item $32.;
informat
Date yymmdd10.
Time time.;
/*Instead input and informat statements you can use:
INPUT date:yymmdd10. time:time. transaction item $32.;
*/
run;
The first line has only 7 characters for the time value, but you told SAS to read exactly 8 characters. So it included the comma. When reading delimited data, like a CSV file, you need to use list mode input and not formatted mode. You do this by either eliminating the informat specification from the INPUT statement (and instead attach an informat to the variable with an INFORMAT statement) or by prefixing informat specification with the : (colon) modifier. Also if you don't define the length for ITEM (or give SAS something else, like an informat, that it can use to guess a length) it will be created as length $8.
input date :yymmdd10. time :time8. transaction item :$40.;
I have a SAS field where the datatype is number and format is date9.
It has a value like 30SEP2018.
How do I convert it to a SAS date so I can do date operations?
SAS dates are stored as number starting from 1/1/1960 and it starts form number = 0 and increases by 1 for every day. Your date is stored as number and then you get from proc contents and format is to display in the way you want.
data have;
input date:date9.;
format date date9.;
datalines;
30SEP2018
;
proc contents data=have;
run;
you can calculations on above date and gives you appropriate results as shown below
data want;
set have;
new_date= date+1;
new_date1= date-1;
format new_date new_date1 date9.;
run;
proc print; run;
I would like to get only date and time separately from 01JAN13:08:29:00
Format & Infomat available in Dataset is:
Date Num 8 DATETIME.(format) ANYDTDTM40(informat)
And If I run datepart() on 01JAN13:08:29:00 I get output as 19359 (I don't want it.)
The DATEPART function extracts the date value from a datetime value. The date value as you have seen is simply a number. A date format must be applied to a variable holding a date value. Base SAS variables have only two value types, character and numeric.
data want;
now_dtm = datetime();
now_dt = datepart(now_dtm);
now_dt_unformatted = now_dt;
format now_dtm datetime.;
format now_dt date9.; * <----- this is what you need, format stored in data set header information;
run;
proc print data=want;
run;
* you can change the format temporarily during a proc step;
proc print data=want;
format now_dt yymmdd10.; * <---- changes format for duration of proc step;
format now_dt_unformatted mmddyy10.;
run;
Actually 19,359 is exactly the value you want. You started with the number of seconds since 1960 and converted it to the number of days since 1960.
data x ;
dt = '01JAN13:08:29:00'dt ;
date = datepart(dt);
time = timepart(dt);
put (dt date time) (=);
run;
Results
dt=1672648140 date=19359 time=30540
You just need to attach a format to your new variable so that SAS will display the value in a format that humans will recognize. You could use a format like DATE9. to have it show 19,359 as 01JAN2013. Similarly you need to attach a format to the time part to make it print in format that human's will interpret as a time.
format date date9. time time8. ;
I have a table in SAS where in one column, the date is stored (e.g. "2005/10").
How do I have to convert this to a SAS data format?
Among many other tries, I tried this code:
data test;
format date YYMMS.;
date = input(ObservationMonth, YYMMS.);
put date=date9.;
run;
You could just use the anydtdte. informat.
data want;
format date yymms.;
text="2005/10";
date=input(text,anydtdte.);
put date;
run;
This informat detects most date formattings and converts the character-stored value to a SAS date.
One way is to use substr() and mdy() to extract the date components:
data _null_;
ObservationMonth ="2005/10";
date =mdy(substr(ObservationMonth,6,2),1,substr(ObservationMonth,1,4));
put date = date9.;
run;
Another option is to use the anydtdte informat (note that results may differ depending on your locale):
data _null_;
ObservationMonth ="2005/10";
date =input(ObservationMonth,anydtdte.);
put date = date9.;
run;
Yet another option is to modify the input to enable use of the YYMMDDw. informat:
data _null_;
ObservationMonth ="2005/10";
date =input(ObservationMonth!!'/01', YYMMDD10.);
put date = date9.;
run;
you're fantastic, guys! Thank you so much, with a "set" statement it works fine!
I have a variable that was entered as 31.01.2002 for all entries, and is a character. I would like to put it in numerical form with date9. .
I have tried the below:
date=input(oldway, 10.);
date=input(oldway, date9.);
put date=ddmmyy10.;
date=input(compress(oldway,'.'),10.);
date = INPUT(compress(oldway),date9.);
format date date9.;
run;
I have also tried combinations of the above and to no avail.
Any ideas for forward motion?
Kind Regards!!
You can't input your date using the date9. informat as your string variable isn't in that format. You can use ddmmyy10., though, and that also takes care of the . characters.
data have;
input old $10.;
cards;
31.01.2014
28.02.2014
01.01.2015
;
run;
data want;
set have;
new = input(old, ddmmyy10.);
format new date9.;
run;
try this:
data _null_;
date ="31.01.2014";
date=compress(date,".");
new_date=input(date,ddmmyy8.);
format new_date date9.;
put new_date;
run;