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

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! 

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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.

View solution in original post

2 REPLIES 2
ballardw
Super User

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.

Dinurik
Fluorite | Level 6

Thank you so much! It worked. 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 957 views
  • 0 likes
  • 2 in conversation