Hello,
I want to have week number 1-4 in a dataset having days.
I appreciate help since I am getting pretty tired of it.
Thank you anyone!
data have;
input id visit daynumber dayname $3. x;
cards;
1 1 1 Mon 20
1 1 2 Tue 30
1 1 3 Wed 0
1 1 4 Thu 2
1 1 5 Fri 4
1 1 6 Sat 60
1 1 7 Sun 20
1 1 1 Mon 2
1 1 2 Tue 3
1 1 3 Wed 20
1 1 4 Thu 20
1 1 5 Fri 40
1 1 6 Sat 6
1 1 7 Sun 2
1 1 1 Mon 50
1 1 2 Tue 6
1 1 3 Wed 9
1 1 4 Thu 9
1 1 5 Fri 8
1 1 6 Sat 8
1 1 7 Sun 2
1 1 1 Mon 7
1 1 2 Tue 3
1 1 3 Wed 90
1 1 4 Thu 6
1 1 5 Fri 9
1 1 6 Sat 6
1 1 7 Sun 4
1 2 1 Mon 30
1 2 2 Tue 30
1 2 3 Wed 0
1 2 4 Thu 2
1 2 5 Fri 40
1 2 6 Sat 10
1 2 7 Sun 10
1 2 1 Mon 60
1 2 2 Tue 3
1 2 3 Wed 1
1 2 4 Thu 5
1 2 5 Fri 50
1 2 6 Sat 1
1 2 7 Sun 1
;
data want;
input id visit daynumber dayname $3. x weeknumber;
cards;
1 1 1 Mon 20 1
1 1 2 Tue 30 1
1 1 3 Wed 0 1
1 1 4 Thu 2 1
1 1 5 Fri 4 1
1 1 6 Sat 60 1
1 1 7 Sun 20 1
1 1 1 Mon 2 2
1 1 2 Tue 3 2
1 1 3 Wed 20 2
1 1 4 Thu 20 2
1 1 5 Fri 40 2
1 1 6 Sat 6 2
1 1 7 Sun 2 2
1 1 1 Mon 50 3
1 1 2 Tue 6 3
1 1 3 Wed 9 3
1 1 4 Thu 9 3
1 1 5 Fri 8 3
1 1 6 Sat 8 3
1 1 7 Sun 2 3
1 1 1 Mon 7 4
1 1 2 Tue 3 4
1 1 3 Wed 90 4
1 1 4 Thu 6 4
1 1 5 Fri 9 4
1 1 6 Sat 6 4
1 1 7 Sun 4 4
1 2 1 Mon 30 1
1 2 2 Tue 30 1
1 2 3 Wed 0 1
1 2 4 Thu 2 1
1 2 5 Fri 40 1
1 2 6 Sat 10 1
1 2 7 Sun 10 1
1 2 1 Mon 60 2
1 2 2 Tue 3 2
1 2 3 Wed 14 2
1 2 4 Thu 5 2
1 2 5 Fri 50 2
1 2 6 Sat 11 2
1 2 7 Sun 12 2
;
run
I suspect you would be better off finding an actual date.
Question: In the following data without knowing the order of the data how do I know (a rule simple enough to tell computers) know which week these to values get?
1 1 1 Mon 20 1
1 1 1 Mon 2 2
The reason I ask is if you have an Id visit combination that does not start on Monday or has fewer than 7 actual "days" for a given week a proposed solution may well fail. Can you guarantee that these conditions never occur in your data?
And what if the Id visit combination exceeds 28 days? What do you want for week? You specifically limit week to 1 - 4?
So let me put it straight, you are tired of it, therefore you need someone else to do your job?
Hi Haikuo,
I was too tired to think any longer, you are right 🙂
The problem you've got is that there's nothing in your data which tells us which week an observation belongs to other than it's physical order. It can be done with a counter ( counting upto 7 and then resetting the week number)!but if it's a real example and not an exercise then it's a rather fragile method and not one I'd want to put my name to.
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.