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

Hello for all,

 

 

I'm new using SAS and PROC SQL.  I wrote a code to cartesian product with different values of 5 variables but i want to parameterize it.

 

%macro first_macro(size);
%let list_var = var1 var2 var3 var4 var5;
%do i=1 %to &size;
 %let var = %scan(&list_var,&i);
	proc sql;
	create table table&i as
	select distinct &var
	from file
	quit;
%end;

PROC SQL;Create Table cross_1 as Select monotonic() as id_5,* from table1,table2,table3,table4,table5;Quit;
PROC SQL;Create Table cross_2 as Select monotonic() as id_4,* from table1,table2,table3,table4;Quit;
PROC SQL;Create Table cross_3 as Select monotonic() as id_3,* from table1,table2,table3;Quit;
PROC SQL;Create Table cross_4 as Select monotonic() as id_2,* from table1,table2;Quit;
PROC SQL;Create Table cross_5 as Select monotonic() as id_1,* from table1;Quit;

%mend;


%first_macro(5);

There is one way to declare a list of tables separated by ',' and using it in proc sql sentence with loop?

 

thanks

1 ACCEPTED SOLUTION

Accepted Solutions
yabwon
Onyx | Level 15

Yes,

%macro first_macro(size);
%let list_var = var1 var2 var3 var4 var5;
%do i=1 %to &size;
 %let var = %scan(&list_var,&i);
	proc sql;
	create table table&i as
	select distinct &var
	from file
	quit;
%end;

%do i=1 %to &size;
  PROC SQL;
  create table cross_&i as 
  select monotonic() as id_&i ,* 
  from table1 
    %do j=2 %to &i;
      ,table&j.
    %end; 
  ;
  Quit;
%end;

%mend;

%first_macro(5);

All the best

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



View solution in original post

6 REPLIES 6
yabwon
Onyx | Level 15

Hi,

 

do you mean use of macroquoting function, like %STR()

%macro first_macro(size, listOfFiles=);
%let list_var = var1 var2 var3 var4 var5;
%do i=1 %to &size;
 %let var = %scan(&list_var,&i);
	proc sql;
	create table table&i as
	select distinct &var
	from file
	quit;
%end;

PROC SQL;Create Table cross_1 as Select monotonic() as id_5,* from &listOfFiles.;Quit;
%mend;


%first_macro(5, listOfFiles=%str(table1,table2,table3,table4,table5));

 

All the best

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



liguang01
Obsidian | Level 7

Yes but i want to create cross_1, cross_2... 

 

In the first iteration it will have to use table1--table5. In the second, it will have to use table1--table4...

yabwon
Onyx | Level 15

Hi,

 

Like that?

 

%macro first_macro(size, listOfFiles=);
%let list_var = var1 var2 var3 var4 var5;
%do i=1 %to &size;
 %let var = %scan(&list_var,&i);
	proc sql;
	create table table&i as
	select distinct &var
	from file
	quit;
%end;

%do i=1 %to &size;
  PROC SQL;
  create table cross_&i as 
  select monotonic() as id_&i ,* 
  from table1 
    %do j=2 %to &i;
      ,%qscan(&listOfFiles., &j, %str(,))
    %end; 
  ;
  Quit;
%end;

%mend;

%first_macro(5, listOfFiles=%str(table1,table2,table3,table4,table5));

If it could be a space separate list of tables you could do it without %str()

 

%macro first_macro(size, listOfFiles=);
%let list_var = var1 var2 var3 var4 var5;
%do i=1 %to &size;
 %let var = %scan(&list_var,&i);
	proc sql;
	create table table&i as
	select distinct &var
	from file
	quit;
%end;

%do i=1 %to &size;
  PROC SQL;
  create table cross_&i as 
  select monotonic() as id_&i ,* 
  from table1 
    %do j=2 %to &i;
      ,%qscan(&listOfFiles., &j, %str( ))
    %end; 
  ;
  Quit;
%end;

%mend;

%first_macro(5, listOfFiles=table1 table2 table3 table4 table5);

 

All the best

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



liguang01
Obsidian | Level 7

It is exact what i want thanks. 

 

is  there is possible that you dont specify list of tables and the program catch it by size (parameter of macro) ?

yabwon
Onyx | Level 15

Yes,

%macro first_macro(size);
%let list_var = var1 var2 var3 var4 var5;
%do i=1 %to &size;
 %let var = %scan(&list_var,&i);
	proc sql;
	create table table&i as
	select distinct &var
	from file
	quit;
%end;

%do i=1 %to &size;
  PROC SQL;
  create table cross_&i as 
  select monotonic() as id_&i ,* 
  from table1 
    %do j=2 %to &i;
      ,table&j.
    %end; 
  ;
  Quit;
%end;

%mend;

%first_macro(5);

All the best

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

DON"T use comma as the delimiter in your macro code. 

You can have the macro put in the commas where they are needed.

For example you could loop loop over the list of variables and then loop over the list of tables.

%macro mymacro(varlist,tablelist);
%local i j var sep;
proc sql;
%do i=1 %to %sysfunc(countw(&varlist,%str( )));
  %let var = %scan(&varlist,&i,%str( ));
create table table&i as
  select distinct &var
  from 
  %let sep=;
  %do j=1 %to %sysfunc(countw(&tablelist,%str( )));
     &sep.%scan(&tablelist,&j,%str( ))
    %let sep=,;
  %end;
;
%end;
quit;
%mend mymacro;
%mymacro(varlist=var1 var2 var3,tablelist=table1 table2 table3)

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 6 replies
  • 1427 views
  • 4 likes
  • 3 in conversation