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. 🙂 

 

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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