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

Hello,

 

I have this data set of weight and height measurements. Sometimes either the height  or the weight is missing.

when on one the measurement is missing I want to pick the  non missing measurement  the closest date and that is in a 3 months window before or after the measurement date.

Thank you

 

data have;

infile datalines missover;

input id weight height vsdt;

format vsdt yymmdd10.;

datalines;

001 65  160   09MAR2017

001 65  .     06JUN2017

001 65  160   18JUL2017

002  55     158       13MAY2014

002  58       .          14SEP2014

002 58       158      15SEP2015

003 70      180       06AUG2017

003 70.5                01MAR2018

003 71                   01FEB2019

run;

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

Try this one :

 

data have;
infile datalines missover;
input id weight height vsdt:date10.;
format vsdt yymmdd10.;
datalines;
001 65 160 09MAR2017
001 65 . 06JUN2017
001 65 160 18JUL2017
002 55 158 13MAY2014
002 58 . 14SEP2014
002 58 158 15SEP2015
003 70 180 06AUG2017
003 70.5 . 01MAR2018
003 71 . 01FEB2019
;
run;

proc sql;
select *,case when missing(height) then 
(select min(height) from have where id=a.id and 
vsdt between intnx('month',a.vsdt,-3,'s') and intnx('month',a.vsdt,3,'s') and
height is not missing
having abs(a.vsdt-vsdt) =  min(abs(a.vsdt-vsdt))
 )
 else height end as new_height
 from have as a;
quit;

View solution in original post

3 REPLIES 3
Jagadishkatam
Amethyst | Level 16

why don't we retain

 

data have;
infile datalines missover;
input id weight height vsdt:date10.;
format vsdt yymmdd10.;
datalines;
001 65 160 09MAR2017
001 65 . 06JUN2017
001 65 160 18JUL2017
002 55 158 13MAY2014
002 58 . 14SEP2014
002 58 158 15SEP2015
003 70 180 06AUG2017
003 70.5 . 01MAR2018
003 71 . 01FEB2019
;
run;

proc sort data=have;
by id vsdt;
run;

data want;
set have;
by id vsdt;
retain height_;
if first.id then height_=.;
if height ne . then height_=height;
run;
Thanks,
Jag
Kc2
Quartz | Level 8 Kc2
Quartz | Level 8

The retain would not work because we want the closest measurement within a 3 week window so for this patient;

001 65 160 09MAR2017
001 65 . 06JUN2017
001 65 160 18JUL2017

for the record where height is missing (06JUN2017) we would want to pick the measurement on 18JUL2017 and not the one from 09MAR2017 because the 18JUL2017 is closer to 06JUNE2017 than 09MAR2017

Ksharp
Super User

Try this one :

 

data have;
infile datalines missover;
input id weight height vsdt:date10.;
format vsdt yymmdd10.;
datalines;
001 65 160 09MAR2017
001 65 . 06JUN2017
001 65 160 18JUL2017
002 55 158 13MAY2014
002 58 . 14SEP2014
002 58 158 15SEP2015
003 70 180 06AUG2017
003 70.5 . 01MAR2018
003 71 . 01FEB2019
;
run;

proc sql;
select *,case when missing(height) then 
(select min(height) from have where id=a.id and 
vsdt between intnx('month',a.vsdt,-3,'s') and intnx('month',a.vsdt,3,'s') and
height is not missing
having abs(a.vsdt-vsdt) =  min(abs(a.vsdt-vsdt))
 )
 else height end as new_height
 from have as a;
quit;

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
  • 3 replies
  • 358 views
  • 0 likes
  • 3 in conversation