Hi, I am seeking for help on formatting a date variable. Please see the example as below.
I would like to format the date1 variable from char $12. to num (length 8, final format DATETIME. and informat ANYDTDTM40). The solution I have thought about is firstly to remove the comma and make the date1 into 01Aug2018 format (then I know how to process further), but how can I achieve this first step (the underlined)?
Or, any other solutions/ suggestions?
Thank you very much. I really appreciate it.
/*1-raw data*/
data have;
input id date1 $12.;
datalines;
1 Aug 01, 2018
2 Aug 16, 2018
3 Jul 17, 2018
4 Sep 30, 2018
;
run;
/*2-solution?*/
data have;
input id date1 $12.;
datalines;
1 Aug 01, 2018
2 Aug 16, 2018
3 Jul 17, 2018
4 Sep 30, 2018
;
run;
data want;
set have;
new_d=input(date1,anydtdte21.);
format new_d date9.;
run;
You will need to create a new variable. DATE1 is already character, and can't be changed to numeric.
data want;
set have;
date1 = cat(scan(date1, 2, ', '). scan(date1, 1, ', '), scan(date1, 3, ', '));
date2 = input(date1, date9.);
format date2 date9.;
run;
Thank you so much! One typo after SCAN function - should be "," instead of "."
data have;
input id date1 $12.;
datalines;
1 Aug 01, 2018
2 Aug 16, 2018
3 Jul 17, 2018
4 Sep 30, 2018
;
run;
data want;
set have;
new_d=input(date1,anydtdte21.);
format new_d date9.;
run;
Thank you very much!
Why would you want to store values that are clearly DATE values into a DATETIME variable?
To do that you will need make up a timepart. Do you want to assume the timepart is zero (ie Midnight).
Somebody who uses R for the next step requires the datetime. format for date.
Thanks for the suggestions. It does not matter if R can handle it or not; it is just requirements.
So did you mean that I was not choosing a correct solution? But I did not see any error when transferring it back to DATE format, and so I chose it.
@cpulala wrote:
Hi, I am seeking for help on formatting a date variable. Please see the example as below.
I would like to format the date1 variable from char $12. to num (length 8, final format DATETIME. and informat ANYDTDTM40). The solution I have thought about is firstly to remove the comma and make the date1 into 01Aug2018 format (then I know how to process further), but how can I achieve this first step (the underlined)?
Or, any other solutions/ suggestions?
Thank you very much. I really appreciate it.
/*1-raw data*/
data have;
input id date1 $12.;
datalines;
1 Aug 01, 2018
2 Aug 16, 2018
3 Jul 17, 2018
4 Sep 30, 2018
;
run;
/*2-solution?*/
Unless you have some other program examining informats specifically there is seldom a reason to force the informat assignment. If so, the ANDTDTM would be a last choice as it indicates you didn't know what format your data was going to come from.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.