Obsidian | Level 7

## Get last day the week

Hi

I have a dataset that is split by weeks. example:

 Date value 201801 a 201802 b 201803 c

The date indicates the week number of the year. The date is in character format. How do convert the date to get the last day of that week instead?

Want:

 Date value Date2 201801 a 2018-01-06 201802 b 2018-01-13 201803 c 2018-01-20
1 ACCEPTED SOLUTION

Accepted Solutions
Super User

## Re: Get last day the week

1. Add W to get in a week format that SAS understands, ie 2018W01, 2018W02, 2018W03

2. Convert to a SAS date using INPUT and the WEEKV informat

3. Increment to the end of the week.

For the WEEKV rules:

https://documentation.sas.com/?cdcId=pgmsascdc&cdcVersion=9.4_3.4&docsetId=leforinforref&docsetTarge...

``````*create sample data to test - please provide this in the future;
data have;
input date \$ value \$;
cards;
201801 a
201802 b
201803 c
;
run;

data want;
set have;

dateW = catt(substr(date, 1, 4), 'W', substr(date, 5, 2));

/*2 Convert to SAS Date*/
z = input(dateW, weekv.);

/*3. Find end of week*/
x = intnx('week', z, 0, 'e');

/*4 doing it in one step*/
x2 = intnx('week', input(catt(substr(date, 1, 4), 'W', substr(date, 5, 2)), weekv.), 0, 'e');

*Format appearance;
format x x2 date9.;
run;``````

@kz134 wrote:

Hi

I have a dataset that is split by weeks. example:

 Date value 201801 a 201802 b 201803 c

The date indicates the week number of the year. The date is in character format. How do convert the date to get the last day of that week instead?

Want:

 Date value Date2 201801 a 2018-01-06 201802 b 2018-01-13 201803 c 2018-01-20

3 REPLIES 3
Super User

## Re: Get last day the week

1. Add W to get in a week format that SAS understands, ie 2018W01, 2018W02, 2018W03

2. Convert to a SAS date using INPUT and the WEEKV informat

3. Increment to the end of the week.

For the WEEKV rules:

https://documentation.sas.com/?cdcId=pgmsascdc&cdcVersion=9.4_3.4&docsetId=leforinforref&docsetTarge...

``````*create sample data to test - please provide this in the future;
data have;
input date \$ value \$;
cards;
201801 a
201802 b
201803 c
;
run;

data want;
set have;

dateW = catt(substr(date, 1, 4), 'W', substr(date, 5, 2));

/*2 Convert to SAS Date*/
z = input(dateW, weekv.);

/*3. Find end of week*/
x = intnx('week', z, 0, 'e');

/*4 doing it in one step*/
x2 = intnx('week', input(catt(substr(date, 1, 4), 'W', substr(date, 5, 2)), weekv.), 0, 'e');

*Format appearance;
format x x2 date9.;
run;``````

@kz134 wrote:

Hi

I have a dataset that is split by weeks. example:

 Date value 201801 a 201802 b 201803 c

The date indicates the week number of the year. The date is in character format. How do convert the date to get the last day of that week instead?

Want:

 Date value Date2 201801 a 2018-01-06 201802 b 2018-01-13 201803 c 2018-01-20

Obsidian | Level 7

## Re: Get last day the week

worked well. thank you

PROC Star

## Re: Get last day the week

How do you define "last day of the week" ?

Why should the first week end on January 6, not January 7?

Discussion stats
• 3 replies
• 4240 views
• 1 like
• 3 in conversation