datetime and date in same column - sas

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

Related

SAS date imported wrong

When I imported my excel sheet some dates imported differently than others. I tried to fix this with the code below to format the date.
DATA volume;
SET mice.volume;
format Date MMDDYY10.;
run;
However, I received the following error.
ERROR 48-59: The format $MMDDYY was not found or could not be loaded.
I had also tried with the following code
DATA volume;
SET mice.volume;
If date= 44138 then date= '11/3';
If date= 44141 then date= '11/6';
run;
NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column).
A Proc Contents shows the variable= Date type= Char Len=7 format=$7 Informat=$7 Label= Date
How do I fix this?
The date column being character having a mix of 'date looking' strings, and Excel date value numbers tells me some of the date values in your Excel are actually strings, such as '11/10 or ='11/10'.
The raw number 44138 is:
a SAS date value is 04-NOV-2080 (obviously not what is wanted)
an Excel date value 03-NOV-2020 (aha!)
03-NOV-2020 as SAS date value is 22222
an offset of -21916 from Excel
-21916 is the SAS date 30-DEC-1899
Date Epochs
An epoch is the date corresponding to a base number 0 in a systems calendar. SAS Base year is 1960 and Excel Base year is 1900.
Formatted
Number Actual Date Date Shown System/Format
------ ----------- ----------- ----------
0 31-DEC-1899 1/0/1900 Excel / Short Date (Formatter is weird at Epoch)
0 01-JAN-1960 01-JAN-1960 SAS / DATE11.
21916 01-JAN-1960 1/1/1960 Excel / Short Date
-21916 30-DEC-1899 31-DEC-1899 SAS / DATE11.
Notice the round trip is from 31-dec-1899 to 30-dec-1899. This due to an Excel 97 bug that has been carried forth for legacy reasons. See Microsoft's explanation in "Excel incorrectly assumes that the year 1900 is a leap year" which pushes the blame back even further to Lotus 1-2-3
The formula to convert between systems S1 and S2 date numbers is to add the # for the other systems epoch date (# # 0)
SAS#(date) = Excel#(date) + SAS#(Excel Epoch Date) - 1 (Excel leap year bug), or
sas_dt = excel_dt + '31-DEC-1899'd - 1; *or;
sas_dt = excel_dt + '30-DEC-1899'd;
What happened
Mixed value types in the Excel date column forced IMPORT to perceive the date variable as character.
The Excel cells with a date looking m/d string were brought in as the string
The Excel cells with a date, likely custom formatted as m/d, were brought in as the underlying Excel date number.
The ERROR
You tried to apply the date format MMDDYY. to the character variable Date.
A character column can not be assigned a numeric or date format, thus you get the
ERROR 48-59: The format $MMDDYY was not found or could not be loaded.
SAS automatically presumed MMDDYY. meant a character format $MMDDYY. because the variable type was character.
The Fix
You can convert the values in the character date column with code such as the following (untested):
if index(date,'/') then
date_fixed = input (trim(date)||'/2020', mmddyy10.);
else
date_fixed = input(date,best12.) + '30-DEC-1899'D;
format date_fixed yymmdd10.;
If you want to continue showing only mm/dd in SAS, use the format NLDATEM5.
format date_fixed NLDATEM5.;

Changing format from time to data time format in SAS

Currently , I have date column in time format ,I want to change it to date time stamp format I.e ( I want the date column to look like 12nov 2020 12:03:45:00 )
Could someone help me on this ?
According to #KurtBremser:
SAS dates are counts of days, SAS datetimes are counts of seconds.
datetime = dhms(date,0,0,0);
will convert a date to a datetime. Or multiply by 86400.
A column showing a time representation hh:mm:ss can be one of three things:
A character column type containing digit characters 0-9 and :
A number column type containing a SAS time value being displayed as hh:mm:ss with the time format TIME8.
A number column type containing a SAS datetime value being displayed as hh:mm:ss with the datetime format TOD.
This sample program demonstrates how different kinds of values can all look the same when viewed.
data have;
v1 = '12:34:56';
v2 = hms(12,34,56);
v3 = dhms(today(),12,34,56);
put v1= / v2= time8. / v3=tod. / v3=datetime18.;
run;
------ LOG ------
v1=12:34:56
v2=12:34:56
v3=12:34:56
v3=25NOV20:12:34:56
Only #3 has enough information in the raw value to be formatted as ddmmmyyy:hh:mm:ss
format myDate datetime18.;
#2 requires computing a new value assuming something about the date part
* supposing myDate contains only time values (00:00:00 to 23:59:59) for today;
myNewDate = dhms(today(),0,0,0) + myDate;
format myNewDate datetime18.;
#1 requires interpretation through INPUT and a date assumption
* supposing myDate contains "hh:mm:ss" for today
myNewDate = dhms(today(),0,0,0) + input(myDate,time8.);
format myNewDate datetime18.;

Converting to date5. format in SAS

I have a column which has mixed values of month and date (its in character $5 format).
date
7/23
5/23
23MAR
7/19
I want the data to come as uniform date5. format like this
date
23MAR
23MAY
23MAR
19JUL.
Here is the code that I'm using
data DAte_check4again;
set Date_2test;
format check_dt date5.;
check_dt=datepart(date);
run;
SAS stores DATE, TIME and DATETIME values as numbers. The DATEPART() function you are trying to use is for converting DATETIME values to DATE values. But your source variable is character with a length of 5. (FORMATs are just instructions for how to display values).
So your first problem will be to convert the string into a DATE value. You can then take the first 5 characters of the DATE. format and store that into either your original variable or some other variable. Assuming that the month/day values are for the current year and you only have those two styles of strings here is one method to generate a date and also the 5 character string.
data want;
set have ;
if index(date,'/') then date_ck = input(cats(date,'/',year(today())),mmmddyy10.);
else date_ck = input(cats(date,year(today())),date9.);
format date_ck date9.;
new_date = substr(put(date_ck,date9.),1,5);
run;

Convert ddmmyy date to datetime in sas

I want to convert date type 12/09/1991 to 12sep1991 00:00:00 in sas.tried using multiple formats but it is not working.
I have also checked with converting to numeric format.
Any help is appreciated
Dates are stored as the number of days since 1960. Datetime is stored as the number of seconds. There is not a format that can be used to display a date value as if it was a datetime value. You will need to convert the value instead.
You can use the dhms() function. Days, Hours, Minutes, Seconds.
So if your current variable is named date you can make a new variable named datetime.
datetime=dhms(date,0,0,0);
format datetime datetime20. ;
You could assign the value back to the original variable because both date and datetime values are stored as numbers.
select to_char(cast(sysdate as timestamp),'DD-MON-YYYY HH24:MI:SS.FF') from dual
Of course in the above the FF would also always be 000000
But if you have a timestamp variable you would not cast
select to_char(systimestamp,'DD-MON-YYYY HH24:MI:SS.FF') from dual

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 ;