DATA Step, Macro, Functions and more

Counter Flags

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 5
Accepted Solution

Counter Flags

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


Accepted Solutions
Solution
‎09-19-2017 11:44 AM
Occasional Contributor
Posts: 5

Re: Counter Flags

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


All Replies
Super User
Posts: 13,084

Re: Counter Flags

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?

Solution
‎09-19-2017 11:44 AM
Occasional Contributor
Posts: 5

Re: Counter Flags

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*;
Respected Advisor
Posts: 3,162

Re: Counter Flags

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

Occasional Contributor
Posts: 5

Re: Counter Flags

Hi Haikuo,

 

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

Valued Guide
Posts: 561

Re: Counter Flags

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.

Occasional Contributor
Posts: 5

Re: Counter Flags

Posted in reply to ChrisBrooks
Thank you anyway Chris. I do have the date for Mondays. I got a solution though.
Occasional Contributor
Posts: 5

Re: Counter Flags

Posted in reply to ChrisBrooks
I have derived the date from the mondays in my real data, so i do have a date.
☑ This topic is solved.

Need further help from the community? Please ask a new question.

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