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

I am trying to connect MSSQL Server with SAS using the following:

 

1. SQL Pass-Through, or explicit pass-through

2. LIBNAME statement, or implicit pass-through

 

But I am unable to make a connection between them.

 

Here are my MSSQl details (I am trying to do this using Windows authentication)

vijaypratap0195_1-1685183775710.png

 

vijaypratap0195_2-1685183872964.png

 

Can anyone help with the complete code I can use in my SAS EG to connect SAS with MSSQL?

 

-Vijay

1 ACCEPTED SOLUTION

Accepted Solutions
Quentin
Super User

Great, so that shows that you're connected to the right server, but wrong database.

 

Are you able to pass the database name inside the connection string, something like:

 

libname SQLSRVR odbc noprompt = "Driver=SQL Server;server=VIJAY_PRATAP\SQLEXPRESS;Trusted Connection=yes;DATABASE=test" ;

?  You might need to look up the connection string syntax for SQLEXPRESS.   There are examples at https://www.connectionstrings.com/sql-server/ .

BASUG is hosting free webinars Next up: Mike Raithel presenting on validating data files on Wednesday July 17. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.

View solution in original post

14 REPLIES 14
ballardw
Super User

First run this code and then show us the result from the log:

Proc setinit; 
run;

Go to the log and copy the results starting at Product Expiration dates and then post into a text box opened on the forum using the </> icon above the main message window. You should see something like:

Product expiration dates:
---Base SAS Software
      30JAN2025
---SAS/STAT
      30JAN2025

The output shows what products you have licensed to use. If you don't have SAS Access/SQL Server (or whatever name actually comes up) you may be able to access data through  the ODBC connection but that has different limits and abilities than the SQL Server package.

 

If your SAS is running EG on a server then the links would have to be available to server. It would not see any local connection you may have to SQL server and likely need to get your SAS admin involved in getting the mappings set up.

SASKiwi
PROC Star

Where is your SAS installed? On a server or on a PC? What SAS/ACCESS product are you using? SQL Server or ODBC? Please post the SAS logs for your two attempts including any errors.

vijaypratap0195
Obsidian | Level 7

I have installed in my PC

vijaypratap0195_0-1685291424590.png

 

SASKiwi
PROC Star

Please post the SAS logs from your two attempts.

Tom
Super User Tom
Super User

That photograph (why take the time to make a picture out of text?) you posted appears to be from SAS running on some server.  When I run SETINIT on SAS running on PC it does not mention anything about CPU's.

Product expiration dates:
---Base SAS Software                                                                                    31OCT2023
---SAS/STAT                                                                                             31OCT2023
---SAS/GRAPH                                                                                            31OCT2023
---SAS/ETS                                                                                              31OCT2023
---SAS/FSP                                                                                              31OCT2023
---SAS/OR                                                                                               31OCT2023
---SAS/AF                                                                                               31OCT2023
---SAS/IML                                                                                              31OCT2023
---SAS/QC                                                                                               31OCT2023
---SAS/SHARE                                                                                            31OCT2023
---SAS/ASSIST                                                                                           31OCT2023
---SAS/CONNECT                                                                                          31OCT2023
---SAS/EIS                                                                                              31OCT2023
---SAS/SHARE*NET                                                                                        31OCT2023
---MDDB Server common products                                                                          31OCT2023
---SAS/Secure 168-bit                                                                                   31OCT2023
---SAS/Secure Windows                                                                                   31OCT2023
---SAS Enterprise Guide                                                                                 31OCT2023
---OR OPT                                                                                               31OCT2023
---OR PRS                                                                                               31OCT2023
---OR IVS                                                                                               31OCT2023
---OR LSO                                                                                               31OCT2023
---SAS/ACCESS Interface to Oracle                                                                       31OCT2023
---SAS/ACCESS Interface to PC Files                                                                     31OCT2023
---SAS/ACCESS Interface to ODBC                                                                         31OCT2023
---SAS/IML Studio                                                                                       31OCT2023
---SAS Workspace Server for Local Access                                                                31OCT2023
---High Performance Suite                                                                               31OCT2023

Compared to running in SAS on UNIX server:

