BookmarkSubscribeRSS Feed
milts
Pyrite | Level 9
Hello,

let's say i have tables a and b.

a has var1 var2 and b has var3 var4

then i need to create a table that adds a column to table a named tag which depends on the first 5 chars of var1. so when substr(var1,1,5) is equal to a certain value of var3 then tag = var4.

here's the code:

proc sql;
select * from a left join b
on substr(var1,1,5) = var3;
quit;

assuming that there are only two values for var4. how come when i create a table that separates the two values of var3, their total count is different to the whole?

i feel i wasn't able to explain it well but if you understood what i'm trying to talk about your help is much appreciated. is there wrong with my code? or logic? or other thing? thanks a lot! 😃
2 REPLIES 2
LinusH
Tourmaline | Level 20
One have to be careful when joining tables. In most cases you wish to join on key columns, that is the on/where-clause should contain unique values at least in on of the tables being joined, otherwise there might unpredictable results. Has var3 unique vales? Please attach some example data (of both input and expected output).

Regards,
Linus
Data never sleeps
RichardDeVen
Barite | Level 11
Your question is one of performing a lookup. In SQL creating a table with a resolved lookup generally involves the use of a LEFT JOIN.

data one;
length var1 var2 $8;
input var1 var2;
datalines;
abcdef qwerty
bcdefg tyuiop
cdefgh zxcvb
run;

data two;
length var3 var4 $8;
input var3 var4;
datalines;
abcde Richard
xyzab Linus
run;

proc sql;
create table one_tagged as
select
one.*
, two.var4 as tag
from
one
left join
two
on
substr(one.var1,1,5) = two.var3
;
quit;

Note that these types of joins are often utilized as a VIEW stored in a permanent library (create view instead of create table). That way the join is always upto date.

Furthermore, this type of lookup (left join) can also be accomplished using custom formats. A common use of formats is to map a range of values to some other value (a lookup)

Richard A. DeVenezia
http://www.devenezia.com

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
  • 2 replies
  • 1730 views
  • 0 likes
  • 3 in conversation