Desktop productivity for business analysts and programmers

Conditional Date Statement Issue

Accepted Solution Solved
Reply
Contributor
Posts: 55
Accepted Solution

Conditional Date Statement Issue

Hello,

 

I copied over some code from SQL Server and I'm trying to create a where statement when the two-digit month of a date equals 10 (October). I'm getting an error back when I execute.

 

where XXXX 

and a.month(timesent) = 10
                   _
                  22
                  76
ERROR 22-322: Syntax error, expecting one of the following: !, !!, &, *, **, +, -, /, <, <=, <>, =, >, >=, ?, AND, BETWEEN,
CONTAINS, EQ, EQT, GE, GET, GROUP, GT, GTT, HAVING, LE, LET, LIKE, LT, LTT, NE, NET, OR, ^=, |, ||, ~=.

ERROR 76-322: Syntax error, statement will be ignored.


Accepted Solutions
Solution
‎12-08-2017 02:48 PM
Super User
Super User
Posts: 7,855

Re: Conditional Date Statement Issue

The SQL alias applies to the variable, not the function.  So to find the month of the variable TIMESENT from the dataset A (or dataset aliases as A) you would use.

month(a.timesent)

Now if your TIMESENT is really a datetime value and not a date value then you need also use the DATEPART() function to convert the number of seconds into number of days so that you can use the MONTH() function.

month(datepart(a.timesent))

View solution in original post


All Replies
Super Contributor
Super Contributor
Posts: 260

Re: Conditional Date Statement Issue

Perhaps some example data and a little more of your code would be in order.

 

Function:  MONTH
Purpose:
To extract the month of the year from a SAS date (1 = January, 2=February, etc.).
Syntax:
MONTH(date)
date is a SAS date value.
Examples
MONTH('16AUG2002'd)
8

 

Data Input;
  input Acct_num Tran_Amt:dollar6. Tran_DT:mmddyy10. Item_ID:$3.;
  format tran_Dt yymmdd10.;
  datalines;
111 $100 6/1/2014  AAA
111 $200 8/2/2014  AAA
111 $500 6/4/2014  ABC
222 $300 8/3/2014  CCC
;
run;

proc sql;
	select tran_amt
	from input
	where month(tran_dt) = 8;
quit;

yeilds

 

                                        The SAS System       11:34 Friday, December 8, 2017   2

                                            Tran_Amt
                                                 200
                                                 300

 

Solution
‎12-08-2017 02:48 PM
Super User
Super User
Posts: 7,855

Re: Conditional Date Statement Issue

The SQL alias applies to the variable, not the function.  So to find the month of the variable TIMESENT from the dataset A (or dataset aliases as A) you would use.

month(a.timesent)

Now if your TIMESENT is really a datetime value and not a date value then you need also use the DATEPART() function to convert the number of seconds into number of days so that you can use the MONTH() function.

month(datepart(a.timesent))
Super Contributor
Super Contributor
Posts: 260

Re: Conditional Date Statement Issue

Duh!  Why didn't I see month(a.time)  vs a.month(time) ?  Bang! Zoom!

 

 

☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 228 views
  • 2 likes
  • 3 in conversation