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-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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
  • 3 replies
  • 1072 views
  • 1 like
  • 3 in conversation