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

I need to join tables, but the key must be different depending on the conditions
 Exemple 

Given each type and salary the following fee must be charged

Table 1

TypesalaryFEE(%)
A10001
A20002
A30002,5
B10002
B20003
C20001


Table 2 

 

clientetypesalary
1A1500
2B1600
3A1000
4B3500
5C8000
6C2000


My Key is type and salary, but the fee the fee must be charged according to the previous range

Exemple
cliente 1 must pay 1 % because the salary is smaller then the second type 2000.

how can I create the condition of the key considering the range?

I tried "if  - then ", but I have many conditions (more than 1000, it would be unfeasible)

  

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

A datastep solution:

 

data fees;
input type $ salary  FEE;
datalines;
A   1000    1
A   2000    2
A   3000    2.5
B   1000    2
B   2000    3
C   2000    1
;

data clients;
input cliente type $ salary;
datalines;
1   A   1500
2   B   1600
3   A   1000
4   B   3500
5   C   8000
6   C   2000
;

proc sort data=clients; by type salary;

data want;
merge fees (in=inFee) clients(in=inClients); by type salary;
retain f;
if inFee then f = fee;
if inClients;
drop fee; rename f=fee;
run;

proc sort data=want; by cliente; run;

proc print data=want; run;

PGStats_0-1614659673521.png

 

Note that you should have a record with a salary = 0 for each type to cover all cases.

 

PG

View solution in original post

1 REPLY 1
PGStats
Opal | Level 21

A datastep solution:

 

data fees;
input type $ salary  FEE;
datalines;
A   1000    1
A   2000    2
A   3000    2.5
B   1000    2
B   2000    3
C   2000    1
;

data clients;
input cliente type $ salary;
datalines;
1   A   1500
2   B   1600
3   A   1000
4   B   3500
5   C   8000
6   C   2000
;

proc sort data=clients; by type salary;

data want;
merge fees (in=inFee) clients(in=inClients); by type salary;
retain f;
if inFee then f = fee;
if inClients;
drop fee; rename f=fee;
run;

proc sort data=want; by cliente; run;

proc print data=want; run;

PGStats_0-1614659673521.png

 

Note that you should have a record with a salary = 0 for each type to cover all cases.

 

PG

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 connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 1 reply
  • 912 views
  • 1 like
  • 2 in conversation