<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Executing a sql stored proc through sas and getting the results in to sas in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Executing-a-sql-stored-proc-through-sas-and-getting-the-results/m-p/533142#M146133</link>
    <description>&lt;P&gt;What results are you referring to? If the stored procedure produces a result set, this cannot be read by the EXECUTE statement. The way around this is to create a temporary table or view in the EXECUTE step, then follow that with a SELECT from CONNECTION TO ODS statement.&lt;/P&gt;</description>
    <pubDate>Wed, 06 Feb 2019 05:57:59 GMT</pubDate>
    <dc:creator>SASKiwi</dc:creator>
    <dc:date>2019-02-06T05:57:59Z</dc:date>
    <item>
      <title>Executing a sql stored proc through sas and getting the results in to sas</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Executing-a-sql-stored-proc-through-sas-and-getting-the-results/m-p/533136#M146130</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;I tried to execute a sql stored proc and it went well.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But how to get the results to sas log.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The sas program is :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;PROC SQL;&lt;/P&gt;&lt;P&gt;CONNECT TO ODBC AS ods ( DATAsrc=PriorityYou_Test USER='XXXXXXXXXX' PASSWORD='XXXXXX');&lt;BR /&gt;execute ([PriorityYou].[dbo].[HelloWorld] ) by ods;&lt;BR /&gt;%PUT &amp;amp;SQLXRC. &amp;amp;SQLXMSG. ;&lt;BR /&gt;DISCONNECT FROM ods;&lt;BR /&gt;QUIT;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Do we need to add any options or is there any other way?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 07 Feb 2019 01:18:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Executing-a-sql-stored-proc-through-sas-and-getting-the-results/m-p/533136#M146130</guid>
      <dc:creator>Swathi12</dc:creator>
      <dc:date>2019-02-07T01:18:23Z</dc:date>
    </item>
    <item>
      <title>Re: Executing a sql stored proc through sas and getting the results in to sas</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Executing-a-sql-stored-proc-through-sas-and-getting-the-results/m-p/533142#M146133</link>
      <description>&lt;P&gt;What results are you referring to? If the stored procedure produces a result set, this cannot be read by the EXECUTE statement. The way around this is to create a temporary table or view in the EXECUTE step, then follow that with a SELECT from CONNECTION TO ODS statement.&lt;/P&gt;</description>
      <pubDate>Wed, 06 Feb 2019 05:57:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Executing-a-sql-stored-proc-through-sas-and-getting-the-results/m-p/533142#M146133</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2019-02-06T05:57:59Z</dc:date>
    </item>
    <item>
      <title>Re: Executing a sql stored proc through sas and getting the results in to sas</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Executing-a-sql-stored-proc-through-sas-and-getting-the-results/m-p/533148#M146138</link>
      <description>&lt;P&gt;I do this all the time (MS SQL Server), here is example code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
   connect using rldx;
   create table rldx_operational_metrics as
   select * 
   from connection to rldx (
EXEC etl.pLoad_RLDX_OPERATIONAL
   );
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;In this example, the stored procedure loads a target table, then returns metrics about the load as a result set.&amp;nbsp; I want to display those results in EG, so I create a table, and later use PROC PRINT to display formatted results.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Read the doc for options that may be unclear, such as connect &lt;FONT color="#FF0000"&gt;&lt;U&gt;&lt;STRONG&gt;using&lt;/STRONG&gt; &lt;/U&gt;&lt;/FONT&gt;&amp;lt;ODBC libref&amp;gt;.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;P.S.: Probably not a great idea to post your database, user name, and password in a public forum.&amp;nbsp; Your firewall is probably secure, but anyone inside Westpac that has access to your server, and reads this post, could probably read your data.&amp;nbsp; Up to you whether that complies with your internal security requirements.&amp;nbsp; If not, perhaps edit your post &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Feb 2019 06:46:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Executing-a-sql-stored-proc-through-sas-and-getting-the-results/m-p/533148#M146138</guid>
      <dc:creator>ScottBass</dc:creator>
      <dc:date>2019-02-06T06:46:09Z</dc:date>
    </item>
    <item>
      <title>Re: Executing a sql stored proc through sas and getting the results in to sas</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Executing-a-sql-stored-proc-through-sas-and-getting-the-results/m-p/533459#M146255</link>
      <description>&lt;P&gt;Thanks a lot for your guidance.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But the output of my SQL Sp is below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc [HelloWorld] had started at : 02/06/19&amp;nbsp; 3:24:42 PM&lt;/P&gt;&lt;P&gt;Hello World from TEST ODS&lt;/P&gt;&lt;P&gt;Line 1&lt;/P&gt;&lt;P&gt;Line 2&lt;/P&gt;&lt;P&gt;Stored proc [HelloWorld] had ended at : 02/06/19&amp;nbsp; 3:24:42 PM&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;where as i am getting below output from the dataset&amp;nbsp;&lt;/P&gt;&lt;DIV&gt;&lt;BR /&gt;Obs x&lt;/DIV&gt;&lt;P&gt;below is the sas code.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;PROC SQL;&lt;/P&gt;&lt;P&gt;CONNECT TO ODBC AS ods ( DATAsrc=PriorityYou_Test USER='******' PASSWORD='******');&lt;BR /&gt;create table one as select * from connection to ods(&lt;BR /&gt;execute ([PriorityYou].[dbo].[HelloWorld] ) by ods;&lt;/P&gt;&lt;P&gt;DISCONNECT FROM ods;&lt;BR /&gt;QUIT;&lt;/P&gt;&lt;P&gt;proc print data=one;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want the entire thing done by thr Sp.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is it possible in SAS?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;First of all running a SQL SP through SAS and getting the whole output in sas possible?&lt;/P&gt;</description>
      <pubDate>Thu, 07 Feb 2019 01:32:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Executing-a-sql-stored-proc-through-sas-and-getting-the-results/m-p/533459#M146255</guid>
      <dc:creator>Swathi12</dc:creator>
      <dc:date>2019-02-07T01:32:20Z</dc:date>
    </item>
    <item>
      <title>Re: Executing a sql stored proc through sas and getting the results in to sas</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Executing-a-sql-stored-proc-through-sas-and-getting-the-results/m-p/533461#M146257</link>
      <description>&lt;P&gt;Create this stored procedure in SQL Server:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;-- ================================================
-- Template generated from Template Explorer using:
-- Create Procedure (New Menu).SQL
--
-- Use the Specify Values for Template Parameters 
-- command (Ctrl-Shift-M) to fill in the parameter 
-- values below.
--
-- This block of comments will not be included in
-- the definition of the procedure.
-- ================================================
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		&amp;lt;Author,,Name&amp;gt;
-- Create date: &amp;lt;Create Date,,&amp;gt;
-- Description:	&amp;lt;Description,,&amp;gt;
-- =============================================
CREATE PROCEDURE dbo.fubar
	-- Add the parameters for the stored procedure here
	@switch INT=1
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	IF @switch = 1
	BEGIN
		SELECT * FROM sys.databases
	END
	ELSE
	IF @switch =2 
	BEGIN
		DECLARE @MyTable TABLE (line VARCHAR(50))
		INSERT INTO @MyTable
		VALUES ('FOO'),('BAR'),('BLAH')
		SELECT * FROM @MyTable
	END
END
GO&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Run this code in SAS:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%libname_sqlsvr(libref=MyLib,server=&amp;lt;your server&amp;gt;,database=&amp;lt;your database&amp;gt;,schema=dbo)

proc sql;
   connect using mylib;
   create table fubar as
   select * from connection to mylib(
EXEC dbo.fubar
   )
quit;
proc print;
run;

proc sql;
   connect using mylib;
   create table fubar as
   select * from connection to mylib(
EXEC dbo.fubar @switch=1
   );
quit;
proc print;
run;

proc sql;
   connect using mylib;
   create table fubar as
   select * from connection to mylib(
EXEC dbo.fubar @switch=2
   );
quit;
proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;A href="https://github.com/scottbass/SAS/blob/master/Macro/libname_sqlsvr.sas" target="_blank"&gt;https://github.com/scottbass/SAS/blob/master/Macro/libname_sqlsvr.sas&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If you still have problems, 1) hit the SAS and/or SQL Server doc as needed, 2) ask a colleague, or 3) repeat #1.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hope this helps...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;P.S.:&amp;nbsp; My guess is you're expecting SAS to return PRINT statements.&amp;nbsp; It doesn't work that way.&amp;nbsp; If that's the case, see #1.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Lastly, if you return multiple result sets, SAS will only pick up the first one (using the above technique).&amp;nbsp; You have to combine all your result sets somehow (if you really need to do this).&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Otherwise, if you REALLY need multiple, separate results, your stored procedure could create multiple tables, either in a tmp schema in your database, or in ##global temp tables, then you could print them with multiple print procedures, then delete the tables from SAS.&amp;nbsp; Cumbersome but would work.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 07 Feb 2019 02:05:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Executing-a-sql-stored-proc-through-sas-and-getting-the-results/m-p/533461#M146257</guid>
      <dc:creator>ScottBass</dc:creator>
      <dc:date>2019-02-07T02:05:41Z</dc:date>
    </item>
  </channel>
</rss>

