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.
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;
Renaming the subject to better specify the question/subject.
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.
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!
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.