BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
cpulala
Obsidian | Level 7

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?*/

 

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20
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;

View solution in original post

9 REPLIES 9
Astounding
PROC Star

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;

cpulala
Obsidian | Level 7

Thank you so much! One typo after SCAN function - should be "," instead of "."

novinosrin
Tourmaline | Level 20
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;
cpulala
Obsidian | Level 7

Thank you very much!

Tom
Super User Tom
Super User

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).

cpulala
Obsidian | Level 7

Somebody who uses R for the next step requires the datetime. format for date. 

Tom
Super User Tom
Super User
I am pretty sure that R can handle reading DATE variables from SAS. I certainly hope so since you marked a solution that is create a DATE variable as the correct solution for your problem.
In SAS a date is stored as the number of days and a datetime is stored as the number of seconds. You can convert from DATETIME to DATE using the DATEPART() function. You can convert from DATE to DATETIME use the DHMS() function (Days,Hours,Minutes,Seconds) by using the date value for the first argument and whatever time of day you want to use for the Hour, Minute and Second arguments. Normally you would use zeros.
cpulala
Obsidian | Level 7

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. 

 

ballardw
Super User

@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.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 9 replies
  • 1688 views
  • 2 likes
  • 5 in conversation