How to transform SAS String Value into Timestamp? - sas

how to transform this string value into a timestamp format like dd.mm.yyyy hh:mm?
17.02.22 08:06 (Format dd.mm.yy hh:mm)
This is not working:
format BEGINN_TMP DATETIME19.;
BEGINN_TMP = input(BEGINN, DATETIME19.);

That is because the string's format is not in datetime. format. datetime. looks as follows:
22FEB17:08:06:00
We'll use the anydtdtm. informat to read it, which automatically tries a variety of time formats to find the right one.
data want;
string = '17.02.22 08:06';
datetime = input(string, anydtdtm.);
format datetime datetime.;
run;
Output:
string datetime
17.02.22 08:06 22FEB17:08:06:00

Related

Converting a date string with leading year into a data in SAS

I've had various success with the input statement converting character strings into dates, but when the date is formatted YY/MM/DD, I can't find the right INFORMAT to get INPUT to convert it to a number.
e.g.
data test;
format date DDMMYY10.;
date_string = "22/11/07";
date_no = input(date_string, YYMMDDw.);
date = date_no;
run;
I've tried using the list of SAS INFORMATs but non of them seem to work when converting this type of date. I can only get INPUT to work with formats like DDMMYY10.
Expected output would be:
date_string "22/11/07"
date_no 22956
date 07/11/2022
Code:
data test;
format date DDMMYY10.;
date_string = "22/11/07";
date_no = input(date_string, YYMMDD8.);
date = date_no;
put date=;
run;
Log:
date=07/11/2022

SAS: Converting numeric to character values

I am trying to convert datatime20. from numeric to character value.
Currently I have numeric values like this: 01Jan200:00:00:00 and I need to convert it to character values and received output like: 2020-01-01 00:00:00.0
What format and informat should be used in aboved ?
I have tried used PUT function to convert numeric to character and tried many option, each time receiving other format. Should be also use DHMS function before PUT ?
There is not a native format that produces that string exactly. But it it not hard to build it in steps using existing formats. Or you could use PICTURE statement in PROC FORMAT to build your own format.
If you don't really care about the time of day part of the datetime value then this is an easy and clearly understand way to convert the numeric variable DT with number of seconds into a new character variable in that style. Use DATEPART() to get the date (number of days) from the datetime value and then use the YYMMDD format to generate the 10 character string for the date and then just append the constant string of the formatted zeros.
length dt_string $21.;
dt_string = put(datepart(dt),yymmdd10.)||' 00:00:00.0';
If you need the time of day part then you could also use the TOD format.
dt_string = put(datepart(dt),yymmdd10.)||put(dt,tod11.1);
Or you could use the format E8601DT21.1 and then change the letter T between the date and time to a space instead.
dt_string = translate(put(dt,E8601DT21.1),' ','T');
If you want to figure out what formats exist for datetime values and what the formatted results look like you could run a little program to pull the formats from the meta data and apply them to a specific datetime value.
data datetime_formats;
length format $50 string $80 ;
set sashelp.vformat;
where fmttype='F';
where also fmtinfo(fmtname,'cat')='datetime';
keep format string fmtname maxw minw maxd ;
format=cats(fmtname,maxw,'.','-L');
string=putn('01Jan2020:01:02:03'dt,format);
run;
A custom format can be defined to return the result of a user defined function. Docs
proc format;
value <format-name> (default=<width>)
other = [<function-name>()]
;
run;
Example:
options cmplib=(sasuser.functions);
proc fcmp outlib=sasuser.functions.temporal;
function E8601DTS (datetime) $21;
return (
translate (putn(datetime,'E8601DT21.1'),' ','T')
);
endsub;
run;
proc format;
value E8601DTS (default=21)
other = [E8601DTS()]
;
run;
data have;
do dt = '01jan2020:0:0'dt to '10jan2020:0:0'dt by '60:00't;
output;
end;
format dt datetime16.;
run;
ods html file='function-based-format.html';
proc print data=have(obs=4); title 'stock E8601DT';
proc print data=have(obs=4); title 'custom E8601DTS';
format dt E8601DTS.;
run;
ods html close;

SAS How convert string to time, when load data from csv?

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.;

SAS Datetime25.6 to Character YYYYMMDD

Original Value is in DATETIME25.6
I need to change it to YYYYMMDD
I am using the below in a datastep;
'Date of Birth'n = put(borrower_dob,yymmddn8.);
However it returns ******** as the value. Help!
The date part of a SAS datetime value can be output with the format B8601DN8.
From docs
B8601DNw.Format
Writes dates from datetime values by using the ISO8601 basic notation yyyymmdd.
data want;
dtnow = datetime();
format dtnow datetime20.;
put dtnow= B8601dn8. 'is the yyyymmdd rendering of ' dtnow=;
yyyymmddstring = put (dtnow, B8601DN8.);
run;
----- LOG -----
dtnow=20191003 is the yyyymmdd rendering of dtnow=03OCT2019:10:02:14
You need just the date part(days) of the date time(seconds)variable. Check the documentation for explanation of SAS date and date-time variables.
'Date of Birth'n = put(DATEPART(borrower_dob),yymmddn8.);

How to convert Datetime into SAS format

I am working with a huge number of observations in different tables in different versions.
I will use a date %let date_to_view = "20JAN2014:16:10"dt;
But how to convert this date into SAS format?
I knew how to Convert SAS data type (use proc sql):
dhms("01JAN1970'd,3,0,i.valid_dttm/1000) format datetime20.
I see date 20JAN2014:16:34:10 is 1390224849927 but how to convert it into code?
In your formula dhms("01JAN1970'd,3,0,i.valid_dttm/1000) you are converting a number that represents the number of milliseconds since 01JAN1970 to a SAS datetime value that represents the number of seconds since 01JAN1960. You also appear to be adding 3 hours.
So it sounds like your question is how to convert a SAS DATETIME value into a Unix timestamp value. So just reverse the arithmetic.
Your formula to convert from a Unix timestamp to a SAS datetime was:
sasdt2 = '01JAN1970:00:00'dt + '03:00't + unix_timestamp2/1000 ;
So to convert from a SAS datetime value to a Unix timestamp use:
unix_timestamp1 = 1000*(sasdt1 - '01JAN1970:00:00'dt - '03:00't) ;
"20JAN2014:16:10"dt is already in the correct SAS date (datetime) format, but as a date literal. SAS stores this as a number, representing the number of seconds since 01JAN1960:00:00:00.
If you just want the date component of the datetime, use the datepart() function, and format the result accordingly, e.g. date9..
data want ;
dt = "20JAN2014:16:10"dt ;
date = datepart(dt) ;
format dt datetime19. date date9. ;
/* To have 'date' show as the unformatted value, simply remove the format */
format date best32. ;
run ;