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: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 938 views
  • 0 likes
  • 2 in conversation