Create two datasets from one dataset

Reply
N/A
Posts: 0

Create two datasets from one dataset

I am trying to split a dataset into two datasets. My dataset consists of three variables, patient, note_date, note. For example, the dataset has many patients and each patient has many records. I am trying to determine what is the first visit date for each patient and then seperate the dataset into one dataset containg all patients with their first visits only and a dataset with all patients with all visits except the first one. For clarity the patient can have more than one first visit date. I tried using first. but can't seem to work out the logic. Any help is appreciated.
N/A
Posts: 0

Re: Create two datasets from one dataset

Well I figured this one out.

My code is below.

proc sort data=progressnotes;
by pat_name note_date;
run;

data temp;
set progressnotes;
format n_date mmddyy10.;
n_date=DATEPART(note_date);
run;

data temp;
set temp;
retain first_visit;
format first_visit mmddyy10.;
by pat_name note_date;
if first.pat_name then do;
first_visit=n_date;
end;
run;
SAS Super FREQ
Posts: 8,645

Re: Create two datasets from one dataset

Hi:
In order to streamline your code, this construction:

[pre]
data temp;
set temp;
[/pre]

is not needed. Since progressnotes is sorted and you are creating n_date only for the purpose of creating first_visit for every obs in a patient group, you can do everything you need in one data step program. You might, instead, consider code like this:
[pre]
proc sort data=progressnotes;
by pat_name note_date;
run;

data temp;
retain first_visit;
format first_visit mmddyy10.
n_date mmddyy10.;

set progressnotes;
by pat_name note_date;

n_date=DATEPART(note_date);

if first.pat_name then do;
first_visit=n_date;
end;
run;
[/pre]

If you don't want a separate n_date on every obs, then you could just have
[pre]
first_visit=datepart(note_date);
[/pre]

But you must have wanted a separate n_date on every obs, since you put it there in a separate data step program. At any rate, you can create n_date and first_visit in the same program.

cynthia
N/A
Posts: 0

Re: Create two datasets from one dataset

Thanks Cynthia,

I don't mind shortening my code.
Post a Question
Discussion Stats
  • 3 replies
  • 351 views
  • 0 likes
  • 2 in conversation