BookmarkSubscribeRSS Feed
iiibbb
Quartz | Level 8

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

4 REPLIES 4
iiibbb
Quartz | Level 8

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

newvariable = input(sas_dt, best12.); 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

iiibbb
Quartz | Level 8

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.

Tom
Super User Tom
Super User

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;

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 432 views
  • 0 likes
  • 3 in conversation