Splitting a row into two based on date

Accepted Solution Solved
Reply
Contributor Kc2
Contributor
Posts: 31
Accepted Solution

Splitting a row into two based on date

Hello ,

I have a dataset of patient and the dr. office dates.

Some visits are mandatory while other are just when the patient has an issue.

The visits are grouped into periods.

Mandatory visits are 6 and visit 9

Have dataset

User               stdate                            enddate                   visit6dt                          visit9dt                              

A                2013-10-25                       2013-11-16                 2013-10-25                    2014-01-25

B                2013-02-01                      2013-04-21                 2013-03-10                    2013-06-10

C               2013-09-01                       2013-09                     2013-09-10                    2013-12-01

Want datset

User               stdate                            enddate                   visit6dt                          visit9dt                              

A                2013-10-25                       2013-10-25                 2013-10-25                    2014-01-25                            

A                2013-10-26                       2014-01-25                 2013-10-25                    2014-01-25

B                2013-02-01                       2013-02-01                2013-03-10                    2013-06-10

B                2013-02-02                       2013-06-10                2013-03-10                    2013-06-10

C               2013-09-01                       2013-09-09                 2013-09-10                    2013-12-01

C               2013-09-10                      2013-09-10                   2013-09-10                    2013-12-01

C               2013-09-11                      2013-09 -30                  2013-09-10                    2013-12-01

C               2013-10-01                      2013-12-01                 2013-09-10                    2013-12-01

becasue the en day in one case is missing i had to split it into 4 rows;

first until the 6 visit Then until the en of the month and then again until the end date.

Thank you for your help


Accepted Solutions
Solution
‎06-24-2015 09:52 AM
Valued Guide
Posts: 860

Re: Splitting a row into two based on date

Here is a solution for user C.

data have;

infile cards;

informat user $1. stdate enddate visit6dt visit9dt yymmdd10.;

format user $1. stdate enddate visit6dt visit9dt mmddyy10.;

input User$ stdate enddate visit6dt visit9dt;

cards;

A               2013-10-25                      2013-11-16                 2013-10-25                    2014-01-25

B               2013-02-01                      2013-04-21                 2013-03-10                    2013-06-10

C               2013-09-01                      2013-09                    2013-09-10                    2013-12-01

;

data prep;

set have;

output;

if missing(enddate) then do;

    enddate = visit6dt-1;output;

    enddate = visit6dt;output;

    enddate = intnx('month',stdate,0,'e');output;

    enddate = visit9dt;output;

end;

run;

data want;

set prep;

if missing(enddate) then delete;

run;

View solution in original post


All Replies
Super User
Super User
Posts: 7,976

Re: Splitting a row into two based on date

Hi,

It would seem to be straightforward if then statements, something like:

     period=0; /* I.e. before v6 */

     new_start=stdate;

     new_end=visit6dt;

     output;

     period=6;

     new_start=visit6dt;

     new_end=visit9dt;

     output;

How you handle missing data items is up to you, maybe check length of date and assign a value or cover with some other error checking logic.

Super User
Posts: 11,343

Re: Splitting a row into two based on date

I think you need to provide some more rules. Such as how the STDATE for the second or subsequent line is determined.

Why does User C end up with 4 lines?

And are any of these variables SAS date valued? Date manipulations are simpler with those.

Solution
‎06-24-2015 09:52 AM
Valued Guide
Posts: 860

Re: Splitting a row into two based on date

Here is a solution for user C.

data have;

infile cards;

informat user $1. stdate enddate visit6dt visit9dt yymmdd10.;

format user $1. stdate enddate visit6dt visit9dt mmddyy10.;

input User$ stdate enddate visit6dt visit9dt;

cards;

A               2013-10-25                      2013-11-16                 2013-10-25                    2014-01-25

B               2013-02-01                      2013-04-21                 2013-03-10                    2013-06-10

C               2013-09-01                      2013-09                    2013-09-10                    2013-12-01

;

data prep;

set have;

output;

if missing(enddate) then do;

    enddate = visit6dt-1;output;

    enddate = visit6dt;output;

    enddate = intnx('month',stdate,0,'e');output;

    enddate = visit9dt;output;

end;

run;

data want;

set prep;

if missing(enddate) then delete;

run;

Contributor Kc2
Contributor
Posts: 31

Re: Splitting a row into two based on date

Thank you everyone  for the quick response. Mark Johnson's solution is the closest one . I still need to make a few adjustemnt but I am getting there. (user C si the problem).

Valued Guide
Posts: 860

Re: Splitting a row into two based on date

I didn't see what you were doing with the duplicate A and B rows, here's another more complete solution:

data prep;

set have;

output;

if missing(enddate) then do;

    enddate = visit6dt-1;output;

    enddate = visit6dt;output;

    enddate = intnx('month',stdate,0,'e');output;

    enddate = visit9dt;output;

end;

if not missing(enddate) then do;

    retain User enddate visit6dt visit9dt;

    stdate = stdate+1;output;

end;

run;

data want;

set prep;

if missing(enddate) then delete;

run;

Contributor Kc2
Contributor
Posts: 31

Re: Splitting a row into two based on date

Posted in reply to Steelers_In_DC

Thank you Mark.

🔒 This topic is solved and locked.

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

Discussion stats
  • 6 replies
  • 299 views
  • 0 likes
  • 4 in conversation