BookmarkSubscribeRSS Feed
Peter_Y
Calcite | Level 5

Hello:

  I am working on a program to display datetime variables with missing day/month/second information. The questions I have are

  1. How to display a datetime as "01JAN2006/09:25". i.e replace the colon separating the date and time by slash and remove the second.
  2. How to display a datetime as "JAN2006/Unknown" or "2006/Unknown" when day or month is missing?

Thanks,

Peter

3 REPLIES 3
LinusH
Tourmaline | Level 20

First, to make use of SAS datetime values, a part of the datetime can't be 'missing'. The same should be valid from any reliable source system. If your datetime values have missing part, I assume they are in char format. This sounds like a candidate for char tweaking, not nay processing that includes SAS dateime function/formats.

What does your input look like?

1) In this case, convert to SAS datetime value by using a standard informat (depending on the format of your input data), Now, you can write your own picture format, which let's you do some formatting/logic, there are quite few directives that help you manipulate datetime values.

2) Can't se how this could work wit SAS date/datetime values, since they are numerical values that count days/seconds from 1Jan19060. So, here you probably need to do some character manipulating logic.

Data never sleeps
djbateman
Lapis Lazuli | Level 10

I don't know if what you are trying to do is possible and still retaining the numeric aspect of date/time variables.  You would have to change them to character values and then do a "find and replace" (probably using the SUBSTR() function).  But this takes away all ability to use dates and time for their true nature.  You won't be able to properly sort or find differences between dates any more.

pradeepalankar
Obsidian | Level 7

hi please see if this is what you want;program will also take care of consecutive missing months.

data test;

input date_have :datetime21.;

format date_have datetime21.;

diff=dif(datepart(date_have));

if dif(datepart(date_have))>1 then

do i=1 to diff-1;

date_have=date_have;

output;

date_have=date_have-24*60*60;

missing=1;

end;

output;

cards;

01DEC2008:09:22:25

02DEC2008:10:23:45

04DEC2008:10:23:45

08DEC2008:10:23:45

10JAN2009:10:23:45

20FEB2009:10:23:45

;

proc sort data=test;

by date_have;

run;

data test(keep=date_want);

set test;

date_have_char=put(date_have,datetime21.);

date_want=catx('/',scan(date_have_char,1,':'),catx(':',scan(date_have_char,2,':'),scan(date_have_char,3,':'))) ;

if missing=1 then date_want=catx('/',put(datepart(date_have),monyy7.),'UNKNOWN');

run;

OUTPUT:

--------------

date_want=01DEC2008/09:22

date_want=02DEC2008/10:23

date_want=DEC2008/UNKNOWN

date_want=04DEC2008/10:23

date_want=DEC2008/UNKNOWN

date_want=DEC2008/UNKNOWN

date_want=DEC2008/UNKNOWN

date_want=08DEC2008/10:23

.....

...

...

date_want=JAN2009/UNKNOWN

date_want=10JAN2009/10:23

.....

...

...

date_want=FEB2009/UNKNOWN

date_want=20FEB2009/10:23

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 2489 views
  • 0 likes
  • 4 in conversation