SAS Datetime25.6 to Character YYYYMMDD - sas

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

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

How to convert dd-mmm-yyyy to date9 in sas

Want to convert date = 01-SEP-2021 to date = 01SEP2021. I used this code to convert:
New date = input(compress(date,'-'),date9.);
But that didn't work. I also used substring to remove the parts of day month and year but seems this method is a bit lengthy.
If the type is numeric and SAS already displays it as a date then it's a formatting issue.
proc datasets lib=work nolist;
modify have;
format date date9.;
quit;
Otherwise, it must be a character and you have to convert it to a date and display it in date9. format.
data want;
date = '01-SEP-2021';
new_date = input(date, anydtdte.); *anydtdte. automatically reads a variety of date formats;
format new_date date9.;
run;

datetime and date in same column

I have a question. I have a field in my SAS dataset that has a mixture of datetime and date variables.
The field is PST_DT and is Type: Numeric. Group: Date. Format: Date9. Length: 8.
Some values look like this:
PST_DT
8/22/2018 11:59:59 PM
8/22/2018
How can I turn just the datetime values in date format? I want all the values to be in date format.
Thanks.
The question does not make much sense as a date variable (number of days since 1960) cannot have a time component at all, much less have it selectively.
If you have a DATETIME value (number of seconds since 1960) and want to convert it to a date value use the datepart() function. And attach a date format so that the value displays in a human friendly way.
pst_date = datepart(pst_dt);
format pst_date yymmdd10. ;
If you have instead a character string then use the ANYDTDTE. informat to convert it to a date value.
pst_date = input(pst_dt,anydtdte40.);
format pst_date yymmdd10. ;

Convert number to date format in SAS

What I have
I have a column/variable named Release_Date with length 8 format 11. and informat 11.
The observation in that column are in the form YYYYMMDD SAS actually reads them as pure number
What I want
I want to convert those observation in DD/MM/YYYY format with format and informat as DDMMYY10.
Example
Release_Date is 20180612 then I want 12/06/2018
Any help with this regard will be appreciated
Convert to character
Read back in as date
make sure the variable is different since SAS does not allow you to change a variable type
releaseDate_NUM = input(put(Release_Date, 8. -l), yymmdd10.);
The following solution creates a simple dataset, then processes that dataset to add the converted date:
data myDates;
input intDate;
datalines;
20110101
20120202
;run;
data myDates;
set myDates;
format cvtDate mmddyy10.; * create new variable by specifying the format;
cvtDate = input(put(intDate,8.),yymmdd8.); * assign new variable value;
run;
Results should be:
intDate cvtDate
-------- ----------
20110101 01/01/2011
20120202 02/02/2012

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 ;