DATA Step, Macro, Functions and more

first. and last.

Accepted Solution Solved
Reply
Regular Contributor
Posts: 200
Accepted Solution

first. and last.

I need to get for each usubjid i need the first ECSTDTC(start date) and last ECENDTC (end date) so basically one record per usubjid.

 

proc sort data=ec out=ec7; by usubjid ECSTDTC; run;
data ec1;
set ec7;
by usubjid ECSTDTC ECENDTC;
if First.ECSTDTC and last.ECENDTC then output;
run;

 

can anyonew help me


Accepted Solutions
Solution
‎08-31-2017 02:36 AM
Super User
Super User
Posts: 7,860

Re: first. and last.

That is not how the by variables work. They indicate whether the current record is the first or the last for the group defined by the by variable.  So FIRST.ECSTDTC and LAST.ECENDTC could only be true if there is only one record for that value of ECSTDTC within that value of USUBJID.

 

If your data it properly sorted and has no missing values then you want.

data ec1;
  set ec7;
  by usubjid ;
  retain first_start ;
  if first.usubjid then first_start=ECSTDTC;
  if last.usubjid ;
  last_stop = ECENDTC;
  keep usubjid first_start last_stop ;
run;

If you have missing values then you will need to add more logic and also retain the variable you use to store the last end date.

View solution in original post


All Replies
PROC Star
Posts: 1,190

Re: first. and last.

If you want to use ECENDTC in the BY statement in your data step, then you should also sort by that variable in your PROC SORT.

Regular Contributor
Posts: 200

Re: first. and last.

i used it but still doesnt work

Super User
Posts: 6,542

Re: first. and last.

How about:

 

proc summary data=ec7 nway;

class usubjid;

var ecstdtc ecendtc;

output out=want (keep=usubjid start_date end_date) min(ecstdtc)=start_date max(ecendtc)=end_date;

run;

Regular Contributor
Posts: 200

Re: first. and last.

Posted in reply to Astounding

unfortunately i cannot use proc summary as the dates are character dates.

 

 

Super User
Posts: 6,542

Re: first. and last.

It's easy enough to convert character dates to numeric.  What format are they in? 

 

It would guess that they're in year-month-day form, or else sorting them couldn't help.  But one never knows.  Give a couple of examples.

Regular Contributor
Posts: 200

Re: first. and last.

attaching test data

Attachment
Super User
Posts: 6,542

Re: first. and last.

So these variables have both a date and a time.  Do you care about the time portion, or just the date portion?

Regular Contributor
Posts: 200

Re: first. and last.

Posted in reply to Astounding

can usubjid be used in that code and take first and last observation? as i need time as well to calculate duration later

Super User
Posts: 6,542

Re: first. and last.

It would definitely work (but might be overkill) to process each variable separately:

 

proc sort data=have;

   by usubjid ecstdtc;

run;

data start;

set have;

by usubjid ecstdtc;

if first.usubjid;

keep usubjid ecstdtc;

run;

 

proc sort data=have;

by usubjid ecendtc;

run;

data finish;

set have;

by usubjid ecendtc;

if last.usubjid;

keep usubjid ecendtc;

run;

 

Perhaps you can see the handwriting on the wall here ... your life will be a lot simpler down the road if you start out by converting  those character variables to numeric DATETIMEs, since SAS is built to handle those easily.

Solution
‎08-31-2017 02:36 AM
Super User
Super User
Posts: 7,860

Re: first. and last.

That is not how the by variables work. They indicate whether the current record is the first or the last for the group defined by the by variable.  So FIRST.ECSTDTC and LAST.ECENDTC could only be true if there is only one record for that value of ECSTDTC within that value of USUBJID.

 

If your data it properly sorted and has no missing values then you want.

data ec1;
  set ec7;
  by usubjid ;
  retain first_start ;
  if first.usubjid then first_start=ECSTDTC;
  if last.usubjid ;
  last_stop = ECENDTC;
  keep usubjid first_start last_stop ;
run;

If you have missing values then you will need to add more logic and also retain the variable you use to store the last end date.

Regular Contributor
Posts: 200

Re: first. and last.

Thanks a lot Tom.

Can the new date format be converted to date9(characcter) in the same data step?

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 11 replies
  • 375 views
  • 2 likes
  • 4 in conversation