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

Hi,

I have a list of file with name say, one two three ...

They all have the same number of row and they all have a variable date.

I want to run the below SQL code on all combinations of file.

 

so there will be ouput file onetwo ,onethree, twothree .

 

Can you help me to create a Loop to do it?

Thank you,

 

HHCFX

 

proc sql;
create table onetwo
as select * from one join two
from a.date=b.date;quit; 
1 ACCEPTED SOLUTION

Accepted Solutions
hhchenfx
Barite | Level 11

 

here we go

	%let Rate_list= one two three;

	%macro BUYcode;
	%do i=1 %to %sysfunc(countw(&Rate_list));
			%let rate1=%scan(&Rate_list,&i);
	%do j=&i+1 %to %sysfunc(countw(&Rate_list));
			%let rate2=%scan(&Rate_list,&j);

			proc sql;
			create table &rate1._&rate2
			as select *
			from &rate1 as a join &rate2 as b
			on a.date=b.date;quit; 	
	%end;
	%end;
	%mend;

%BUYcode;

View solution in original post

2 REPLIES 2
ballardw
Super User

 

Do you have the list of names in a data set? Does it include any needed library to find the sets? If you have a library name what do want for the output?

What is longest name of the datasets involved? If your names are longer than 16 characters you have a strong possibility of exceeding the 32 character limit for data set names the way you combine them.

 

This may get you started:

data listdataset;
   informat name $41.;
   input name;
datalines;
one
two 
three
alongerset
;
run;

proc sql;
   create table control as
   select a.name, b.name as bname
   from listdataset as a, listdataset as b
   where a.name lt b.name
   ;
quit;

data _null_;
   set control;
   call execute ('proc sql; create table '||
      cats(name,bname)||'as select'|| 
        catx(',',catx(',',name,'*'), catx(',',bname,'*'))||
     'from '|| name||' join '|| bname ||
     'from ' catx('=',catx('.',name,'date'), catx('.',bname,'date') )||
    ';quit;');
run;

Caution: if combinations of your data set names aren't unique you won't get what you want:

 

Example:

TE NT and

T  ENT would both try to create TENT. So only the last combination run would exist.

If that might happen you would need to insert _ character between names to create TE_NT and T_ENT. Which reduces the number of characters allowed in the data set names by one per possible combination.

You are going to get a low of warnings about the variable DATE in both sets. If you have any other variables with the same name in the data sets your results are unpredictable and possibly generate errors if they are not of the same type.

hhchenfx
Barite | Level 11

 

here we go

	%let Rate_list= one two three;

	%macro BUYcode;
	%do i=1 %to %sysfunc(countw(&Rate_list));
			%let rate1=%scan(&Rate_list,&i);
	%do j=&i+1 %to %sysfunc(countw(&Rate_list));
			%let rate2=%scan(&Rate_list,&j);

			proc sql;
			create table &rate1._&rate2
			as select *
			from &rate1 as a join &rate2 as b
			on a.date=b.date;quit; 	
	%end;
	%end;
	%mend;

%BUYcode;

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 2 replies
  • 564 views
  • 0 likes
  • 2 in conversation