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

Hello I am using SAS 9.4 and I am trying to figure out how to delete dates that aren't in my desired date range just for one ID.

For example:

 

DATA : OLD


ID                Date

1                   1/2/2018

1                  11/23/2017

1                  12/12/2017

1                  12/6/2017

1                   1/16/2018

1                   3/31/2018

1                  10/25/2017

 

2                    4/25/2017

2                    9/26/2017

2                    1/13/2018

2                    12/1/2017

 

DATA WANT:

 

ID                Date

1                   1/2/2018

1                  11/23/2017

1                  12/12/2017

1                  12/6/2017

 

2                    4/25/2017

2                    9/26/2017

2                    1/13/2018

2                    12/1/2017

 

I am trying to filter the dates that are not between 11/23/2017 and 1/2/2018 for just ID 1. I do not want ID 2 to change.

 

I am not sure how to go about this. Should I use an if statement?

 

Many thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

Hello @Kbug   The solution is in your description "I am trying to filter the dates that are not between 11/23/2017 and 1/2/2018 for just ID 1. I do not want ID 2 to change."  i.e Using between and logical operator

 

data have;
input ID Date : mmddyy10.;
format Date mmddyy10.;
datalines;
1 1/2/2018
1 11/23/2017
1 12/12/2017
1 12/6/2017
1 1/16/2018
1 3/31/2018
1 10/25/2017
2 4/25/2017
2 9/26/2017
2 1/13/2018
2 12/1/2017
;
proc sql;
create table want as
select *
from have
where id=1 and Date between '23nov2017'd  and '02jan2018'd or id ne 1;
quit;

View solution in original post

3 REPLIES 3
PeterClemmensen
Tourmaline | Level 20

One way

 

data have;
input ID Date : mmddyy10.;
format Date mmddyy10.;
datalines;
1 1/2/2018
1 11/23/2017
1 12/12/2017
1 12/6/2017
1 1/16/2018
1 3/31/2018
1 10/25/2017
2 4/25/2017
2 9/26/2017
2 1/13/2018
2 12/1/2017
;

data want;
	set have;
	if ID=1 and (Date lt '23nov2017'd | Date gt '02jan2018'd)  then delete;
run;
Kbug
Obsidian | Level 7

It ends up deleting all of ID dates, even if they fall in between the range. What does the lt and the gt mean in the if statement?

novinosrin
Tourmaline | Level 20

Hello @Kbug   The solution is in your description "I am trying to filter the dates that are not between 11/23/2017 and 1/2/2018 for just ID 1. I do not want ID 2 to change."  i.e Using between and logical operator

 

data have;
input ID Date : mmddyy10.;
format Date mmddyy10.;
datalines;
1 1/2/2018
1 11/23/2017
1 12/12/2017
1 12/6/2017
1 1/16/2018
1 3/31/2018
1 10/25/2017
2 4/25/2017
2 9/26/2017
2 1/13/2018
2 12/1/2017
;
proc sql;
create table want as
select *
from have
where id=1 and Date between '23nov2017'd  and '02jan2018'd or id ne 1;
quit;

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 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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 327 views
  • 1 like
  • 3 in conversation