---Base SAS Software                                                                                    30DEC2023 (CPU A)
---SAS/STAT                                                                                             30DEC2023 (CPU A)
---SAS/GRAPH                                                                                            30DEC2023 (CPU A)
---SAS/ETS                                                                                              30DEC2023 (CPU A)
---SAS/FSP                                                                                              30DEC2023 (CPU A)
---SAS/OR                                                                                               30DEC2023 (CPU A)
---SAS/AF                                                                                               30DEC2023 (CPU A)
---SAS/IML                                                                                              30DEC2023 (CPU A)
---SAS/QC                                                                                               30DEC2023 (CPU A)
---SAS/SHARE                                                                                            30DEC2023 (CPU A)
---SAS/CONNECT                                                                                          30DEC2023 (CPU A)
---SAS/SHARE*NET                                                                                        30DEC2023 (CPU A)
---SAS/IntrNet                                                                                          30DEC2023 (CPU A)
---SAS Integration Technologies                                                                         30DEC2023 (CPU A)
---SAS/Secure 168-bit                                                                                   30DEC2023 (CPU A)
---SAS/Genetics                                                                                         30DEC2023 (CPU A)
---SAS Enterprise Guide                                                                                 30DEC2023 (CPU A)
---OR OPT                                                                                               30DEC2023 (CPU A)
---OR PRS                                                                                               30DEC2023 (CPU A)
---OR IVS                                                                                               30DEC2023 (CPU A)
---OR LSO                                                                                               30DEC2023 (CPU A)
---SAS/ACCESS Interface to Oracle                                                                       30DEC2023 (CPU A)
---SAS/ACCESS Interface to PC Files                                                                     30DEC2023 (CPU A)
---SAS/ACCESS Interface to ODBC                                                                         30DEC2023 (CPU A)
---SAS/ACCESS Interface to Microsoft SQL Server                                                         30DEC2023 (CPU A)
---Grid Manager for Platform                                                                            30DEC2023 (CPU A)
---SAS/IML Studio                                                                                       30DEC2023 (CPU A)
---SAS Workspace Server for Local Access                                                                30DEC2023 (CPU A)
---SAS Workspace Server for Enterprise Access                                                           30DEC2023 (CPU A)
---SAS/ACCESS to Amazon Redshift                                                                        30DEC2023 (CPU A)
---High Performance Suite                                                                               30DEC2023 (CPU A)
---SAS Add-in for Microsoft Excel                                                                       30DEC2023 (CPU A)
---SAS Add-in for Microsoft Outlook                                                                     30DEC2023 (CPU A)
---SAS Add-in for Microsoft PowerPoint                                                                  30DEC2023 (CPU A)
---SAS Add-in for Microsoft Word                                                                        30DEC2023 (CPU A)
---SAS/ACCESS to Snowflake                                                                              30DEC2023 (CPU A)

I am not sure if using Windows Authentication can work from Unix.

 

ballardw
Super User

@vijaypratap0195 wrote:

I have installed in my PC

vijaypratap0195_0-1685291424590.png

 


That actually only shows that the product is licensed.

Proc product_status;
run;

will verify that the modules are actually installed.

The log looks something like this for each product installed:

For Base SAS Software ...
   Custom version information: 9.4_M4
   Image version information: 9.04.01M4P110916
For SAS/STAT ...
   Custom version information: 14.2
For SAS/GRAPH ...
   Custom version information: 9.4_M4

You should be able to copy text from your log and then on the forum open a text box using the </> icon above the message window. Then paste in the text box. For one thing it should be faster than making an image and attaching, second it is easier to read and in the cases of syntax discussion, much easier to copy, correct or make suggestions and paste.

Quentin
Super User

Since you mention EG and you have SAS/ACCESS to SQL Server, that makes me think that EG (which might be running on Windows) is connecting to a linux SAS server.  Is that right?  

 

If so, then you won't be able to use Windows authentication, because the connection is made from the linux server where the SAS code executes to the database.

 

For connecting from a SAS server, connecting to a new SQL server would typically involve having a SAS server administrator update the odbc.ini file on the server which defines the ODBC connections, and the administrator might also create a SAS library for you.  I would suggest working with your server administrator.  Or do you have admin privileges on the server?

 

