BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
tomas-andriotti
Calcite | Level 5

Hi,

 

I have a longitudinal dataset with many ED (emergency department) visits per individual (id). Each visit has an index date (begdate) and the variable year. I am trying to model a cox for recurrent events creating time0 (start) and time2 (stop).

 

The main variables are:

id: individuals

k: observations or visits

begdate : date of the observations - visit the ED

year : calendar year 

freq_user_y : 4 visits or more to the ED a year

 

I created the difference between the observations and the lag time but it turns out the observations k=1 are being excluded because they do not have a time2. And the lag time has been created just for the observations k >= 2 .

I wonder how I can create a time 2 for the first observation then the difference between time 1 and 2 (t2-t1) be kept in this line (k=1), not in the second visit (k=2) and so on, time difference t3-t2 in line k=2, not in k=3...

 

Look forward to hearing from you.

 

Tomas

1 ACCEPTED SOLUTION

Accepted Solutions
noling
SAS Employee

Ok, so only scooting up records that have more than 1 ID records. I changed my sample data and added a "BY" statement and second output condition for single ID records:

 

data have;
	length id k date 8.; 
	format date mmddyy10.;
	id=1;
		do k = 1 to 4;
			date = '06MAR2019'd + (k*k);
			output;
		end;
	k=1;date = '06MAR2019'd;
	id=2; output;
	id=3; output;

run;
data temp;
	set have;
	by id notsorted;
	format prev_date mmddyy10.;
	prev_date= lag(date);

	if first.id then call missing(prev_date);
	diff=date - prev_date;
run;

*now scoot ONLY the diff's up a line if their id is in more than one row.
Output the single ID records; 
data want(rename=(temp_id = id temp_k=k temp_date=date));
	set temp;
	by id;
	length temp_id temp_k temp_date 8.;
	format temp_date mmddyy10.;
	retain temp_id temp_k temp_date;

	if not missing(diff) then output;

	temp_id = id;
	temp_k= k;
	temp_date = date;
	keep diff temp_id temp_k temp_date;
	
	*output again for single records with only 1 ID row;
	if first.id and last.id then output;
run;

Yields:

Capture.PNG


Register today and join us virtually on June 16!
sasglobalforum.com | #SASGF

View now: on-demand content for SAS users

View solution in original post

4 REPLIES 4
noling
SAS Employee

I think this is what you're asking for, but I'm not sure it's what you really want:

data have;
	length id k date 8.; 
	format date mmddyy10.;
	do id = 1 to 2;
		do k = 1 to 4;
			date = '06MAR2019'd + (k*k);
			output;
		end;
	end;
run;
data temp;
	set have;
	by id notsorted;
	format prev_date mmddyy10.;
	prev_date= lag(date);

	if first.id then call missing(prev_date);
	diff=date - prev_date;
run;

*now scoot all the diff's up a line. 
What do you want to do with the final diff per id?;
data want(rename=(temp_id = id temp_k=k temp_date=date));
	set temp;
	length temp_id temp_k temp_date 8.;
	format temp_date mmddyy10.;
	retain temp_id temp_k temp_date;

	if not missing(diff) then output;

	temp_id = id;
	temp_k= k;
	temp_date = date;
	keep diff temp_id temp_k temp_date;
run;

Creates:

Capture.PNG

So here, diff is the difference between (k+1) - k. Is this what you need? I would think through your problem before copying this logic. 


Register today and join us virtually on June 16!
sasglobalforum.com | #SASGF

View now: on-demand content for SAS users

tomas-andriotti
Calcite | Level 5

Hi,

I ran the command and it created another table with the difference in line 1 for all individuals even the ones with no second observation because there are individuals with no k2. So, I do have a dataset have and what I need is to scoot up the diff only for those individuals with a recurrent event ( k>2) those with no second event should not have a difference because they were not observed or followed up to a second time.

 

Look forward to hearing from you.

 

Tomas

noling
SAS Employee

Ok, so only scooting up records that have more than 1 ID records. I changed my sample data and added a "BY" statement and second output condition for single ID records:

 

data have;
	length id k date 8.; 
	format date mmddyy10.;
	id=1;
		do k = 1 to 4;
			date = '06MAR2019'd + (k*k);
			output;
		end;
	k=1;date = '06MAR2019'd;
	id=2; output;
	id=3; output;

run;
data temp;
	set have;
	by id notsorted;
	format prev_date mmddyy10.;
	prev_date= lag(date);

	if first.id then call missing(prev_date);
	diff=date - prev_date;
run;

*now scoot ONLY the diff's up a line if their id is in more than one row.
Output the single ID records; 
data want(rename=(temp_id = id temp_k=k temp_date=date));
	set temp;
	by id;
	length temp_id temp_k temp_date 8.;
	format temp_date mmddyy10.;
	retain temp_id temp_k temp_date;

	if not missing(diff) then output;

	temp_id = id;
	temp_k= k;
	temp_date = date;
	keep diff temp_id temp_k temp_date;
	
	*output again for single records with only 1 ID row;
	if first.id and last.id then output;
run;

Yields:

Capture.PNG


Register today and join us virtually on June 16!
sasglobalforum.com | #SASGF

View now: on-demand content for SAS users

tomas-andriotti
Calcite | Level 5
Hey thanks !
I believe it worked. I just have to merge table want with the dataset I have.
Best,
Tomas

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1418 views
  • 0 likes
  • 2 in conversation