BookmarkSubscribeRSS Feed
steffick
Calcite | Level 5

Hi,

I'm having problems with this piece of code. I get different errors when I change the order of the tables I'm left joining.

proc sql;
CONNECT TO SQLSVR AS CDW(DATAsrc=<project name> &SQL_OPTIMAL.);

create table scr0 as select scrssn, 
	labchemresultnumericvalue as num, 
	labchemcompletedatetime as res, 
	upper(LabChemTestName) as test_name, 
	upper(c.topography) as topography, e.loinc, 35 as dsslarno
	
 connection to cdw

(select d.scrssn, a.patientsid, labchemresultnumericvalue
as num, labchemcompletedatetime as res, 
upper(LabChemTestName) as test_name, 
upper(c.topography) as topography, e.loinc from

src.chem_labchem as a

	left join CDWWork.dim.labchemtest as  b on  
		a.LabChemTestSID=b.labchemtestsid

	left join CDWWork.dim.Topography as c  on 
		a.TopographySID=c.TopographySID) 

	left join Src.CohortCrosswalk d on 
		a.patientsid=d.patientsid

	left join cdwwork.dim.loinc e on
		a.LOINCSID=e.LOINCSID;
quit;

When I have the tables in the order above (abcde) I get:

left join cdwwork.dim.loinc as e on

                                 22

                                 76
ERROR 22-322: Syntax error, expecting one of the following: a name, (, AS, ON.

I have other code that uses the same 3 part dataset name and it runs fine.

 

When I switch the datasets around to acbde or abdce:

left join CDWWork.dim.labchemtest as b on
                               _
                               22
                               76

When in aebdc order:

ERROR: Unresolved reference to table/correlation name c.
ERROR: Unresolved reference to table/correlation name e.
ERROR: The following columns were not found in the contributing tables: labchemcompletedatetime, labchemresultnumericvalue, LabChemTestName.
NOTE: PROC SQL set option NOEXEC and will continue to check the syntax of statements.

 

2 REPLIES 2
Tom
Super User Tom
Super User

Your tables names are not valid SAS dataset names.  If you want to refer to a dataset with a name instead of quoted physical path you can use with a one level name (for a WORK or perhaps a USER datasets) or a two level name.  You cannot use a three level name.

 

What datasets are you actually trying to query?

 

SASKiwi
PROC Star

You aren't closing your passthrough query correctly and repeating your SQL logic in SAS SQL outside of the passthrough section isn't a good idea. Try something like this:

proc sql;
CONNECT TO SQLSVR AS CDW(DATAsrc=<project name> &SQL_OPTIMAL.);

create table scr0 as 
select *
	
 from connection to cdw

(select d.scrssn, a.patientsid, labchemresultnumericvalue
as num, labchemcompletedatetime as res, 
upper(LabChemTestName) as test_name, 
upper(c.topography) as topography, e.loinc from

src.chem_labchem as a

	left join CDWWork.dim.labchemtest as  b on  
		a.LabChemTestSID=b.labchemtestsid

	left join CDWWork.dim.Topography as c  on 
		a.TopographySID=c.TopographySID) 

	left join Src.CohortCrosswalk d on 
		a.patientsid=d.patientsid

	left join cdwwork.dim.loinc e on
		a.LOINCSID=e.LOINCSID
);
quit;

  

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

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
  • 2 replies
  • 298 views
  • 0 likes
  • 3 in conversation