Convert %10.0g to date - stata

I have data with dates as:
ID Date1 Date2
1 1.929e+12 1.929e+12
2 1.917e+12 1.901e+12
3 1.922e+12 .
Based on other dates in the dataset, they should be in yyyy-mm-dd hh:mm:ss format.
Open to solutions within Stata or using different software.

Your values are already date-times so all you need to do is assign a date format starting %tc. Here are examples using display on one of your scalar values:
. di %tc 1.929e+12
15feb2021 09:20:00
. di %tcCCyy-NN-dd_HH:MM:SS 1.929e+12
2021-02-15 09:20:00
See
help datetime_display_formats
help format
for how to select a datetime format and assign it to a variable.
For example,
format Date1 %tcCCyy-NN-dd_HH:MM:SS

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

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

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

Excel to SAS Date not working in SAS

I Have a file from excel that is in a short date format, but when SAS reads it in, it turns it into numbers in the 4000 range...when I try and convert this to an excel date with the below formula, it turns the year into 2077...is there a formula to ensure that this date remains in the original format on the read in, or avoid it turning into this 4000 range that is not at all close to the 2017 and 2018 year that my file is starting in. Does that make sense?
data change_date;
format Completed_Date mmddyy8. ;
set check;
completed_date = date_completed;
if 42005 => date_completed >=43466 and date_completed ^=. then
Completed_date = Date_Completed-21916; *commented out 12-21-17 Xalka
dates back to how they are expected;
run;
I am pretty sure this is a duplicate question, but I can't find it.
This is usually caused by mixing character and date values in the same column. This made SAS import the data as a character variable and it results in the actual dates being copied as character versions of the integers that Excel uses to store dates.
Frequently this is caused by entries that look like dates but are really character strings in the Excel file. The best way to fix it is to fix the Excel file so that the column only contains dates. Otherwise you just need to convert the strings to integers and adjust the values to account for the differences in index dates.
So if your values are in a SAS dataset named HAVE in the character variable DATESTRING then you could use this data step to create a new variable with an actual date value.
data want ;
set have ;
if indexc(datestring,'-/') then date=input(datestring,anydtdte32.);
else date = input(datestring,32.) + '01JAN1900'D -2;
format date yymmdd10. ;
run;
The minus 2 is because of difference in whether to start numbering with 1 or 0 and because Excel thinks 1900 was a leap year.
Excel and SAS have different default dates in back-end.
Day 0 in SAS is 1 January 1960 and Day 0 in Excel is 1 January 1900.
So, you will need to convert excel numeric date to sas date using the below formula.
SAS_date = Excel_date - 21916;
data dateExample;
informat dt mmddyy8.;
set dates;
SAS_date = dates - 21916;
dt=sas_Date;
format dt date9.;
run;

In Stata, how can I change the format of a date from "2010-01-11 00:00:00" to "1/11/2010"?

I am currently trying to change the format of a date from "2010-01-11 00:00:00" to "01-11-2010" or "1/11/2010". Currently "2010-01-11 00:00:00" is in a string format. I have tried to coerce using the date() function but it never returns to the point where Stata can recognize and sort. Would anyone have any idea how to do this?
It's best if for future questions you post attempted code and why it's not working for you.
Maybe this works in your case:
clear all
set more off
*----- example data -----
set obs 1
gen dat = "2010-01-11 00:00:00"
describe
list
*----- what you want -----
gen double dat2 = clock(dat, "YDM hms")
format dat2 %tcDD-NN-YY
describe
list
Note that we go from string type to numeric type (double), and then adjust the display format.
See help format, help datetime and help datetime_display_formats.
Read also:
Stata tip 113: Changing a variable's format: What it does and does not mean
N. J. Cox. 2012.
Stata Journal Volume 12 Number 4.
http://www.stata-journal.com/article.html?article=dm0067
If you are ingesting time data in "2010-01-11 00:00:00" (SQL) format, then by default it is ingested into Stata as a str23
If you would like it as a Stata date format to manipulate, you could try the following (ingested_date_1 ... being your date columns)
foreach sqltime in ingested_date_1 ingested_date_2 {
rename `sqltime' X
generate double `sqltime' = clock(X, "YMD hms")
drop X
format %tcDDmonCCYY_HH:MM:SS `sqltime'
}
This, takes in multiple "dates", just replace your column names with ingested_date_1 ingested_date_2 etc and reformats them and keeps their 'original' name
Now the dates are in a stata recognised time format, %tc based of the clock, this will be sorted in the time-sense like you expect, rather than the ingested string which was not.
Additionally you may now reformat the display of the date to something that you would like or are comfortable reading, although it will make no difference to date manipulation, it is just the displayed appearance, in the case of viewing as "01-11-2010"
as Roberto says
format ingetsed_date_i %tcDD-NN-YY