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 -

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

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