But then you also said you have "installed on your PC."  Are you actually running SAS on your local PC (not a server), and using EG to connect to the SAS session on your PC?

BASUG is hosting free webinars Next up: Mike Raithel presenting on validating data files on Wednesday July 17. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
vijaypratap0195
Obsidian | Level 7

I am able to establish the connection between SAS and MSSQL using this:

 

libname SQLSRVR odbc noprompt = "Driver=SQL Server;server=VIJAY_PRATAP\SQLEXPRESS;Trusted Connection=yes" DATABASE=test schema=dbo;

 

vijaypratap0195_0-1685428901262.png

 

Connection test: 

%put %sysfunc(ifc(%sysfunc(libref(SQLSRVR)) = 0, "Connection successful", "Connection failed"));

vijaypratap0195_1-1685429937691.png

proc datasets lib=SQLSRVR ;
quit;

vijaypratap0195_2-1685429985867.png

I can see only these many datasets/Tables.

 

In MSSQL my table is located here in test database:

vijaypratap0195_3-1685430068196.png

 

Can you please tell me how can I fetch the Table data in SAS here?

 

I tried :

proc print data=SQLSRVR.MyTable; run;

vijaypratap0195_4-1685430136283.png

 

 

Quentin
Super User

Instead of using trusted_connection=yes, maybe try assing user ID and password in the connection string.

 

Also what happens if you run:

data sqlsrvr.foo;
  x=1;
run;

Does the SAS log say that it successfully created the table?  Can you see the table in SQL Server Management Studio?

BASUG is hosting free webinars Next up: Mike Raithel presenting on validating data files on Wednesday July 17. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
vijaypratap0195
Obsidian | Level 7

It runs without an error:


data sqlsrvr.foo;
x=1;
run;

 

vijaypratap0195_0-1685451962649.png

 

table got created in master database instead of test:

vijaypratap0195_0-1685452332930.png

 

code:

 

libname SQLSRVR odbc noprompt = "Driver=SQL Server;server=VIJAY_PRATAP\SQLEXPRESS;Trusted Connection=yes" DATABASE=test schema=dbo;

%put %sysfunc(ifc(%sysfunc(libref(SQLSRVR)) = 0, "Connection successful", "Connection failed"));

proc datasets lib=SQLSRVR ;
quit;

data sqlsrvr.foo;
x=1;
run;

 

AhmedAl_Attar
Rhodochrosite | Level 12

@vijaypratap0195 

Try using SQL Passthrough

Proc SQL;
connect using sqlsrvr as sqlsrvr;
create table <mytable> as
select * from connection to sqlsrvr (Select * from dbo.Mytable);
disconnect from sqlsrvr;
quit;

Here is paper for more details and illustration

Emulating FIRST. and LAST. SAS® DATA Step Processing in SQL? Concepts and Review (lexjansen.com)

 

Hope this helps

vijaypratap0195
Obsidian | Level 7

code:

libname SQLSRVR odbc noprompt = "Driver=SQL Server;server=VIJAY_PRATAP\SQLEXPRESS;Trusted Connection=yes" DATABASE=test schema=dbo;

Proc SQL;
connect using sqlsrvr as sqlsrvr;
create table a as
select * from connection to sqlsrvr
(Select * from dbo.Mytable);
disconnect from sqlsrvr;
quit;

 

vijaypratap0195_0-1685453270266.png

 

Quentin
Super User

Great, so that shows that you're connected to the right server, but wrong database.

 

Are you able to pass the database name inside the connection string, something like:

 

libname SQLSRVR odbc noprompt = "Driver=SQL Server;server=VIJAY_PRATAP\SQLEXPRESS;Trusted Connection=yes;DATABASE=test" ;

?  You might need to look up the connection string syntax for SQLEXPRESS.   There are examples at https://www.connectionstrings.com/sql-server/ .

BASUG is hosting free webinars Next up: Mike Raithel presenting on validating data files on Wednesday July 17. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
vijaypratap0195
Obsidian | Level 7

Worked, Thanks

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 14 replies
  • 3072 views
  • 0 likes
  • 6 in conversation