Changing a number sequence centered around weekdays

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 6
Accepted Solution

Changing a number sequence centered around weekdays

Hi,

I am trying to change my number sequence in my week variable, by replacing values and keep the ordering (it is ordered sequentially by date). Currently all my information starts on a Day -1 which is coded as the beginning of week 0 and the day before our event date. From there, there is always a week -1 which is the five days before day -1. After the five days of week 0 (days -1 to day 3) we have week 1 (days 4-8) and so on up until the 5 days before week zero (always the week -1). Sometimes we have a full week 5, sometimes a full week 6, etc. I am trying to change the data where instead of going upwards, I need the sequence to start at week 0, and then go backwards (-1, -2, -3, -4, -5, until you get the 5 days of week 0). In otherwords, I will be changing everything but the week 0 dates. I apologize if this is a bit ambiguous. Your help is much appreciated.


Accepted Solutions
Solution
‎04-22-2015 04:48 PM
PROC Star
Posts: 7,433

Re: Changing a number sequence centered around weekdays

The code I wrote was based on setting new after discovering a date that had a week of 0.

If it isn't doing what you actually want, then I'd suggest that you figure out what you want and attempt to modify the code accordingly.

View solution in original post


All Replies
PROC Star
Posts: 7,433

Re: Changing a number sequence centered around weekdays

Two example data sets, one an example of you have, and another of what you want based on the first dataset, will likely be needed for anyone to be able to help.

Occasional Contributor
Posts: 6

Re: Changing a number sequence centered around weekdays

Hi, it won't allow me to import the bdat file? Why?

PROC Star
Posts: 7,433

Re: Changing a number sequence centered around weekdays

You have to zip the file and upload the zipped version.

Occasional Contributor
Posts: 6

Re: Changing a number sequence centered around weekdays

I have provided my simple file. I am unable to send the full SAS file due to instruction from the head of my project, however, I think this file should illustrate what I am looking for.

Thank you!

Jonathan

Attachment
Super User
Posts: 11,134

Re: Changing a number sequence centered around weekdays

And for many purposes it is usually better to provide a small data step program to reduce any data sensitivity issues and to provide only variables of interest.

Something like:

data have;

     input value1 value2 datival : mmddyy10.;

datalines;

1 2 01/01/2015

2 4 01/02/2015

5 6 01/04/2015

;

run;

Respected Advisor
Posts: 4,135

Re: Changing a number sequence centered around weekdays

Like Art wrote: You will need to provide sample data (best: a data step creating such a sample) and then provide the desired result.

SAS has a few great calendar functions like intnx() and intck() and also allows for custom intervals; so all we need is to understand what you have and what you want.

PROC Star
Posts: 7,433

Re: Changing a number sequence centered around weekdays

Your example data only showed some of what you want, thus I don't know if I really understand what you want to do.

The following produces the same results as your example:

data want (keep=date mktrf week what: new);

  set sashelp;

  by week notsorted;

  retain start last_date last_new;

  if first.week and week eq 0 then do;

    counter=0;

    start=1;

  end;

  else call missing(new);

  if start eq 1 then do;

    counter+1;

    if counter le 5 then new=0;

    else if last_new in (0,-6) then do;

      if date-last_date eq 1 then new=-6;

      else do;

        new=-5;

        counter=6;

      end;

    end;

    else do;

      new=last_new;

      if mod(counter,5) eq 1 then do;

        new+1;

      end;

    end;

  end;

  last_date=date;

  last_new=new;

run;

Occasional Contributor
Posts: 6

Re: Changing a number sequence centered around weekdays

Thank you.

You're the man!

Occasional Contributor
Posts: 6

Re: Changing a number sequence centered around weekdays

I have a couple weeks where ( check line for Date - 05nov2001) where the sequence goes 0 then -2 not -1

Solution
‎04-22-2015 04:48 PM
PROC Star
Posts: 7,433

Re: Changing a number sequence centered around weekdays

The code I wrote was based on setting new after discovering a date that had a week of 0.

If it isn't doing what you actually want, then I'd suggest that you figure out what you want and attempt to modify the code accordingly.

Occasional Contributor
Posts: 6

Re: Changing a number sequence centered around weekdays

Awesome. I really appreciate your help on this, it completely saved me today.

🔒 This topic is solved and locked.

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

Discussion stats
  • 11 replies
  • 400 views
  • 2 likes
  • 4 in conversation