I have a bunch of numeric data in this kind of format: 06NOV2019:00:00:00.000. I would like to get 06NOV2019. I tried to use put, but it did not work. Can anyone help with that?
If it's numeric with a datetime format you need the DATEPART() function which will extract the date portion alone for you.
Depending on what you're doing you could also apply a format.
data want;
set have;
date_var = datepart(datetimeVariable);
format date_var date9.;
run;
Here's a great, but longer and in depth, reference for dates and times in SAS
https://communities.sas.com/t5/SAS-Communities-Library/Working-with-Dates-and-Times-in-SAS-Tutorial/...
@JillChen0131 wrote:
I have a bunch of numeric data in this kind of format: 06NOV2019:00:00:00.000. I would like to get 06NOV2019. I tried to use put, but it did not work. Can anyone help with that?
If it's numeric with a datetime format you need the DATEPART() function which will extract the date portion alone for you.
Depending on what you're doing you could also apply a format.
data want;
set have;
date_var = datepart(datetimeVariable);
format date_var date9.;
run;
Here's a great, but longer and in depth, reference for dates and times in SAS
https://communities.sas.com/t5/SAS-Communities-Library/Working-with-Dates-and-Times-in-SAS-Tutorial/...
@JillChen0131 wrote:
I have a bunch of numeric data in this kind of format: 06NOV2019:00:00:00.000. I would like to get 06NOV2019. I tried to use put, but it did not work. Can anyone help with that?
So you a variable that contains the number of seconds since 1960 and you are currently use the DATETIME23.3 format to display it in ddMONyyyy:hh:mm:ss.uuu style?
Do you want to change how the value is displayed?
If you use the DTDATE9. format instead you will have it displayed in the style of ddMONyyyy.
Do you want to convert it from number of seconds into number of days? You can use the DATEPART() function to make that conversion. Then you could use the regular DATE9. format instead to display the number of days since 1960 in ddMONyyyy style.
If you just want to create a 9 character variable from your existing datetime values then use the PUT() function with the DTDATE9. format. You will need to create a new variable to do that since you cannot change the type of a variable. Although you can rename variables.
Example:
405 data test; 406 have='06NOV2019:00:00:00.000'dt; 407 format have datetime23.3 ; 408 want1 = have; 409 format want1 dtdate9.; 410 want2 = datepart(have); 411 format want2 date9.; 412 want3=want2; 413 format want3 yymmdd10.; 414 want4 = put(have,dtdate9.); 415 put 'Formatted' (_all_) (=/) 416 // 'RAW' / (have -- want3) (=comma14. /) 417 ; 418 run; Formatted have=06NOV2019:00:00:00.000 want1=06NOV2019 want2=06NOV2019 want3=2019-11-06 want4=06NOV2019 RAW have=1,888,617,600 want1=1,888,617,600 want2=21,859 want3=21,859
Thanks for the detailed explaination
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.