BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Alathuythu
Calcite | Level 5

 

hello,

 

I'm new with sas. Can someone to help me with this simple script looping or macro?


proc sql;
connect using oracle; 
create table  temp_oracle as
select * from connection to oracle
(
     select * from table a

          where variable in(&split1.)
                     or variable in(&split2.)
                     or variable in(&split3.)
                     or variable in(&split4.)
                     or variable in(&split5.)
                     or variable in(&split6.) ….
)

;quit;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

I take it you want to reduce the typing to get this repetitive code:

variable in(&split1.)
                     or variable in(&split2.)
                     or variable in(&split3.)
                     or variable in(&split4.)
                     or variable in(&split5.)
                     or variable in(&split6.) ….

Here's a macro that generates the code for you:

 

%macro splits (n_splits);
   %local k;
   %do k = 1 %to &n_splits;
      variable in (&&split&k)
      %if &k < &n_splits %then or;
   %end;
%mend splits;

You would use that macro to replace all the typing:

 

(
     select * from table a

          where %splits (6)

)

Use whatever number is appropriate in parentheses, it doesn't have to be 6.

View solution in original post

5 REPLIES 5
SuryaKiran
Meteorite | Level 14

Hello,

 

Need more information on how many time you need this loop and on what basis. You probably can create that condition using a dataset or write a logic to put the whole condition as a macro and call it in pass-through. 

 

Loop to be outside the pass-through. 

 

%let condition=variable in(&split1.)
                     or variable in(&split2.)
                     or variable in(&split3.)
                     or variable in(&split4.)
                     or variable in(&split5.)
                     or variable in(&split6.) ;

proc sql;
connect using oracle; 
create table  temp_oracle as
select * from connection to oracle
(
     select * from table a

          where &condition
)

;quit;

Provide more information so to create a logic for creating the condition. 

Thanks,
Suryakiran
ed_sas_member
Meteorite | Level 14

Hi @Alathuythu 

 

Do you want to loop through the "WHERE" clause as follows?

 

/* First iteration */
proc sql;
connect using oracle; 
create table  temp_oracle as
select * from connection to oracle
(
     select * from table a where variable in (&split1.)
)
;quit;

/* Second iteration */
proc sql;
connect using oracle; 
create table  temp_oracle as
select * from connection to oracle
(
     select * from table a where variable in (&split2.)
)
;quit;

/* etc. */
Astounding
PROC Star

I take it you want to reduce the typing to get this repetitive code:

variable in(&split1.)
                     or variable in(&split2.)
                     or variable in(&split3.)
                     or variable in(&split4.)
                     or variable in(&split5.)
                     or variable in(&split6.) ….

Here's a macro that generates the code for you:

 

%macro splits (n_splits);
   %local k;
   %do k = 1 %to &n_splits;
      variable in (&&split&k)
      %if &k < &n_splits %then or;
   %end;
%mend splits;

You would use that macro to replace all the typing:

 

(
     select * from table a

          where %splits (6)

)

Use whatever number is appropriate in parentheses, it doesn't have to be 6.

Alathuythu
Calcite | Level 5
This is work perfectly. Thank you very much!
Tom
Super User Tom
Super User

@Alathuythu wrote:

 

hello,

 

I'm new with sas. Can someone to help me with this simple script looping or macro?


proc sql;
connect using oracle; 
create table  temp_oracle as
select * from connection to oracle
(
     select * from table a

          where variable in(&split1.)
                     or variable in(&split2.)
                     or variable in(&split3.)
                     or variable in(&split4.)
                     or variable in(&split5.)
                     or variable in(&split6.) ….
)

;quit;

 


Your code currently does not define (or use) any macros. 

It is referencing 6 macro variables. But you did not show how you are defining those macro variables.

 

What is it you need help with? 

CFC_SAS_Communities_400x225.jpgCFC_SAS_Communities_400x225.jpg

Call for content now open!

It's your turn to help shape SAS Innovate 2027. Share your expertise and inspire the SAS community.

Submit your proposal →

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
  • 5 replies
  • 4374 views
  • 1 like
  • 5 in conversation