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.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and save with the early bird rate—just $795!
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.