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

Hi 

 

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

 

Datevalue 
201801a
201802b
201803c

 

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:

 

Datevalue Date2
201801a2018-01-06
201802b2018-01-13
201803c2018-01-20
1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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;

	/*1 Add W*/
	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

 

View solution in original post

3 REPLIES 3
Reeza
Super User

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;

	/*1 Add W*/
	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

 

kz134
Obsidian | Level 7

worked well. thank you 

Astounding
PROC Star

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

 

 

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

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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
  • 3 replies
  • 4706 views
  • 1 like
  • 3 in conversation