DATA Step, Macro, Functions and more

join many tables with SQL full outer join

Reply
Contributor
Posts: 25

join many tables with SQL full outer join

DATA REVENUE;
MERGE REV_1004 (KEEP = ID REVENUE RENAME =(REVENUE = REV1004)
	REV_1005 (KEEP = ID REVENUE RENAME =(REVENUE = REV1005)
	REV_1006 (KEEP = ID REVENUE RENAME =(REVENUE = REV1006)
	REV_1007 (KEEP = ID REVENUE RENAME =(REVENUE = REV1007)
	REV_1008 (KEEP = ID REVENUE RENAME =(REVENUE = REV1008)
	REV_1009 (KEEP = ID REVENUE RENAME =(REVENUE = REV1009)
	REV_1010 (KEEP = ID REVENUE RENAME =(REVENUE = REV1010)
	REV_1011 (KEEP = ID REVENUE RENAME =(REVENUE = REV1011)
	REV_1012 (KEEP = ID REVENUE RENAME =(REVENUE = REV1012)
	REV_1101 (KEEP = ID REVENUE RENAME =(REVENUE = REV1101)
	REV_1102 (KEEP = ID REVENUE RENAME =(REVENUE = REV1102)
	REV_1103 (KEEP = ID REVENUE RENAME =(REVENUE = REV1103);
BY ID;
RUN;

I would like to write the same code in SQL and output supposed to be same, any help?

Super User
Posts: 5,254

Re: join many tables with SQL full outer join

Just use the FILM JOIN - ON construct.
Why do you want to use SQL I'd you already have a working piece of code?

Without knowing anything about the data, but shouldn't a union be more appropriate? Looks like you are denormalizing.
Data never sleeps
Contributor
Posts: 25

Re: join many tables with SQL full outer join

I am exploring SQL options, thus trying to convert SAS data steps and procedures in to PROC SQL.

by the way do we have FILM JOIN? never heard of it. how to use?

Super User
Posts: 17,745

Re: join many tables with SQL full outer join

I would echo suggestion from above, union or stack datasets and then transpose from wide to long. 

Super User
Posts: 6,928

Re: join many tables with SQL full outer join

The only thing that I would do with this code is that I would automate it so I only need to set begin and end months, and the code is built dynamically.

 

%let begin='01apr2010'd;
%let end='01mar2011'd;

data _null_;
length command $100;
date = &begin;
call execute('data revenue; merge ');
do while (date <= &end);
  yymm = substr(put(year(date),z4.),3,2)!!put(month(date),z2.);
  command = 'rev_'!!yymm!!' (keep=id revenue rename=(revenue = rev'!!yymm!!')';
  call execute(command);
  date = intnx('month',date,1,'begin');
end;
call execute('; by id; run;');
run;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Frequent Contributor
Posts: 103

Re: join many tables with SQL full outer join

[ Edited ]

In this particular case one could also shorten the code to:

 

DATA _NULL_;
   SET sashelp.vtable end=last;
   where libname eq 'WORK' and substr(memname,1,4) eq 'REV_';
   if _N_ eq 1 then call execute('DATA REVENUE; MERGE ');
   call execute(strip(memname)||' (KEEP = ID REVENUE RENAME =(REVENUE = '||strip(compress(memname,"_"))||'))');
   if last then call execute(';BY ID; RUN;');
RUN;

 

________________________

- That still only counts as one -

Ask a Question
Discussion stats
  • 5 replies
  • 362 views
  • 4 likes
  • 5 in conversation