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

Hi everyone,

 

Please help me with this query. I want to convert this date 20DEC2019:12:49:00.000 to 20DEC2019.

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Why did you use PUT() to convert it to character if you didn't want a character variable?

Might help to use more descriptive names for the variables.

data test;
  set hello;
  AppDate1 = datepart(AppDate);
  format appdate1 date9.;
  rename appdate=AppDatetime appdate1=AppDate ;
run;

View solution in original post

7 REPLIES 7
PaigeMiller
Diamond | Level 26

Use the DATEPART() function and apply the DATE9. format 

--
Paige Miller
MagD
Quartz | Level 8
Hey Paige

I used this data step:

data test;
set hello;
AppDate1 = put(datepart(AppDate),date9.);
run;

AppDate1 comes out as desired (20DEC2019) but it gets converted into a character variable and I need it as a date.
Tom
Super User Tom
Super User

The first one looks like a DATETIME value, not a DATE value.

SAS stores DATETIME values as number of seconds since 1960 and DATE values as number of days since 1960.

 

To convert the number of seconds into number of days you can use the DATEPART() function.  You will then need to use a  date type format, like the DATE format that display in the style you showed, with the new value.

 

If you just want to change how the value is displayed you can just change the FORMAT used to display the value without changing the value that is stored.  The DTDATE format will display datetime values in the style that the DATE format displays date values.

MagD
Quartz | Level 8
Hey Tom,

I used this data step:

data test;
set hello;
AppDate1 = put(datepart(AppDate),date9.);
run;

AppDate1 comes out as desired (20DEC2019) but it gets converted into a character varaible and I need it as a date.
Tom
Super User Tom
Super User

Why did you use PUT() to convert it to character if you didn't want a character variable?

Might help to use more descriptive names for the variables.

data test;
  set hello;
  AppDate1 = datepart(AppDate);
  format appdate1 date9.;
  rename appdate=AppDatetime appdate1=AppDate ;
run;
MagD
Quartz | Level 8
Thanks Tom, it worked perfectly.
Kurt_Bremser
Super User

The PUT function takes stuff (character or numeric), and converts it to character according to the used format.

You do not want it here. Just use the DATEPART function and a FORMAT statement:

data test;
set hello;
AppDate1 = datepart(AppDate);
format Appdate1 date9.;
run;

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!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 7 replies
  • 630 views
  • 2 likes
  • 4 in conversation