- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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!
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content