BookmarkSubscribeRSS Feed
DarrylLawrence
Obsidian | Level 7

Good day

 

A datetime field was incorrectly stored (into a new table) as numeric 8.

 

Original Date value of 9 July 2019 is stored as 1878249600. I think this includes the date and time.

 

How do I convert the numeric field back to a datetime or date9 field.

4 REPLIES 4
PeterClemmensen
Tourmaline | Level 20

Simply use an appropriate format to display the value correctly. And use the Datepart Function to display the value as a date

 

data _null_;
    dt=1878249600;
    d =datepart(dt);
    put dt = datetime20. // d= date9.;
run;

Result:

 

dt=09JUL2019:00:00:00

d=09JUL2019
SASKiwi
PROC Star

Just apply a SAS datetime format to the variable. For example:

format MyDatetime datetime22.2;
DarrylLawrence
Obsidian | Level 7

Thanks Patrick

 

I tried it and it works.

 

Sometimes the simplest answer is the best. I was trying many different ways to convert the number to a date and in the end it just needed to be formatted.

 

 

Patrick
Opal | Level 21

SAS date and datetime values are stored as numeric and a length of 8 is correct as well. The number you see looks like a SAS datetime value which is nothing else than the count of seconds since 1/1/1960.

The only thing you need to do is apply a SAS format to the numerical column so your SAS datetime value becomes human readable. Here the docu explaining this much better than I can.

 

data test;
  dttm=1878249600;
  dttm2=dttm;
  dttm3=dttm;
  format dttm2 datetime21. dttm3 B8601DN10.;
  output;
  stop;
run;

proc print data=test;
run;

Capture.JPG

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 733 views
  • 0 likes
  • 4 in conversation