DATA Step, Macro, Functions and more

Data step: setting flag on last day

Reply
Super Contributor
Posts: 270

Data step: setting flag on last day

[ Edited ]

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.

Respected Advisor
Posts: 4,641

Re: data step

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
Super User
Posts: 5,255

Re: data step

Or do last.visit and make flag a retained variable.
Data never sleeps
Super User
Posts: 5,255

Re: Data step: setting flag on last day

Renaming the subject to better specify the question/subject.

Data never sleeps
Super User
Super User
Posts: 7,392

Re: Data step: setting flag on last day

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.

Ask a Question
Discussion stats
  • 4 replies
  • 264 views
  • 3 likes
  • 4 in conversation