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-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

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!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 21380 views
  • 2 likes
  • 4 in conversation