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 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 3 replies
  • 2807 views
  • 0 likes
  • 4 in conversation