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

Dear Experts, 

 

I will have to join two tables. Table 1 contains information about sales in NOK, and Table 2 contains information on Exchange rates. I will have to join the table to get the appropriate exchange rate to the sales table. Exchange rate table does not contain rates for weekends and holidays. So we will have to use the latest available rate for weekends and Holidays. This process going to be performed in very large tables.

 

Sales table

DateValue
01/01/2020652
02/01/2020325
03/01/2020632
04/01/2020256
05/01/2020852
06/01/2020369
07/01/2020999
08/01/2020875

 

Exchange rate table

DateExchange rate
01/01/20200.6589
02/01/20200.6583
03/01/20200.7412
04/01/20200.9875
05/01/20200.9632
08/01/20200.9873
09/01/20200.9345
10/01/20200.6322

 

Desired Results:

Joined table

DateValueExchange rate
01/01/20206520.6589
02/01/20203250.6583
03/01/20206320.7412
04/01/20202560.9875
05/01/20208520.9632
06/01/20203690.9632
07/01/20209990.9632
08/01/20208750.9873

 

Thank you in advance for your help.

 

Regards,

Myu

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

An easy way assumes both data sets are sorted by DATE:

 

data want;
   set exchange_rate (in=new_rate)  sales (in=keepme);
   by date;
   retain latest_rate;
   if new_rate then latest_rate = exchange_rate;
   if keepme;
   exchange_rate = latest_rate;
   drop latest_rate;
run;
   

View solution in original post

5 REPLIES 5
novinosrin
Tourmaline | Level 20

HI @Myurathan  Please try if you like-


data sales;
input Date :ddmmyy10.	Value;
format date ddmmyy10.;
cards;
01/01/2020	652
02/01/2020	325
03/01/2020	632
04/01/2020	256
05/01/2020	852
06/01/2020	369
07/01/2020	999
08/01/2020	875
;

data Exchange_rate;
input Date  :ddmmyy10.	Exchange_rate;
format date ddmmyy10.;
cards;
01/01/2020	0.6589
02/01/2020	0.6583
03/01/2020	0.7412
04/01/2020	0.9875
05/01/2020	0.9632
08/01/2020	0.9873
09/01/2020	0.9345
10/01/2020	0.6322
;

data want ;
 if _n_=1 then do;
   if 0 then set sales Exchange_rate;
   dcl hash H (dataset:'Exchange_rate') ;
   h.definekey  ("date") ;
   h.definedata ("Exchange_rate") ;
   h.definedone () ;
 end;
 set sales;
 _iorc_=h.find();
run;

 Since your dataset sales is sorted by "date". It's very straight forward.

Myurathan
Quartz | Level 8
@novinosrin. Thank you so much for your quick reply.

How would I have to use one more variable (Currency) to join in your code?
EX: Sales table has a currency column and Exchange rate table also have a current column.

Cheers.
novinosrin
Tourmaline | Level 20

Hi @Myurathan , You could add the variables to definedata list from the exchange table

 h.definedata ("Exchange_rate") ;

Set sales;

would anyway read all the variables from sales table.

 

@Astounding 's merge is perhaps much easier to understand and manage. If I am unfamiliar with Hashing, I wouldn't bother though this particular task is very simple albeit can become problematic later when queries become complex. Nonetheless, I will leave that up to you

Astounding
PROC Star

An easy way assumes both data sets are sorted by DATE:

 

data want;
   set exchange_rate (in=new_rate)  sales (in=keepme);
   by date;
   retain latest_rate;
   if new_rate then latest_rate = exchange_rate;
   if keepme;
   exchange_rate = latest_rate;
   drop latest_rate;
run;
   
Myurathan
Quartz | Level 8
Thank you so much for your help.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 855 views
  • 3 likes
  • 3 in conversation