Longitudinal calculation across observations

Solved
Super Contributor
Posts: 383

Longitudinal calculation across observations

[ Edited ]

I'd like to calculate lagtime in admission dates between source 1 and 2. Below attempt results in lagtime variable created but missing all the way. Not calculated. Admission_date is a SAS date and numeric. I had no problem for substraction in data step in other programs.

Do you see where I mess up?

``````data demo;
cards;
m12 1 20110
m12 2 20114
m13 1 20119
m13 2 20231
m14 1 20500
m14 2 20600
;
run;

proc sort data=demo;
by UID source;
run;

DATA demo1; SET demo;
BY UID source;
IF FIRST.source THEN DO;
END;
IF LAST.source THEN DO;
OUTPUT;
END;
DROP R_: ;
RUN;``````

Thank you very much for your time.

Accepted Solutions
Solution
‎07-19-2017 09:45 AM
Super User
Posts: 24,028

Re: Longitudinal calculation across observations

Cruise wrote:
Even if i am interested in the difference between source 1 and 2 within same uid?

Yes.

``````data demo2;
set demo;
by uid source;
first_source= first.source;
last_source = last.source;
first_uid=first.uid;
last_uid=last.uid;

run;

proc print;run;``````

Obs UID Source <- UID + SOURCE uniquely identifies a record admission_date first_source last_source first_uid last_uid
1 m12 1 20110 1 1 1 0
2 m12 2 20114 1 1 0 1
3 m13 1 20119 1 1 1 0
4 m13 2 20231 1 1 0 1
5 m14 1 20500 1 1 1 0
6 m14 2 20600 1 1 0 1

All Replies
Super User
Posts: 6,939

Re: Longitudinal calculation across observations

This program should give you all zero values (not all missing values).

The logic should condition on FIRST.UID and LAST.UID, not FIRST.SOURCE and LAST.SOURCE.  The way it reads now, it takes the beginning and ending date from the same observation.

Super Contributor
Posts: 383

Re: Longitudinal calculation across observations

Even if i am interested in the difference between source 1 and 2 within same uid?
No sas with me yet. Will try your suggestion when i get to it.
Solution
‎07-19-2017 09:45 AM
Super User
Posts: 24,028

Re: Longitudinal calculation across observations

Cruise wrote:
Even if i am interested in the difference between source 1 and 2 within same uid?

Yes.

``````data demo2;
set demo;
by uid source;
first_source= first.source;
last_source = last.source;
first_uid=first.uid;
last_uid=last.uid;

run;

proc print;run;``````

Obs UID Source <- UID + SOURCE uniquely identifies a record admission_date first_source last_source first_uid last_uid
1 m12 1 20110 1 1 1 0
2 m12 2 20114 1 1 0 1
3 m13 1 20119 1 1 1 0
4 m13 2 20231 1 1 0 1
5 m14 1 20500 1 1 1 0
6 m14 2 20600 1 1 0 1

Super Contributor
Posts: 383

Re: Longitudinal calculation across observations

[ Edited ]

Thanks a lot. Below codes suggested worked out. It outputs half of the data with lagtime between same UID which I wanted. However, I'd like to retain whole dataset. For that purpose when I eliminate:

`` IF last.UID THEN OUTPUT;``

output is not truncated to the last obs of UID but it contains lagtime between adjacent but different UIDs which i do not need. To get back to the whole dataset I merge the lagtime output data to the original one.

I wonder, if there is a way to output whole dataset with lagtime within same UID but no unnessecary lagtime calculated between different UIDs?

``````proc sort data=demo;
by UID;

DATA demo1; SET demo;
BY UID;
IF FIRST.UID THEN DO;
END;
IF LAST.UID THEN DO;
OUTPUT;
END;
DROP R_: ;
RUN;``````

Using lag function:

`````` data demo1; set demo;
by UID;
IF last.UID THEN OUTPUT;
run;``````

Thank you all.

My final code then is:

`````` data have1(drop=admission_startdate); set have(keep=UID admission_startdate);
by UID;
IF last.UID THEN OUTPUT;
run;

data want;
merge
doh.have  (in=a)
doh.have1 (in=b);
by UID;
if a and b then output;
run;
``````

Super User
Posts: 2,519

Re: Longitudinal calculation across observations

If you have pairs of observations, the dif function may interest you.

``````
data DEMO1;
set DEMO;
by UID ;