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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Manuz
Fluorite | Level 6
Thank you for the answer.

I have a date only for the Mondays. It is from a questionnaire used for
alcohol consumption (TLFB) where they are writing consumption per day,
each day of the week and only date for Mondays.

this simple code is working

*data* week4;

set have;

by id visit;

if first.visit then week=*0*;

if daynumber= *1* then week+*1*;

*run*;

View solution in original post

7 REPLIES 7
ballardw
Super User

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?

Manuz
Fluorite | Level 6
Thank you for the answer.

I have a date only for the Mondays. It is from a questionnaire used for
alcohol consumption (TLFB) where they are writing consumption per day,
each day of the week and only date for Mondays.

this simple code is working

*data* week4;

set have;

by id visit;

if first.visit then week=*0*;

if daynumber= *1* then week+*1*;

*run*;
Haikuo
Onyx | Level 15

So let me put it straight, you are tired of it, therefore you need someone else to do your job?

Manuz
Fluorite | Level 6

Hi Haikuo,

 

I was too tired to think any longer, you are right 🙂

ChrisBrooks
Ammonite | Level 13

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.

Manuz
Fluorite | Level 6
Thank you anyway Chris. I do have the date for Mondays. I got a solution though.
Manuz
Fluorite | Level 6
I have derived the date from the mondays in my real data, so i do have a date.

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!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 1226 views
  • 0 likes
  • 4 in conversation