DATA Step, Macro, Functions and more

display datetime with missing day/month/second

Reply
Contributor
Posts: 25

display datetime with missing day/month/second

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

Super User
Posts: 5,424

Re: display datetime with missing day/month/second

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
Regular Contributor
Posts: 237

Re: display datetime with missing day/month/second

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.

Frequent Contributor
Posts: 106

Re: display datetime with missing day/month/second

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

Ask a Question
Discussion stats
  • 3 replies
  • 767 views
  • 0 likes
  • 4 in conversation