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

I have a dataset as shown in Table 1. I want to separate this dataset into two datasets made up of only Previous visits for each of the IDs present, and another dataset for only the visits after the index date for each ID. It’s important to have all ids whether they had a visit or not so each ID can be compared in the previous vs after phases of treatment. A little stumped on how to go about this, any help would be appreciated!

 

Table 1:

ID

Visit date

Index date

Previous visits

label

After index label

1

.

Jun7,2005

.

.

2

Jan3,1999

Apr8,1999

1

0

3

Jan3,1999

Jan1,1998

0

1

4

Oct7,2002

Sept10,2001

0

1

4

Jun1,2001

Sep10,2001

1

0

 

 

Wanted datasets:

Dataset A (Previous visits)

ID

Visit date

Index date

Previous visits

label

1

.

Jun7,2005

.

2

Jan3,1999

Apr8,1999

1

3

.

Jan1,1998

.

4

Jun1,2001

Sep10,2001

1

 

 Dataset 2 (After index visits)

ID

Visit date

Index date

After index label

label

1

.

Jun7,2005

.

2

.

Apr8,1999

.

3

Jan3,1999

Jan1,1998

1

4

Oct7,2002

Sep10,2001

1

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

Assuming I understand your question.

 

data have;
input ID
Visitdate : $20.
Indexdate : $20.
Previousvisitslabel	
Afterindexlabel ;
cards;
1
.
Jun7,2005
.
.
2
Jan3,1999
Apr8,1999
1
0
3
Jan3,1999
Jan1,1998
0
1
4
Oct7,2002
Sept10,2001
0
1
4
Jun1,2001
Sep10,2001
1
0
;
run;
proc sql;
create table A as
 select id,' ' as Visitdate,Indexdate,. as Previousvisitslabel from have
  where id not in (select id from have where Previousvisitslabel=1)
 union all
  select ID,
 Visitdate ,
Indexdate, 
Previousvisitslabel from have where Previousvisitslabel=1
order by id ;
  
  
  
create table B as
 select id,' ' as Visitdate,Indexdate,. as Afterindexlabel from have
  where id not in (select id from have where Afterindexlabel=1)
 union all
  select ID,
 Visitdate ,
Indexdate, 
Afterindexlabel from have where Afterindexlabel=1
order by id ;  
quit;


View solution in original post

1 REPLY 1
Ksharp
Super User

Assuming I understand your question.

 

data have;
input ID
Visitdate : $20.
Indexdate : $20.
Previousvisitslabel	
Afterindexlabel ;
cards;
1
.
Jun7,2005
.
.
2
Jan3,1999
Apr8,1999
1
0
3
Jan3,1999
Jan1,1998
0
1
4
Oct7,2002
Sept10,2001
0
1
4
Jun1,2001
Sep10,2001
1
0
;
run;
proc sql;
create table A as
 select id,' ' as Visitdate,Indexdate,. as Previousvisitslabel from have
  where id not in (select id from have where Previousvisitslabel=1)
 union all
  select ID,
 Visitdate ,
Indexdate, 
Previousvisitslabel from have where Previousvisitslabel=1
order by id ;
  
  
  
create table B as
 select id,' ' as Visitdate,Indexdate,. as Afterindexlabel from have
  where id not in (select id from have where Afterindexlabel=1)
 union all
  select ID,
 Visitdate ,
Indexdate, 
Afterindexlabel from have where Afterindexlabel=1
order by id ;  
quit;


SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 1 reply
  • 553 views
  • 0 likes
  • 2 in conversation