Hi, I am trying to add the names in the where statement but getting error.
Proc sql ;
create table Test as
select distinct
debt_code,
ResultType,
datepart(tcfDateConfirmed) as Test_date format date9.,
DateConfirmed as Test_dt format datetime20.,
Description,
t3.Portfolioid,
t3.CaseflowPortfolioname,
t3.Accountstatus,
t3.Bookondate
from
(select
t1.ReferenceNumber,
t1.TraceResultType,
t1.DateConfirmed,
t2.Description
from Trace.CaseFlowData as t1
inner join Trace.ResultType as t2 on ( t1.TraceResultType = t2.TypeID and t2.TraceSource ='Test' and t1.TracerID = 99)/
inner join Test_account as t3 on ReferenceNumber = t3.accountnumber
Where
(t1.TimeStamp >= '01DEC2021:00:00:00'dt and t1.TimeStamp < '31DEC2021:00:00:00'dt) and
t3.Bookondate >= '01DEC2021:00:00:00'dt and t3.Bookondate < '31DEC2021:00:00:00'dt) and
t3.CaseflowPortfolioname like ('BT039', 'EDF058', 'EDF060', 'EDF059', 'BRITGAS129', 'EDF057', 'BRITGAS129');
quit;
When I add the bold line in the where clause to get only these portfolionames, I am getting below error:
54 from Trace.tCaseFlowDataAudit as t1
55 inner join Trace.tResultType as t2 on ( t1.TraceResultType = t2.TypeID and t2.TraceSource ='Test' and
55 ! t1.TracerID = 99)
56 inner join Test_account as t3 on ReferenceNumber = t3.accountnumber
57
58 Where
59 (t1.TimeStamp >= '01DEC2021:00:00:00'dt and t1.TimeStamp < '31DEC2021:00:00:00'dt) and
60 t3.CaseflowPortfolioname like ('BT039', 'EDF058', 'EDF060', 'EDF059', 'BRITGAS129', 'EDF057', 'BRITGAS129');
_
22
76
ERROR 22-322: Syntax error, expecting one of the following: !, !!, &, ), *, **, +, -, /, <, <=, <>, =, >, >=, ?, AND, BETWEEN,
CONTAINS, EQ, EQT, GE, GET, GT, GTT, IN, IS, LE, LET, LIKE, LT, LTT, NE, NET, NOT, NOTIN, OR, ^, ^=, |, ||, ~, ~=.
ERROR 76-322: Syntax error, statement will be ignored.
NOTE: PROC SQL set option NOEXEC and will continue to check the syntax of statements.
61 quit;
NOTE: The SAS System stopped processing this step because of errors.
NOTE: PROCEDURE SQL used (Total process time):
Code formatting.
Code formatting.
Code formatting.
As long as you write ugly, unstructured code like this, you'll have trouble seeing the possible problems.
And you can use the Enhanced Editor to detect the relationship of parentheses.
After applying indentation according to the brackets, I get this:
Proc sql ;
create table Test as
select distinct
debt_code,
ResultType,
datepart(tcfDateConfirmed) as Test_date format date9.,
DateConfirmed as Test_dt format datetime20.,
Description,
t3.Portfolioid,
t3.CaseflowPortfolioname,
t3.Accountstatus,
t3.Bookondate
from (
select
t1.ReferenceNumber,
t1.TraceResultType,
t1.DateConfirmed,
t2.Description
from Trace.CaseFlowData as t1
inner join Trace.ResultType as t2
on ( t1.TraceResultType = t2.TypeID and t2.TraceSource ='Test' and t1.TracerID = 99)
inner join Test_account as t3 on ReferenceNumber = t3.accountnumber
Where
(t1.TimeStamp >= '01DEC2021:00:00:00'dt and t1.TimeStamp < '31DEC2021:00:00:00'dt) and
t3.Bookondate >= '01DEC2021:00:00:00'dt and t3.Bookondate < '31DEC2021:00:00:00'dt
) and
t3.CaseflowPortfolioname like ('BT039', 'EDF058', 'EDF060', 'EDF059', 'BRITGAS129', 'EDF057', 'BRITGAS129')
;
quit;
You can see that a bracket terminates the sub-select, causing the rest of the WHERE condition to be invalid syntax.
So I tried to correct your code:
proc sql;
create table Test as
select distinct
debt_code,
ResultType,
datepart(tcfDateConfirmed) as Test_date format date9.,
DateConfirmed as Test_dt format datetime20.,
Description,
t3.Portfolioid,
t3.CaseflowPortfolioname,
t3.Accountstatus,
t3.Bookondate
from (
select
t1.ReferenceNumber,
t1.TraceResultType,
t1.DateConfirmed,
t2.Description
from Trace.CaseFlowData as t1
inner join Trace.ResultType as t2
on t1.TraceResultType = t2.TypeID and t2.TraceSource = 'Test' and t1.TracerID = 99
inner join Test_account as t3 on ReferenceNumber = t3.accountnumber
where
t1.TimeStamp >= '01DEC2021:00:00:00'dt and t1.TimeStamp < '31DEC2021:00:00:00'dt and
t3.Bookondate >= '01DEC2021:00:00:00'dt and t3.Bookondate < '31DEC2021:00:00:00'dt and
t3.CaseflowPortfolioname in ('BT039','EDF058','EDF060','EDF059','BRITGAS129','EDF057','BRITGAS129')
)
;
quit;
I also tried t3.CaseflowPortfolioname IN ('BT039', 'EDF058', 'EDF060', 'EDF059', 'BRITGAS129', 'EDF057', 'BRITGAS129') but getting same error.
Code formatting.
Code formatting.
Code formatting.
As long as you write ugly, unstructured code like this, you'll have trouble seeing the possible problems.
And you can use the Enhanced Editor to detect the relationship of parentheses.
After applying indentation according to the brackets, I get this:
Proc sql ;
create table Test as
select distinct
debt_code,
ResultType,
datepart(tcfDateConfirmed) as Test_date format date9.,
DateConfirmed as Test_dt format datetime20.,
Description,
t3.Portfolioid,
t3.CaseflowPortfolioname,
t3.Accountstatus,
t3.Bookondate
from (
select
t1.ReferenceNumber,
t1.TraceResultType,
t1.DateConfirmed,
t2.Description
from Trace.CaseFlowData as t1
inner join Trace.ResultType as t2
on ( t1.TraceResultType = t2.TypeID and t2.TraceSource ='Test' and t1.TracerID = 99)
inner join Test_account as t3 on ReferenceNumber = t3.accountnumber
Where
(t1.TimeStamp >= '01DEC2021:00:00:00'dt and t1.TimeStamp < '31DEC2021:00:00:00'dt) and
t3.Bookondate >= '01DEC2021:00:00:00'dt and t3.Bookondate < '31DEC2021:00:00:00'dt
) and
t3.CaseflowPortfolioname like ('BT039', 'EDF058', 'EDF060', 'EDF059', 'BRITGAS129', 'EDF057', 'BRITGAS129')
;
quit;
You can see that a bracket terminates the sub-select, causing the rest of the WHERE condition to be invalid syntax.
So I tried to correct your code:
proc sql;
create table Test as
select distinct
debt_code,
ResultType,
datepart(tcfDateConfirmed) as Test_date format date9.,
DateConfirmed as Test_dt format datetime20.,
Description,
t3.Portfolioid,
t3.CaseflowPortfolioname,
t3.Accountstatus,
t3.Bookondate
from (
select
t1.ReferenceNumber,
t1.TraceResultType,
t1.DateConfirmed,
t2.Description
from Trace.CaseFlowData as t1
inner join Trace.ResultType as t2
on t1.TraceResultType = t2.TypeID and t2.TraceSource = 'Test' and t1.TracerID = 99
inner join Test_account as t3 on ReferenceNumber = t3.accountnumber
where
t1.TimeStamp >= '01DEC2021:00:00:00'dt and t1.TimeStamp < '31DEC2021:00:00:00'dt and
t3.Bookondate >= '01DEC2021:00:00:00'dt and t3.Bookondate < '31DEC2021:00:00:00'dt and
t3.CaseflowPortfolioname in ('BT039','EDF058','EDF060','EDF059','BRITGAS129','EDF057','BRITGAS129')
)
;
quit;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.