BookmarkSubscribeRSS Feed
Lips
Calcite | Level 5

Hi,

 

I have a dataset of patients, where each patient can have one or many admissions and one or many surgeries in each admission:

I want to choose the first surgery, and if had many admissions to choose the first surgery in last admission.

 

How can I do it?

 

I use version 9.4

 

Thanks!

Michelle

 

 

 

2 REPLIES 2
Astounding
PROC Star

Well, you haven't told us some key information ... what are the names of the variables, are they SAS dates or something else, are there other observations in the data set that do not represent surgeries ... so here's a guess that is based on inventing some of the key information:

 

proc sort data=have;

by patient descending admission surgery_date;

run;

data want;

set have;

by patient;

if first.patient;

run;

mkeintz
PROC Star

Let's assume data set HAVE is sorted by PATIENT and ADMISSION_DATE.   Each PATIENT/ADMISSON_DATE combination can have multiple records, some of which have variable TYPE="surgery".   Then a double-do-until-last.by_var technique can be used:

 

data want;

  do recnum=1 by 1 until (last.patient);
    set have;
    by patient admission_date;
    where type='surgery';
    if first.admission_date then wantrec=recnum;
  end;

  do recnum=1 by 1 until (last.patient);
    set have;
    by patient;
    where type='surgery';
    if recnum=wantrec then output;
  end;

run;

 

Both DO UNTIL loops read and count only "surgery" records.  The first DO UNTIL keeps updating WANTNUM so that it is set to the earliest surgery-record number ("if first.admission_date") within an admission date.  Of course the last time WANTNUM is updated is for the most recent admission_date.   The second DO UNTIL re-reads the same records and outputs only the one matching WANTNUM.

 

Note the "if first.admission_date" says to test if the record-in-hand is the start of a new admission_date.  But we know it is actually the first "surgery" record within the date (not neccessarily the first overall record within date), because of the filtering of the where type='surgery' statement

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 816 views
  • 0 likes
  • 3 in conversation