BookmarkSubscribeRSS Feed
ljw4
Calcite | Level 5

I am trying to link on a character field but 1 table the character is 3 digits and the other it is 4 digits. I was going to add a leading zero to the 3 digits but I can only see how to use the z function with numbers not characters. I think I need to use a right conc function but I can't figure out how to make that join. 

7 REPLIES 7
Reeza
Super User
if length(variable) = 3 then variable = catt("0", variable);
ljw4
Calcite | Level 5
Thank you. Where do I put this in my code? This is my code below

PROC SQL;
CREATE TABLE MSDRG_table AS
SELECT t1.MSDRG,
t1.DischargeFiscalYearNBR,
t1.MSDRGMajorDiagnosticCategoryCD,
t1.MSDRGTypeCD,
t1.MSDRGDSC,
t1.MSDRGWeightNBR,
t1.MSDRGeometricMeanLengthOfStayNBR,
t1.RowStatusDSC,
t1.CreateDTS,
t1.UpdateDTS,
t1.MSDRGAverageLengthOfStayNBR,
t1.EDWLastModifiedDTS,
if length(MSDRG) = 3 then variable = catt("0", MSDRG)

FROM ANALYTIC.MSDRG t1
WHERE t1.DischargeFiscalYearNBR = 2019;
QUIT;


Reeza
Super User
SQL requires CASE WHEN, not an IF statement. (There was no indication this was a SQL question that I could see).

https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/sqlproc/n0a85s0ijz65irn1h3jtariooea5.htm

Since you are using SQL, Tom's answer is more appropriate.
Tom
Super User Tom
Super User

@ljw4 wrote:

I am using SAS not SQL


In SAS to use SQL syntax you need to use it inside of PROC SQL step.

Do you totally not want to use any SQL code?

 

If you really must use data step to join the tables then you will need modify one or both of the variables.

data for_merge_one;
  set one;
  numid = input(id,32.);
run;
proc sort; by numid; run;
data for_merge_two;
  set two;
  numid=input(id,32.);
run;
proc sort; by numid; run;
data want;
  merge for_merge_one for_merge_two;
  by numid;
run;
Tom
Super User Tom
Super User

If you use SQL to join you can match on function results. 

If the strings are just digit strings of less 16 characters then just convert both to numbers and match the numbers.

proc sql;
create table want as
select *
from table1 
inner join table2
on input(table1.id,32.) = input(table2.id,32.)
;
quit;
ljw4
Calcite | Level 5
This worked but my issue is I need a left join and when I use this with a
left join I'm still only getting results of an inner join. Does that make
sense?

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 7 replies
  • 2132 views
  • 2 likes
  • 3 in conversation