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

Hi! I would like to seek your help to solve my question!

My existing variable format is Char "2018-08-31 00:00:00" and I want to convert it to SAS Date9. format. Please see my code as below:

 

Data Temp;

set T;

format AA date9.;

AA=input(char,MMDDYY10.);

run;

 

Really hope you can help me! Thanks a lot!

 

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26
data want;
  char_date="2018-08-31 00:00:00";
  num_date=input(substr(char_date,1,10),yymmdd10.);
  format num_date date9.;
run;

View solution in original post

5 REPLIES 5
RW9
Diamond | Level 26 RW9
Diamond | Level 26
data want;
  char_date="2018-08-31 00:00:00";
  num_date=input(substr(char_date,1,10),yymmdd10.);
  format num_date date9.;
run;
Kurt_Bremser
Super User

Actually, the informat "substrings" on its own:

data have;
input char :$20.;
cards;
2018-08-31 00:00:00
;
run;

data want;
set have;
format aa date9.;
aa = input(char,yymmdd10.);
run;
RW9
Diamond | Level 26 RW9
Diamond | Level 26

True, but you know my thoughts on implicit anything.

Reeza
Super User

Here's an example of how to:

 

1. Read it in as date time

2. Extract date portion

3. Extract time portion

4. Keep as datetime and format as date to view

 

data have;
x="2018-08-31 00:00:00";

*import and convert to date time variable;
y_datetime=input(x, anydtdtm.);

*get only the date part;
y_date=datepart(y_datetime);

*get only the time part;
y_time=timepart(y_datetime);

*keep as datetime and format as date;
Y_formatted = Y_datetime;

format y_datetime datetime. y_date date9. y_time time. y_formatted dtdate9.;
run;

proc print data=have;run;

SAS Output

Obs x y_datetime y_date y_time Y_formatted
1 2018-08-31 00:00:00 31AUG18:00:00:00 31AUG2018 0:00:00 31AUG2018

 


@New_SAS_user76 wrote:

Hi! I would like to seek your help to solve my question!

My existing variable format is Char "2018-08-31 00:00:00" and I want to convert it to SAS Date9. format. Please see my code as below:

 

Data Temp;

set T;

format AA date9.;

AA=input(char,MMDDYY10.);

run;

 

Really hope you can help me! Thanks a lot!

 


 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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