Date Format Issue

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

Date Format Issue

[ Edited ]

I'm using SAS 9.4. I have two variables that are formatted as DATETIME20. I want to change the format to DATE9.

 

data want;
	
set have;

format predate date9. postdate date9.;

run;

The code works for the second half of the dataset, but the first half changes to "*******". I cannot figure out what I'm missing. Any help appreciated!

screenshot.png


Accepted Solutions
Solution
‎10-29-2017 06:28 PM
Super User
Posts: 6,005

Re: Date Format Issue

If your variables are currently datetimes, you will need to convert them to dates to be able to print them properly in a date format.  Note that by doing so, you may lose some information.  Datetimes are more specific, and refer to a particular second, while dates refer to a particular day.  Assuming you want to go ahead with this, the code would be:

 

data want;

set have;

predate = datepart(predate);

postdate = datepart(postdate);

format predate postdate date9.;

run;

 

Also consider that the current values of PREDATE and POSTDATE might actually be a mix ... some dates and some datetimes.  You might have to consider which observations need to apply DATEPART and which don't.  For example, you might use:

 

data want;

set have;

if predate > '01Jan3000'd then predate = datepart(predate);

if postdate > '01Jan3000'd then postdate = datepart(postdate);

format predate postdate date9.;

run;

View solution in original post


All Replies
Respected Advisor
Posts: 2,173

Re: Date Format Issue

Either you need to make the columns in your table wider ... or the data is not a valid SAS date.

 

By the way, how are you viewing these columns? What exact viewer are you using to see this data?

--
Paige Miller
New Contributor
Posts: 3

Re: Date Format Issue

Posted in reply to PaigeMiller

I'm not sure what you mean... but here's a screenshot before I try to reformat them. In regards to viewer, I'm using results viewer.

 

screenshot2.png

 

 

Respected Advisor
Posts: 2,173

Re: Date Format Issue

Your data down to row row 190 is indeed SAS datetimes, and so as explained above, you need to convert them to SAS datees. Then after that the data is probably SAS dates, but formatted as datetimes, and so you get a date of 01JAN60. Somehow, your data is mismatched, some are dates and some are datetimes. You need to correct this first.

--
Paige Miller
Solution
‎10-29-2017 06:28 PM
Super User
Posts: 6,005

Re: Date Format Issue

If your variables are currently datetimes, you will need to convert them to dates to be able to print them properly in a date format.  Note that by doing so, you may lose some information.  Datetimes are more specific, and refer to a particular second, while dates refer to a particular day.  Assuming you want to go ahead with this, the code would be:

 

data want;

set have;

predate = datepart(predate);

postdate = datepart(postdate);

format predate postdate date9.;

run;

 

Also consider that the current values of PREDATE and POSTDATE might actually be a mix ... some dates and some datetimes.  You might have to consider which observations need to apply DATEPART and which don't.  For example, you might use:

 

data want;

set have;

if predate > '01Jan3000'd then predate = datepart(predate);

if postdate > '01Jan3000'd then postdate = datepart(postdate);

format predate postdate date9.;

run;

New Contributor
Posts: 3

Re: Date Format Issue

Posted in reply to Astounding
Thank you!
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 162 views
  • 0 likes
  • 3 in conversation