BookmarkSubscribeRSS Feed
knveraraju91
Barite | Level 11

Hi I need help in my code.

 

I have three variables.

 

subjid seq visit 

1          1     day 1

1           2     day 1

1           3      day1

1            4      day2

1            5      day2

1            6      day2

1            7      day3

1            8      day3

1            9      day3

2          1     day 1

2           2     day 1

2           3      day1

2            4      day2

2            5      day2

2            6      day2

2            7      day3

2            8      day3

2            9      day3

2            10      day4

2            11      day4

2            12      day4

 

i need to flag  all last day visits. So I need out put like this.

 

subjid seq visit               flag 

1          1     day 1

1           2     day 1

1           3      day1

1            4      day2

1            5      day2

1            6      day2

1            7      day3           1

1            8      day3            1

1            9      day3             1

2          1     day 1

2           2     day 1

2           3      day1

2            4      day2

2            5      day2

2            6      day2

2            7      day3

2            8      day3

2            9      day3

2            10      day4         1

2            11      day4          1

2            12      day4          1

 

I am using the code;

 

 proc sort data=one out=two;
by subjid seq visit  ;
run;
data three;
set two;
by subjid seq visit;
if last.subjid then flag=1;
else flag='';
run;

 

I am getting the output like this:

 

subjid seq visit               flag 

1          1     day 1

1           2     day 1

1           3      day1

1            4      day2

1            5      day2

1            6      day2

1            7      day3           

1            8      day3            

1            9      day3             1

2          1     day 1

2           2     day 1

2           3      day1

2            4      day2

2            5      day2

2            6      day2

2            7      day3

2            8      day3

2            9      day3

2            10      day4         

2            11      day4          

2            12      day4          1

 

Please help in my code.

 

Thank you.

4 REPLIES 4
PGStats
Opal | Level 21

Use two DO UNTIL blocks. The first one checks if this is the last visit. The second one writes out the whole visit. Watch for the consistency of visit names.

 

data one;
input subjid seq v $&;
visit = compress(v);
drop v;
datalines;
1          1     day 1
1           2     day 1
1           3      day1
1            4      day2
1            5      day2
1            6      day2
1            7      day3
1            8      day3
1            9      day3
2          1     day 1
2           2     day 1
2           3      day1
2            4      day2
2            5      day2
2            6      day2
2            7      day3
2            8      day3
2            9      day3
2            10      day4
2            11      day4
2            12      day4
;

proc sort data=one out=two;
by subjid visit seq;;
run;

data three;
do until (last.visit);
    set two; by subjId visit;
    end;
flag = last.subjId;
do until (last.visit);
    set two; by subjId visit;
    output;
    end;
run;

PG
LinusH
Tourmaline | Level 20
Or do last.visit and make flag a retained variable.
Data never sleeps
LinusH
Tourmaline | Level 20

Renaming the subject to better specify the question/subject.

Data never sleeps
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Hi,

 

What do your specifcations say about last day?  Your test example may work fine, but what if you have:

day 1

day 3

day 2

?  Is day 2 the last day.  What about unscheduled visits, repeats, data issues such as dates not matching visit name etc.?  Whilst if your data is really very clean like that, then it might be ok to flag last day, I would really want some specification which details what that actually means.

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
  • 4 replies
  • 1297 views
  • 3 likes
  • 4 in conversation