BookmarkSubscribeRSS Feed
Babloo
Rhodochrosite | Level 12

I would like to know how to filter the records based on two different dates. Assume I've a variables called Valid_from and it has  a value 20200323T000000 and other variable called Valid_to and it has a value 99991231T235959 and also other few variables. Formats and informats for Valid_from and Valid_to is B8601DT.

 

Now I want to select the records only if the current datetime is between  Valid_from and Valid_to.

 

May be something like below but I'm not sure.

 

if VALID_FROM <= DATETIME() <= VALID_TO;

 

I do not have SAS EG at the Moment. So appericiate if someone confirm if the above condition works, otherwise please tell me the correct one. 

 

8 REPLIES 8
PeterClemmensen
Tourmaline | Level 20

I can confirm that 🙂

 

data test;
   VALID_FROM = '23mar2020:00:00:00'dt;
   VALID_TO   = '31dec9999:23:59:59'dt;
   if VALID_FROM <= DATETIME() <= VALID_TO then put 'It works'; 
   format VALID_FROM VALID_TO B8601DT.;
run;

 

Output:

 

It works
Babloo
Rhodochrosite | Level 12

Is it possible for you to show me with my example data and the format which I applied and then applying the filters?

PeterClemmensen
Tourmaline | Level 20

I don't see any example data?

Babloo
Rhodochrosite | Level 12
I gave the example values and the format I used in the initial post
PeterClemmensen
Tourmaline | Level 20

Please look at my code again. I use the variables, values and format exactly as you describe.

Patrick
Opal | Level 21

@Babloo 

As you might know the format applied to a variable doesn't change its internal value so the format is of no relevance here.

Furthermore the variable names indicate that you're dealing with an SCD Type2 structure likely loaded using the DIS SCD Type 2 transformation. If using Datetime() in the selection then you're only after the current record and if so then below two clauses would also work as an alternative:

1. where change_current_ind='Y' 

2. datetime() < valid_to_dttm

 

The 2nd option will work because the SCD Type 2 loader will expire all not current rows based on datetime() when loading; and all current rows will have a valid_to_date somewhere far in the future. 

Kurt_Bremser
Super User

@Babloo wrote:

Is it possible for you to show me with my example data and the format which I applied and then applying the filters?


You did not show example data, only example values. Example data = a data step that creates a dataset.

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!

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
  • 8 replies
  • 1125 views
  • 5 likes
  • 5 in conversation