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.
bg12
Calcite | Level 5

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

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

5 REPLIES 5
PaigeMiller
Diamond | Level 26

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
bg12
Calcite | Level 5

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

 

 

PaigeMiller
Diamond | Level 26

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
Astounding
PROC Star

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;

bg12
Calcite | Level 5
Thank you!

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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
  • 1051 views
  • 0 likes
  • 3 in conversation