Convert datetimes character to numeric

Reply
Contributor
Posts: 52

Convert datetimes character to numeric

[ Edited ]

I have two data sets I want to merge based on a variable sas_dt.  Both were imported from CSV files at one point.

On the first dataset the variable sas_dt has the column attributes Format - BEST12., informat 12., numeric, and if you look at the table in the library it lists sas_dt as an integer, that is ultimately the number of seconds.

 

On the other dataset the variable sas_dt has the column attributes Format - datetime16. Informat 16., numeric, and if you look at the table in the library it lists sas_dt formated as the date.

 

When I print

 

proc print;

    format sas_dt datetime.;


Example of the integer I want to use as a commonality between all tables would look like be 1820225812, 18202225813, etc.

 

They list the same dates and times.


I need to merge these datasets, but this seems to be the hangup.  I also wish to work with the integer format.

How to I convert sas_dt to the integer format so that it is stored in my library as an integer (i.e. I click on the table and it is displayed/stored as an integer)?

And just for the sake of asking, how do I convert an integer so it is stored in my library as a datetime (i.e. I click on the table and it is displayed/stored as a date and time)?

 

Thank you

Contributor
Posts: 52

Re: Convert datetimes character to numeric

I have found my own answer.... silly me.

newvariable = input(sas_dt, best12.); 

Super User
Super User
Posts: 9,599

Re: Convert datetimes character to numeric

Is the variable a numeric or is it a date?  Makes no sense if you have a date variable to "convert it to numeric", convert it to a date<time>.  If its just a numeric, don't call it a date.

Contributor
Posts: 52

Re: Convert datetimes character to numeric

Those are the column attribute designations when you right click on the column when the table is open.

Lists it as numeric, displays it as a date. 

This seems to be a recent thing because whenever I've used datetime. in the past it has always been an integer.

At any rate, the problem is solved well enough for now.  If there is a more elegant solution, I'd be happy to hear it.

Super User
Super User
Posts: 8,069

Re: Convert datetimes character to numeric

[ Edited ]

If they are both numeric (date, time and datetime values are numbers) then there is no need to convert anything. Just make sure to apply the format you want in the resulting merged dataset so that humans can understand the values when they are printed.

 

data want ;
   merge not_formatted formatted ;
   by sas_dt ;
   format sas_dt datetime20. ;
run;

 

Ask a Question
Discussion stats
  • 4 replies
  • 76 views
  • 0 likes
  • 3 in conversation