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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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