BookmarkSubscribeRSS Feed
Alankar
Fluorite | Level 6
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?

5 REPLIES 5
LinusH
Tourmaline | Level 20
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
Alankar
Fluorite | Level 6

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?

Reeza
Super User

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

Kurt_Bremser
Super User

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;
Oligolas
Barite | Level 11

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;

 

________________________

- Cheers -

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

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
  • 2329 views
  • 4 likes
  • 5 in conversation