SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
Patrick
Opal | Level 21
Hi
I'm struggling to understand why date/datetime directives for picture formats don't work for me as documented (SAS 9.2 TS Level 2M3, X64_VSPRO platform).
It seems that directive %V and %W don't work at all, and that %U doesn't have a leading zero. Any ideas what's going wrong here?
That's what I find in the docu:
/**
%U
week number of the year as a decimal number (0,53) with leading 0. Sunday is considered the first day of the week.

%V
week number (01-53) with the first Monday as the start day of the first week. Minimum days of the first week is 4.

%W
week number (00-53) with the first Monday as the start day of the first week.
*/


That's the code I'm running:

proc format;
picture DTyyyyU
low-high='%Y%U' (datatype=datetime );
picture DTyyyy0U
low-high='%Y%0U' (datatype=datetime );
picture DTyyyyV
low-high='%Y%V' (datatype=datetime );
picture DTyyyy0V
low-high='%Y%0W' (datatype=datetime );
picture DTyyyyW
low-high='%Y%W' (datatype=datetime );
picture DTyyyy0W
low-high='%Y%0W' (datatype=datetime );

picture DyyyyU
low-high='%Y%U' (datatype=date );
picture Dyyyy0U
low-high='%Y%0U' (datatype=date );
picture DyyyyV
low-high='%Y%V' (datatype=date );
picture Dyyyy0V
low-high='%Y%0W' (datatype=date );
picture DyyyyW
low-high='%Y%W' (datatype=date );
picture Dyyyy0W
low-high='%Y%0W' (datatype=date );
run;

options ls=100;
data _null_;
put 10*'*' @12 'with datetime values' @33 60*'*';
do var='01jan2011 00:00:00'dt,'24apr2011 00:00:00'dt,'25apr2011 00:00:00'dt;
put '%Y%U: ' var DTyyyyU6. ' ' @;
put '%Y%0U: ' var DTyyyy0U6. ' ' @;
put '%Y%V: ' var DTyyyyV6. ' ' @;
put '%Y%0V: ' var DTyyyy0V6. ' ' @;
put '%Y%W: ' var DTyyyyW6. ' ' @;
put '%Y%0W: ' var DTyyyy0W6. ' ' ;
end;

put 10*'*' @12 'with date values' @29 64*'*';
do var='01jan2011'd,'24apr2011'd,'25apr2011'd;
put '%Y%U: ' var DyyyyU6. ' ' @;
put '%Y%0U: ' var Dyyyy0U6. ' ' @;
put '%Y%V: ' var DyyyyV6. ' ' @;
put '%Y%0V: ' var Dyyyy0V6. ' ' @;
put '%Y%W: ' var DyyyyW6. ' ' @;
put '%Y%0W: ' var Dyyyy0W6. ' ' ;
end;
put 92*'*';
run;

And that's the result I'm getting:
********** with datetime values ************************************************************
%Y%U: 20110 %Y%0U: 201100 %Y%V: 2011 %Y%0V: 2011 %Y%W: 2011 %Y%0W: 2011
%Y%U: 201117 %Y%0U: 201117 %Y%V: 2011 %Y%0V: 2011 %Y%W: 2011 %Y%0W: 2011
%Y%U: 201117 %Y%0U: 201117 %Y%V: 2011 %Y%0V: 2011 %Y%W: 2011 %Y%0W: 2011
********** with date values ****************************************************************
%Y%U: 20110 %Y%0U: 201100 %Y%V: 2011 %Y%0V: 2011 %Y%W: 2011 %Y%0W: 2011
%Y%U: 201117 %Y%0U: 201117 %Y%V: 2011 %Y%0V: 2011 %Y%W: 2011 %Y%0W: 2011
%Y%U: 201117 %Y%0U: 201117 %Y%V: 2011 %Y%0V: 2011 %Y%W: 2011 %Y%0W: 2011
********************************************************************************************

Thanks
Patrick
5 REPLIES 5
data_null__
Jade | Level 19
I don't think V and W directives have been implemented. Although I see them documented in my 9.2 documentation. There are formats in the NLS
[pre]weekU weekV and weekW[/pre]
that may be helpful.
Patrick
Opal | Level 21
Thanks data _null_
Really rare that the SAS docu gets it so wrong.
Ksharp
Super User
Hi.Patrick
Today I check something in SAS documentation.I found some interesting thing.
Maybe just as _null_ said, %V %W directives is only suitable for some local value and
local function.
[pre]
data temp;
date=19802; format date yymmdd10.;
a=nldate(date,'%Y%0V'); b=nldate(date,'%Y%0W');
put date= a= b=;
run;



76 data temp;
77 date=19802; format date yymmdd10.;
78 a=nldate(date,'%Y%0V'); b=nldate(date,'%Y%0W');
79 put date= a= b=;
80 run;

date=2014-03-20 a=201412 b=201411
[/pre]

Ksharp
Patrick
Opal | Level 21
Hi Ksharp
Weird!
So these directives are implemented somehow but work different than the other ones.
Good to know though.
Thanks
Patrick
Peter_C
Rhodochrosite | Level 12
looks like a "feature" to be dealt with or removed

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
  • 1568 views
  • 0 likes
  • 4 in conversation