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

Hi everyone,

 

I have a data with Diagnoses at each year of Medical exam. I am trying to select ALL Diagnosis data (all lines) from the MOST RECENT EXAM ONLY, for each ID. 

 

Please, can you help me with the coding, see below the database structure.

 

Thanks in advance,

 

 IDCalendar YearDiagnosis
12016 TB
12016No health condition
12016No health condition
22014TB
22016Inactive/Latent Tuberculosis
32014TB
32016Inactive/Latent Tuberculosis
32016HIV
32016Hypertension
42015Cancer
42014No health condition
42016TB

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

What most forum members would appreciate is if you could post sample data in the form of a working SAS data step so we don't have to do the work for you.

It's not clear to me what constitutes the most recent exam. Below code sample assumes you're just after all the rows per ID with the latest year.

data have;
  infile datalines dlm=' ' truncover;
  input ID $ Calendar_Year Diagnosis $60.;
  datalines;
1 2016  TB 
1 2016 No health condition 
1 2016 No health condition 
2 2014 TB 
2 2016 Inactive/Latent Tuberculosis 
3 2014 TB 
3 2016 Inactive/Latent Tuberculosis 
3 2016 HIV 
3 2016 Hypertension 
4 2015 Cancer 
4 2014 No health condition 
4 2016 TB 
;
run;

proc sql;
  create table want as
    select *
    from have
    group by id
    having Calendar_Year=max(Calendar_Year)
  ;
quit;

 

View solution in original post

8 REPLIES 8
kiranv_
Rhodochrosite | Level 12

how the output  should look like.

seltonsy
Quartz | Level 8
It doesn't matter much. If you have more than one way in your mind feel free to share. Well appreciated..
Thanks.
Patrick
Opal | Level 21

What most forum members would appreciate is if you could post sample data in the form of a working SAS data step so we don't have to do the work for you.

It's not clear to me what constitutes the most recent exam. Below code sample assumes you're just after all the rows per ID with the latest year.

data have;
  infile datalines dlm=' ' truncover;
  input ID $ Calendar_Year Diagnosis $60.;
  datalines;
1 2016  TB 
1 2016 No health condition 
1 2016 No health condition 
2 2014 TB 
2 2016 Inactive/Latent Tuberculosis 
3 2014 TB 
3 2016 Inactive/Latent Tuberculosis 
3 2016 HIV 
3 2016 Hypertension 
4 2015 Cancer 
4 2014 No health condition 
4 2016 TB 
;
run;

proc sql;
  create table want as
    select *
    from have
    group by id
    having Calendar_Year=max(Calendar_Year)
  ;
quit;

 

Satish_Parida
Lapis Lazuli | Level 10
/*The Code will look like this*/
proc sort data=have;
by ID Calendar_Year;
run;

data want;
set have;
by ID Calendar_Year;
if last.ID then output;
run;

/*But if you have a Diagnoses_Date then It would be Perfect*/
proc sort data=have;
by ID Diagnoses_Date;
run;

data want;
set have;
by ID Diagnoses_Date;
if last.ID then output;
run;
seltonsy
Quartz | Level 8
Thanks for the effort,

I wanted to have not only the last line but all lines that have the latest exam date

for example, you go to the doctor and he writes each diagnosis on a separate line with the same date for all exam
thanks
Satish_Parida
Lapis Lazuli | Level 10

In That Case:

 

Using Solution provide by @Kurt_Bremser

 

proc sql;
create table want as
select a.* from have a ,
(select id,diag_date
from have
group by id
having diag_date = max(diag_date))
where a.id=b.id and a.diag_date=b.diag_date
;
quit;
kiranv_
Rhodochrosite | Level 12

answer is important but asking question is even more important. it makes you successful. if you want to be successful it is very very important to ask question correctly and in effective manner, where in no one have to guess. Most of work in field depends on how to ask proper questions in business or in any other field

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
  • 8 replies
  • 13942 views
  • 0 likes
  • 5 in conversation