BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
bgosiker
Obsidian | Level 7

 

Hello! 

 

I am trying to run a discrete time to event model and am trying to generate indicator variables for each respective study visit. I have people entering in at different times in a cohort (so not everyone has a visit 1 necessarily) . This would mean that the first visit for a person should have an indicator at time0 regardless of the visit number that they enter at and then each subsequent study visit will activate the next indicator variable. 

 

Given only the id and visit number the data I WANT generated is: 

 

id     visit     time0     time2     time3     time4     time5      time6      time7 

1    1     1      0      0      0      0      0      0

1    2     0      1      0      0      0      0      0   

1    3     0      0      1      0      0      0      0

1    4     0      0      0      1      0      0      0

1    5     0      0      0      0      1      0      0

1    6     0      0      0      0      0      1      0

1    7     0      0      0      0      0      0      1

2    2     1      0      0      0      0      0      0

2    3     0      1      0      0      0      0      0    

2    4     0      0      1      0      0      0      0

2    5     0      0      0      1      0      0      0

2    6     0      0      0      0      1      0      0

2    7     0      0      0      0      0      1      0

2    8     0      0      0      0      0      0      1

3    1     1      0      0      0      0      0      0

3    2     0      1      0      0      0      0      0

3    3     0      0      1      0      0      0      0

3    4     0      0      0      1      0      0      0

3    5     0      0      0      0      1      0      0

3    6     0      0      0      0      0      1      0

3    7     0      0      0      0      0      0      1

 

Here is the data that I have: 

 

Data have;
 input ID  visit ;
datalines;
1  1
1  2
1  3
1  4
1  5
1  6
1  7
2  2
2  3
2  4
2  5
2  6
2  7
3  1
3  2
3  3
3  4
3  5
3  6
;
run;

 

I appreciate any and all guidance 🙂 

 

1 ACCEPTED SOLUTION

Accepted Solutions
KachiM
Rhodochrosite | Level 12

The data set requires to be presorted by ID.

The time0 does not match with time2. Therefore time1 to time7 is used instead. Here is Data Step.

 

data want;
   do i = 1 by 1 until(last.id);
      set have;
      by id;
      array t[*] time1 - time7 (7*0);
      t[i] = 1;
      output;
      t[i] = 0;
   end;
drop i;
run;

View solution in original post

4 REPLIES 4
Jagadishkatam
Amethyst | Level 16
data want1;
set have;
by id visit;
time='time'||ifc(visit=1,'0',strip(put(visit,best.)));
val=1;
run;

options missing=0;
proc transpose data=want1 out=trans ;
by id visit;
id time;
var val;
run;
Thanks,
Jag
bgosiker
Obsidian | Level 7

Thanks for this step! 

 

When I run this, it generates a time variable that is just the time combined with the visit number. I altered this a bit and generated a count variable so that the first visit for each person would register as time0 and so on (code below).  I think my issue was mainly how to get the data into the format I had shown initially where it is a bunch of indicator variables for the number visit they are at. 

 

data want; set have;
	count+1; 
	by id visit; 
	if first.id then count=1;
run; 
KachiM
Rhodochrosite | Level 12

The data set requires to be presorted by ID.

The time0 does not match with time2. Therefore time1 to time7 is used instead. Here is Data Step.

 

data want;
   do i = 1 by 1 until(last.id);
      set have;
      by id;
      array t[*] time1 - time7 (7*0);
      t[i] = 1;
      output;
      t[i] = 0;
   end;
drop i;
run;
Ksharp
Super User
Data have;
 input ID  visit ;
datalines;
1  1
1  2
1  3
1  4
1  5
1  6
1  7
2  2
2  3
2  4
2  5
2  6
2  7
3  1
3  2
3  3
3  4
3  5
3  6
;
run;
data temp;
set have;
val=1;
run;

proc transpose data=temp out=temp1(drop=_name_) prefix=time ;
by id visit;
id visit;
var val;
run;

proc stdize data=temp1 out=want missing=0 reponly;
var _numeric_;
run;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 605 views
  • 0 likes
  • 4 in conversation