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

Hello,

 

I have this:

 

/*This works*/
proc sql;    
  connect to oracle (user=&usr
password=&psswd
path=&path
buffsize=5000); 

create table temp as
select * from connection to Oracle(
select count(*)
from schema.table
);
disconnect from oracle;
quit;

But if I add a %include and have the query inside that, then it does NOT work. Why?

/*This works*/
proc sql;    
  connect to oracle (user=&usr
password=&psswd
path=&path
buffsize=5000); 

create table temp as
select * from connection to Oracle(
%include "/path/to/code/code.sas";
);
disconnect from oracle;
quit;
1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

Anything inside of the parentheses in

 

from connection to Oracle( )

 

must be valid code that Oracle understands. I don't think Oracle understands %include.

 

 

--
Paige Miller

View solution in original post

8 REPLIES 8
PaigeMiller
Diamond | Level 26

Anything inside of the parentheses in

 

from connection to Oracle( )

 

must be valid code that Oracle understands. I don't think Oracle understands %include.

 

 

--
Paige Miller
david27
Quartz | Level 8

Thank You

Tom
Super User Tom
Super User

@PaigeMiller wrote:

Anything inside of the parentheses in

 

from connection to Oracle( )

 

must be valid code that Oracle understands. I don't think Oracle understands %include.

 

 


It is not that ORACLE does not understand %INCLUDE.

The issue is that the SAS macro processor only recognizes %INCLUDE at a statement boundary.  You cannot use it after you have already begun the CREATE or SELECT statement.

Reeza
Super User

Anything between the parenthesis below (highlighted in red) are passed directly to the Oracle server. The Oracle server has no knowledge of SAS so SAS command are not valid in that section. You are using SQL pass through which takes the code, passes it to the Oracle server, Oracle executes the commands and returns the results based on the query. 

 

proc sql;    
  connect to oracle (user=&usr
password=&psswd
path=&path
buffsize=5000); 

create table temp as
select * from connection to Oracle(
%include "/path/to/code/code.sas";
);
disconnect from oracle;
quit;
david27
Quartz | Level 8

Thank You.

Tom
Super User Tom
Super User

Try using a utility to read the file and emit the lines as code.

For example you could try MODE=3 option of this macro:  %fread() 

But don't call the file a SAS file if all it contains is SQL code.

create table temp as
select * from connection to Oracle(
%fread("/path/to/code/code.sql",mode=3)
);

Note: Do not expect SAS to honor line breaks in your SQL file.  So do not use those goofy -- end of the line comments.  If you want to have comments in your SQL code use real /* */ block comments.

yabwon
Onyx | Level 15

One other option would be to put it all in the include, like:

filename Head TEMP;
filename Body "/path/to/code/code.sas";
filename Tail TEMP;

data _null_;
file Head; input; put _infile_;
cards4;
proc sql;    
  connect to oracle (user=&usr
password=&psswd
path=&path
buffsize=5000); 
create table temp as
select * from connection to Oracle(
;;;;
run;

data _null_;
file Tail; input; put _infile_;
cards4;
);
disconnect from oracle;
quit;
;;;;
run;

%include Head Body Tail / source2;

 

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Tom
Super User Tom
Super User

That works as well.

Note you just need to have the beginning of the SAS statement in the PRE file because the %INCLUDE statement cannot be called in the middle of a SAS statement. There is no need for a POST file, you can just have that part of the statement in the program itself.

 

Here is an example you can try.  The is example is just using SASHELP dataset to demonstrate the idea.  Just update the prefix code to reference Oracle if you need.

options parmcards=code;
filename code temp;

parmcards;
select * from sashelp.class
;

options parmcards=pre;
filename pre temp;

parmcards;
create table xx as (
;

proc sql;
%include pre code / source2;
);
quit;

Result

347  proc sql;
348  %include pre code / source2;
NOTE: %INCLUDE (level 1) file PRE is file
      C:\Users\...\#LN00096.
349 +create table xx as (
NOTE: %INCLUDE (level 1) ending.
NOTE: %INCLUDE (level 1) file CODE is file
      C:\Users\...\#LN00095.
350 +select * from sashelp.class
NOTE: %INCLUDE (level 1) ending.
351  );
NOTE: Table WORK.XX created, with 19 rows and 5 columns.

352  quit;
NOTE: PROCEDURE SQL used (Total process time):
      real time           0.02 seconds
      cpu time            0.01 seconds

 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 990 views
  • 2 likes
  • 5 in conversation