BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
David_Billa
Rhodochrosite | Level 12

Can someone help me understand the meaning of this SAS code?

 

today() between Start_dt and End_dt
1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

The today() function returns a SAS Date value representing the current date (numerical value; count of days since 1/1/1960)

SAS variables start_dt and end_dt must also contain SAS Date values

 

This expression returns TRUE if the value for today() is >= start_dt and <= end_dt

Another way to write the expression would be: start_dt <= today() <= end_dt

 

Hope that explains it to you.

 

If you don't understand how SAS Date, DateTime and Time values work then I'd strongly recommend some "Googling" to find the relevant SAS documentation. It's important that you do understand this and it's explained in detail in the SAS docu.

View solution in original post

6 REPLIES 6
Patrick
Opal | Level 21

The today() function returns a SAS Date value representing the current date (numerical value; count of days since 1/1/1960)

SAS variables start_dt and end_dt must also contain SAS Date values

 

This expression returns TRUE if the value for today() is >= start_dt and <= end_dt

Another way to write the expression would be: start_dt <= today() <= end_dt

 

Hope that explains it to you.

 

If you don't understand how SAS Date, DateTime and Time values work then I'd strongly recommend some "Googling" to find the relevant SAS documentation. It's important that you do understand this and it's explained in detail in the SAS docu.

PaigeMiller
Diamond | Level 26

Adding to @Patrick 's explanation

 

BETWEEN can only be used in PROC SQL, not elsewhere in SAS.

--
Paige Miller
Patrick
Opal | Level 21

@PaigeMiller 

Let's say where clause given below syntax is also valid in SAS data steps.

data have;
  start_dt=today()-10;
  end_dt=today()+10;
run;

data demo_1;
  set have(where=(today() between start_dt and end_dt));
run;

data demo_2;
  set have;
  where today() between start_dt and end_dt;
run;

proc sql;
  create table demo_3 as  
  select *
  from have
  where today() between start_dt and end_dt
  ;
quit;
PaigeMiller
Diamond | Level 26

Thanks, @Patrick 

 

I have never used BETWEEN in that way. So then it would be correct to say BETWEEN is valid in WHERE statements/clauses?


The following doesn't work

 

data a;
    set sashelp.class;
    if age between 14 and 15 then age_14_15_flag=1;
    else age_14_15_flag=0;
run;
--
Paige Miller
garkbeda43
Calcite | Level 5
Thanks for solution..
Tom
Super User Tom
Super User

Basically WHERE clauses mimic SQL functionality.  So you use use BETWEEN and LIKE.

 

If suspect it is so all of the code that SAS uses to do implicit pass thru to remote SQL databases can be reused in processing WHERE clauses.

 

Note that some things that SQL does not support the WHERE clause does.  Like colon modifier on comparison operators.

1520  proc print data=sashelp.class;
1521    where name =: 'A' ;
1522  run;

NOTE: There were 2 observations read from the data set SASHELP.CLASS.
      WHERE name=:'A';
NOTE: PROCEDURE PRINT used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds


1523
1524  proc sql;
1525    select * from sashelp.class
1526    where name =: 'A'
                    -
                    22
                    200
ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string, a numeric constant, a datetime constant,
              a missing value, (, *, +, -, ALL, ANY, BTRIM, CALCULATED, CASE, INPUT, PUT, SELECT, SOME, SUBSTRING, TRANSLATE,
              USER.

ERROR 200-322: The symbol is not recognized and will be ignored.

1527    ;
1528  quit;
NOTE: The SAS System stopped processing this step because of errors.
NOTE: PROCEDURE SQL used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of 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
  • 6 replies
  • 723 views
  • 4 likes
  • 5 in conversation