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. 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:
*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
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:
*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
worked well. thank you
How do you define "last day of the week" ?
Why should the first week end on January 6, not January 7?
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!
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.
Ready to level-up your skills? Choose your own adventure.