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
I have found my own answer.... silly me.
newvariable = input(sas_dt, best12.);
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.
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.
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 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.