BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
yelkenli
Calcite | Level 5

i am having trouble with a simple format need.  

My first attempt (which was with CATS and no PUT statement) resulted in single digit weeks for weeks 1 through 9.  

The below results in a single digit week number with a blank space where a zero should be.  The rest of data manipulations work but I would prefer to have the zero.  

The remaining data manipulations provide a table showing a volume over a time line by week (across years, etc.).  a second path might be to skip creating this variable and present the data some other way but I have not see that alternate path.    so help with keeping the zero is appreciated. 

 

 

create table want AS
SELECT 
      some variables
      ,CAT('Yr', year(datepart(a_date)) , 'FW', put(week(datepart(a_date),'v'), 2.) ) AS FW_filter

from atable; 

 

 

results are as such:

Yr2017FW 1

Yr2017FW 2

etc.

 

would like 

Yr2017FW01

Yr2017FW02

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Change 2. to Z2, which keeps the leading 0's. 

 

CAT('Yr', year(datepart(a_date)) , 'FW', put(week(datepart(a_date),'v'), Z2.) ) 

Or use a custom format instead.

Create format:

 

proc format;
picture myCustomFmt (default=20)  low-high = 'Yr%YFW%0U' (datatype=datetime ) ;
run;

use format:

put(a_date, myCustomFmt.) as FW_filter

@yelkenli wrote:

i am having trouble with a simple format need.  

My first attempt (which was with CATS and no PUT statement) resulted in single digit weeks for weeks 1 through 9.  

The below results in a single digit week number with a blank space where a zero should be.  The rest of data manipulations work but I would prefer to have the zero.  

The remaining data manipulations provide a table showing a volume over a time line by week (across years, etc.).  a second path might be to skip creating this variable and present the data some other way but I have not see that alternate path.    so help with keeping the zero is appreciated. 

 

 

create table want AS
SELECT 
      some variables
      ,CAT('Yr', year(datepart(a_date)) , 'FW', put(week(datepart(a_date),'v'), 2.) ) AS FW_filter

from atable; 

 

 

results are as such:

Yr2017FW 1

Yr2017FW 2

etc.

 

would like 

Yr2017FW01

Yr2017FW02


 

View solution in original post

2 REPLIES 2
Reeza
Super User

Change 2. to Z2, which keeps the leading 0's. 

 

CAT('Yr', year(datepart(a_date)) , 'FW', put(week(datepart(a_date),'v'), Z2.) ) 

Or use a custom format instead.

Create format:

 

proc format;
picture myCustomFmt (default=20)  low-high = 'Yr%YFW%0U' (datatype=datetime ) ;
run;

use format:

put(a_date, myCustomFmt.) as FW_filter

@yelkenli wrote:

i am having trouble with a simple format need.  

My first attempt (which was with CATS and no PUT statement) resulted in single digit weeks for weeks 1 through 9.  

The below results in a single digit week number with a blank space where a zero should be.  The rest of data manipulations work but I would prefer to have the zero.  

The remaining data manipulations provide a table showing a volume over a time line by week (across years, etc.).  a second path might be to skip creating this variable and present the data some other way but I have not see that alternate path.    so help with keeping the zero is appreciated. 

 

 

create table want AS
SELECT 
      some variables
      ,CAT('Yr', year(datepart(a_date)) , 'FW', put(week(datepart(a_date),'v'), 2.) ) AS FW_filter

from atable; 

 

 

results are as such:

Yr2017FW 1

Yr2017FW 2

etc.

 

would like 

Yr2017FW01

Yr2017FW02


 

ballardw
Super User

A custom format may be the easiest way, IMHO:

proc format library=work;
picture mydtfmt (default=9)
low-high = 'Y%YFW%0V' (datatype=datetime)
;
run;

data work.example;
   dt="15JAN2019:12:24:00"dt;
   y = put (dt,mydtfmt.);
run;

You can use either the %U %V or %W week definition depending which you want. The 0 in %0V places a 0 when there is only a single digit to the week. By declaring the format for use with datetimes you need not extract the date part. The CASE of the % directives is case sensitive. Use of %y would get a 2 digit year. The first Y and FW are literal strings inserted.

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!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 1085 views
  • 0 likes
  • 3 in conversation