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

Hi,

 

I want to add the flag column in the following table:

 

idVisitFlag
111
121
150
14x
13x
211
221
231
250
24x
311
350
32x
33x
34x

 

I have the two columns for patient id and the number of clinic visits. The BMI was measured on each visit (1-5), and visit 5 is the final diagnosis of disease was made. There were few visits that were made after the final diagnosis, which is after visit 5. I wanted to flag 1 for prior diagnosis visit, flag 0 for diagnosis visit (5), and X for those visits after diagnosis (5). Would any please help me add a flag column to the above table?

Thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
mklangley
Lapis Lazuli | Level 10

Here's one way:

data have;
    input id visit;
    datalines;
1 1
1 2
1 5
1 4
1 3
2 1
2 2
2 3
2 5
2 4
3 1
3 5
3 2
3 3
3 4
    ;
run;

data want;
    set have;
    by id;
    retain after;
    if first.id
        then after = 0;
    if visit = 5 
        then do;
            flag = '0';
            after = 1;
        end;
    else if after = 1
        then flag = 'X';
    else flag = '1';
    drop after;
run;

View solution in original post

2 REPLIES 2
mklangley
Lapis Lazuli | Level 10

Here's one way:

data have;
    input id visit;
    datalines;
1 1
1 2
1 5
1 4
1 3
2 1
2 2
2 3
2 5
2 4
3 1
3 5
3 2
3 3
3 4
    ;
run;

data want;
    set have;
    by id;
    retain after;
    if first.id
        then after = 0;
    if visit = 5 
        then do;
            flag = '0';
            after = 1;
        end;
    else if after = 1
        then flag = 'X';
    else flag = '1';
    drop after;
run;
sandyzman1
Obsidian | Level 7

Hi mklangley,

 

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