DATA Step, Macro, Functions and more

Using a Date Constant with a IN Operator

Accepted Solution Solved
Reply
Contributor
Posts: 25
Accepted Solution

Using a Date Constant with a IN Operator

I am trying to use date constants in a numeric range.

 

Example:

 

 

data t;

	input date;
	format date ddmmyy10.;
datalines;
0
1
2
3
4
5
6
7
;
run;

proc print data=work.t;

	*where date in (3:6);
	where date in ('04JAN1960'd : '07JAN1960'd);
run;

 

The first where clause uses the numeric date and the programs execute without errors.

The second where clause uses the date constant and will generate an error.

 

The documentation specifies that the N and M should be numeric.

 

My question is why would it fail with a date constant or even a variable that is numeric when using the range feature with the IN operator.

 

http://support.sas.com/documentation/cdl/en/lrcon/69852/HTML/default/viewer.htm#p00iah2thp63bmn1lt20...

 

M, N, and all the integers

 

 


Accepted Solutions
Solution
‎02-07-2017 06:32 PM
Super User
Super User
Posts: 6,502

Re: Using a Date Constant with a IN Operator

I would assume that the rules for parsing the command doesn't support handling conversion of date literals.  It would also probably fail if you tried to use hexadecimal or binary literals also.

 

You can use macro functions to convert your human readable date into an integer that the SAS compiler will understand.  Try wrapping the date literals inside of the %SYSEVALF() function.

 

20   data x;
21    set sashelp.class ;
22    where age in (10:%sysevalf('01JAN2017'd));
23   run;

NOTE: There were 19 observations read from the data set SASHELP.CLASS.
      WHERE (age=INT(age)) and (age>=10 and age<=20820);
NOTE: The data set WORK.X has 19 observations and 5 variables.

View solution in original post


All Replies
Solution
‎02-07-2017 06:32 PM
Super User
Super User
Posts: 6,502

Re: Using a Date Constant with a IN Operator

I would assume that the rules for parsing the command doesn't support handling conversion of date literals.  It would also probably fail if you tried to use hexadecimal or binary literals also.

 

You can use macro functions to convert your human readable date into an integer that the SAS compiler will understand.  Try wrapping the date literals inside of the %SYSEVALF() function.

 

20   data x;
21    set sashelp.class ;
22    where age in (10:%sysevalf('01JAN2017'd));
23   run;

NOTE: There were 19 observations read from the data set SASHELP.CLASS.
      WHERE (age=INT(age)) and (age>=10 and age<=20820);
NOTE: The data set WORK.X has 19 observations and 5 variables.
Contributor
Posts: 25

Re: Using a Date Constant with a IN Operator

[ Edited ]

Thank you, Tom.

The solution works. I just wonder why it would fail when using a date
constant or numeric variable.

Explanation on SAS inner workings with %sysevalf:

 

Using the macro function the macro processor will first run and execute the macro code.

 

The macro processor will convert the following code:

 

if curr_date in (%sysevalf('01FEB2017'd) : %sysevalf('28FEB2017'd)) then do;

 

to

 

if curr_date in (20851 : 20878) then do;

 

and then the compiler can run the code as if it was numeric.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 130 views
  • 1 like
  • 2 in conversation