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

Hello SAS communities! 

 

I have following data set. 

id start  end
1 1/15/2021 1/21/2021 1/02/2021
2 1/22/2021 1/28/2021 1/09/2021

And I would like to output following data set.

id start  end
1 1/01/2021 1/07/2021 1/02/2021
1 1/08/2021 1/14/2021 1/02/2021
1 1/15/2021 1/21/2021 1/02/2021
2 1/08/2021 1/14/2021 1/09/2021
2 1/15/2021 1/21/2021 1/09/2021
2 1/22/2021 1/28/2021 1/09/2021

 

The rule is.... if X is not in between start and end, create rows until X is in between start and end. For example, from the above table, rows in bold are added.

 

Any type of advice is greatly appreciated! Thank you! 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Looks like you want to move the dates by one week at a time. So calculate the number of weeks and run a DO loop.

data have;
  input id (start  end x) (:mmddyy.);
  format start end x yymmdd10.;
cards;
1 1/15/2021 1/21/2021 1/02/2021
2 1/22/2021 1/28/2021 1/09/2021
;

data want ;
  set have ;
  diff = intck('week',start,x);
  do offset=min(0,diff) to max(0,diff) ;
    new_start = intnx('week',start,offset,'s');
    new_end   = intnx('week',end,offset,'s');
    output;
  end;
  format new_start new_end yymmdd10.;
  rename start=original_start end=original_end
         new_start=start new_end=end
  ;
run;

proc print;
run;
              original_     original_
Obs    id         start           end             x    diff    offset         start           end

 1      1    2021-01-15    2021-01-21    2021-01-02     -2       -2      2021-01-01    2021-01-07
 2      1    2021-01-15    2021-01-21    2021-01-02     -2       -1      2021-01-08    2021-01-14
 3      1    2021-01-15    2021-01-21    2021-01-02     -2        0      2021-01-15    2021-01-21
 4      2    2021-01-22    2021-01-28    2021-01-09     -2       -2      2021-01-08    2021-01-14
 5      2    2021-01-22    2021-01-28    2021-01-09     -2       -1      2021-01-15    2021-01-21
 6      2    2021-01-22    2021-01-28    2021-01-09     -2        0      2021-01-22    2021-01-28

View solution in original post

3 REPLIES 3
Shmuel
Garnet | Level 18

You supplied the rule when to add observation. But what are the rules to define START and END for each added line, while X seems to be retained and copied to those added lnes.

Tom
Super User Tom
Super User

Looks like you want to move the dates by one week at a time. So calculate the number of weeks and run a DO loop.

data have;
  input id (start  end x) (:mmddyy.);
  format start end x yymmdd10.;
cards;
1 1/15/2021 1/21/2021 1/02/2021
2 1/22/2021 1/28/2021 1/09/2021
;

data want ;
  set have ;
  diff = intck('week',start,x);
  do offset=min(0,diff) to max(0,diff) ;
    new_start = intnx('week',start,offset,'s');
    new_end   = intnx('week',end,offset,'s');
    output;
  end;
  format new_start new_end yymmdd10.;
  rename start=original_start end=original_end
         new_start=start new_end=end
  ;
run;

proc print;
run;
              original_     original_
Obs    id         start           end             x    diff    offset         start           end

 1      1    2021-01-15    2021-01-21    2021-01-02     -2       -2      2021-01-01    2021-01-07
 2      1    2021-01-15    2021-01-21    2021-01-02     -2       -1      2021-01-08    2021-01-14
 3      1    2021-01-15    2021-01-21    2021-01-02     -2        0      2021-01-15    2021-01-21
 4      2    2021-01-22    2021-01-28    2021-01-09     -2       -2      2021-01-08    2021-01-14
 5      2    2021-01-22    2021-01-28    2021-01-09     -2       -1      2021-01-15    2021-01-21
 6      2    2021-01-22    2021-01-28    2021-01-09     -2        0      2021-01-22    2021-01-28
_MooMoo
Obsidian | Level 7

Superb! Thanks.

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 937 views
  • 0 likes
  • 3 in conversation