Hi SAS programmers,
Could you please help me with the following problem?
My data is:
data have;
input id visit cvd;
datalines;
1 1 0
1 3 0
1 5 1
1 9 0
2 3 .
2 4 1
2 5 0
2 9 .
;
If a subject had cardiovascular even (CVD=1), I need them to retain CVD=1 in all subsequent visits. Data are unbalanced, measurements are taken for participants at different visits, and there are missing values. So I need to preserve missingness and zeros, unless subjects hits CVD=1, then “1” needs to be carried on until the last visit per id.
Any help/tips will be highly appreciated!
Here is one way for the given data.
data have; input id visit cvd; datalines;
1 1 0 1 3 0 1 5 1 1 9 0 2 3 . 2 4 1 2 5 0 2 9 . ; data want; set have; by id; retain cvdtru; /* reset the retained value for the first visit of each id */ if first.id then call missing(cvdtru); /* when find the cvd=1 then set the retained value*/ if cvd=1 then cvdtru=1; /* if the retained value is set, then assign it to the needed variable */ if cvdtru=1 then cvd=1; drop cvdtru; run;
assume the data are sorted by ID and visit number as shown.
Here is one way for the given data.
data have; input id visit cvd; datalines;
1 1 0 1 3 0 1 5 1 1 9 0 2 3 . 2 4 1 2 5 0 2 9 . ; data want; set have; by id; retain cvdtru; /* reset the retained value for the first visit of each id */ if first.id then call missing(cvdtru); /* when find the cvd=1 then set the retained value*/ if cvd=1 then cvdtru=1; /* if the retained value is set, then assign it to the needed variable */ if cvdtru=1 then cvd=1; drop cvdtru; run;
assume the data are sorted by ID and visit number as shown.
Thank you so much! It worked.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.