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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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