SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
zoomzoom
Obsidian | Level 7

Hello members,

I'm a new member joined. I know the question I'm going to ask might have been asked n number of times. But so far I'm unable to fix the problem.

Problem:

I'm trying to convert SAS Date in DHMS format to SAS Date Date9 format. The date came in from SQL server and in DDMMMYYYY:00:00:00.000 format.

The date I have is 10JAN2011. I have converted to numeric and to character and I get the value 18637 in character format. But then, I'm unable to convert to date9 format. All I get is missing values with error message as "Invalid argument to function Input". I also tried with sasdatefmt in standard syntax, but the code is not working

Code :

data want;

set have;

dt= input(put(datepart(Date),5.),ddmmyy10.);

format dt Date9.;

run;

Thank you.

1 ACCEPTED SOLUTION

Accepted Solutions
esjackso
Quartz | Level 8

Not sure its clear what you are trying to do.

If all you need is the date to show as 10Jan2011 then you do not need to use the put function (just the datepart) and just associate the format with the variable. If you are converting to a character variable then I think you need to just replace the 5. format in the put with the date9. format.

EJ

EDIT:

Sample code to illustrate

data temp;

  x = 18637;

  format x date9.;

  x2= put(x,date9.);

  y='10Jan2011 12:00:00'dt;

  y2 = datepart(y);

  format y2 date9.;

  y3 = put(y2,date9.);

run;

View solution in original post

7 REPLIES 7
data_null__
Jade | Level 19

18637 is 10Jan2011 nothing else to do except associate a format.

17         data _null_;

18            x = 18637;

19            put x=date9.;

20            run;

x=10JAN2011

zoomzoom
Obsidian | Level 7

Thanks for the reply.

So I have modified the code to:

data want;

set have;

dt= put(datepart(Date),5.);

format dt date9.;

run;

But the error exits. The log says:

"Note 484-185: Format $Date was not found or could not be loaded".

esjackso
Quartz | Level 8

Not sure its clear what you are trying to do.

If all you need is the date to show as 10Jan2011 then you do not need to use the put function (just the datepart) and just associate the format with the variable. If you are converting to a character variable then I think you need to just replace the 5. format in the put with the date9. format.

EJ

EDIT:

Sample code to illustrate

data temp;

  x = 18637;

  format x date9.;

  x2= put(x,date9.);

  y='10Jan2011 12:00:00'dt;

  y2 = datepart(y);

  format y2 date9.;

  y3 = put(y2,date9.);

run;

zoomzoom
Obsidian | Level 7

Thank you, esjackso1. That was right, the code is working fine.

DBailey
Lapis Lazuli | Level 10

The DTDATE9. format display DDMMMYYYY when applied to a datetime variable.

zoomzoom
Obsidian | Level 7

Thank you, DBailey. Your answer works just fine.

data_null__
Jade | Level 19

The date I have is 10JAN2011. I have converted to numeric and to character and I get the value 18637 in character format.

Do NOT convert to character.  When you get the NUMBER 18637 you are done.  All you need after that is to associate a date format.

Message was edited by: data _null_

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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
  • 7 replies
  • 4586 views
  • 6 likes
  • 4 in conversation