BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
daufoi
Fluorite | Level 6

I have the following data. I sorted it by ID and date. How can I get the first date for each patient but if there is a missing value in the location column, I want the next non-missing value?

data fake_data;
input patID $ date monyy6. location $ outcome ;
format date monyy.;
datalines;
1693 Dec-14 . 1
1693 Nov-14 . 1
1693 Oct-14 CA 1
1693 Sep-14 CA 0
1693 Aug-14 CO 0
1693 Jul-14 CO 0
1129 Feb-18 . 1
1129 Jan-18 . 0
1129 Dec-17 NY 0
1129 Nov-17 PA 0
1345 Jun-19 . 0
1345 May-19 . 0
1345 Apr-19 NY 0
1345 Mar-19 NY 0
1345 Feb-19 NY 0
1345 Jan-19 CA 0
1345 Dec-18 CA 0
1345 Nov-18 CA 0
;

 

I have this so far...

proc sql;
create table test as 
select A.*, 
coalesce(A.location, B.location) AS location_2
from fake_data A, fake_data B
where A.patID = B.patID
group by A.patID
order by A.patID, A.date desc;
quit;

I would like to be able to sort it so that I can choose the first entry which will have all columns with data.

 

1 ACCEPTED SOLUTION

Accepted Solutions
daufoi
Fluorite | Level 6

I wanted not missing location but all the date and outcome data have to come from the most recent date. I am interested in the data with most recent date and backfilling location. But I found the solution if anyone comes across this...

 

data fake_data;
input patID $ date monyy6. location $ outcome ;
format date monyy.;
datalines;
1693 Dec-14 . 1
1693 Nov-14 . 1
1693 Oct-14 CA 0
1693 Sep-14 CA 0
1693 Aug-14 CO 0
1693 Jul-14 CO 0
1129 Feb-18 . 1 
1129 Jan-18 . 0
1129 Dec-17 NY 0
1129 Nov-17 PA 0
1345 Jun-19 . 0 
1345 May-19 . 0 
1345 Apr-19 NY 0 
1345 Mar-19 NY 0
1345 Feb-19 NY 0
1345 Jan-19 CA 0 
1345 Dec-18 CA 0
1345 Nov-18 CA 0
;

proc sort data=fake_data; by patID descending date; run;
data fake_row;
  set fake_data;
  rownum=_n_;
run;

proc sql;
create table test as 
select A.*, B.rownum as rownum2,
coalesce(A.location, B.location) AS location_2
from fake_row A, fake_row B
where A.patID = B.patID
group by A.patID
order by A.patID, A.date desc;
quit;

data test2;
	set test;
	where not missing(location_2);
	row = rownum + rownum2;
run;
proc sort data=test2; by patID row; run;

data final;
	set test2;
	by patID row;
	if first.patID;
run;

View solution in original post

2 REPLIES 2
andreas_lds
Jade | Level 19

It is not clear what you expect as result.

If you just want the first obs with non-missing location for each id, then try:

data want;
  set fake_data;
  by patID notsorted; /* EDIT: Your data is grouped  by id, but not sorted */
  where not missing(location);
  if first.patID;
run;
daufoi
Fluorite | Level 6

I wanted not missing location but all the date and outcome data have to come from the most recent date. I am interested in the data with most recent date and backfilling location. But I found the solution if anyone comes across this...

 

data fake_data;
input patID $ date monyy6. location $ outcome ;
format date monyy.;
datalines;
1693 Dec-14 . 1
1693 Nov-14 . 1
1693 Oct-14 CA 0
1693 Sep-14 CA 0
1693 Aug-14 CO 0
1693 Jul-14 CO 0
1129 Feb-18 . 1 
1129 Jan-18 . 0
1129 Dec-17 NY 0
1129 Nov-17 PA 0
1345 Jun-19 . 0 
1345 May-19 . 0 
1345 Apr-19 NY 0 
1345 Mar-19 NY 0
1345 Feb-19 NY 0
1345 Jan-19 CA 0 
1345 Dec-18 CA 0
1345 Nov-18 CA 0
;

proc sort data=fake_data; by patID descending date; run;
data fake_row;
  set fake_data;
  rownum=_n_;
run;

proc sql;
create table test as 
select A.*, B.rownum as rownum2,
coalesce(A.location, B.location) AS location_2
from fake_row A, fake_row B
where A.patID = B.patID
group by A.patID
order by A.patID, A.date desc;
quit;

data test2;
	set test;
	where not missing(location_2);
	row = rownum + rownum2;
run;
proc sort data=test2; by patID row; run;

data final;
	set test2;
	by patID row;
	if first.patID;
run;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 2006 views
  • 1 like
  • 2 in conversation