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-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!

5 Steps to Your First Analytics Project Using SAS

For SAS newbies, this video is a great way to get started. James Harroun walks through the process using SAS Studio for SAS OnDemand for Academics, but the same steps apply to any analytics project.

Find more tutorials on the SAS Users YouTube channel.

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